Creating a new Java program
Create a new text file named HelloWorld.java
and paste this code in it:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Note: In order for Java to recognize this as a class (and not throw a compiler error), the filename must comprise the name of the class
(HelloWorld
in this example) with a .java extension. Convention states that Java classes should begin with an uppercase character.
To compile your code, open the directory where HelloWorld.java
is located in a terminal window:
cd /path/to/containing/folder/
and enter javac
followed by the file name and extension as follows:
$ javac HelloWorld.java
Note: The javac
command invokes the Java compiler.
The compiler will then generate a bytecode file called HelloWorld.class
which can be executed in the Java Virtual Machine (JVM). The Java programming language compiler, javac, reads source files written in the Java programming language, and compiles them into bytecode class files. Optionally, the compiler can also process annotations found in source and class files using the Pluggable Annotation Processing API. The compiler is a command line tool but can also be invoked using the Java Compiler API.
Next enter java
, followed by the name of the class with the main
method (HelloWorld
in our example). Notice how the .class
is omitted:
$ java HelloWorld
Note: The java
command runs a Java application.
This will output:
Hello, World!
You have successfully programmed and built your very first Java program!
Note: In order for Java commands (
java
,javac
, etc) to be recognised, you will need to make sure:
- A JDK is installed (e.g. Oracle, OpenJDK and other sources)
- Your environment variables are properly set up
You will need to use a compiler (
javac
) and an executor (java
) provided by your JVM.java -version
andjavac -version
on the command line, will report which versions (e.g.1.8.0_73
) of these are installed.
A closer look at the Hello World application
The Hello World application consists of a HelloWorld
class definition and a main
method.
The class
keyword begins the class definition for a class named HelloWorld
. Every Java application contains at least one class definition (Further information about classes).
The main
method:
This is an entry point method (defined by its name main
) from which the JVM can run your program. Every Java program should have one. It is:
public
: meaning where the method can be called from isn't limited to inside your program.static
: meaning it exists and can be run by itself (at the class level without creating an object).void
: meaning it returns no value. Note: This is unlike C where a return code such asint
is expected (Java's way isSystem.exit()
).
It accepts :
- An array (called
args
) ofString
s used to pass arguments into your program (e.g. from command line arguments).
Almost all of this is required for a Java entry point method. The exceptions being:
- The name
args
is a variable name, so can be called anything. - Whether its type is an array or Varargs (
String... args
).
Note A single application may have multiple classes containing an entry point (main
) method, and the entry point of the application is determined by the class name passed as argument to the java
command.
Sign up or log in
Save edit as a guest
Join Stack Overflow
We recognize you from another Stack Exchange Network site!
Join and Save Draft