Theory:Running programs on your computer
# Installing Java on your computer
In this topic, you will compile and run the simplest Hello World program on your computer. There is one prerequisite: you need to install a JDK to develop Java applications. Please, download the up to date Java version (opens new window). Our projects now support Java 17. Just follow the installation instructions given for your operating system.
To check that the installation has been completed, let's check the version of Java by typing the following command in a terminal:
java -version
It outputs the version of Java that is installed on your computer. If it does not work correctly, open the installation instructions and try to set the path
variable in your operating system.
If you get stuck with Javac not being recognizable, try to watch this video (opens new window).
# Writing a program
Let's write a simple program and then start it on your computer. To do that we will use a terminal.
Step 1. Create a file named Main.java
using any text editor (such as TextPad or NotePad++ for Windows; jEdit or gedit for Mac OS X; gedit for Ubuntu; or something else) and save it in a folder.
Step 2. Paste the following source code into this file:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Java");
}
}
2
3
4
5
6
The public class name must be the same as the file name.
提示
Note, we assume you already understand how the Hello World program works. If not, see here (opens new window) and then come back.
# Compiling and running a program
To run the program, we will use a terminal installed in your OS. All the following commands need to be executed from within the same folder that the .java
file is created in.
Step 3. Compile the program using the following command in the terminal:
javac Main.java
The javac
command asks the compiler to translate the source code into bytecode. The result of this command is a file named Main.class
.
Step 4. Run the compiled program (make sure that your terminal is open in the same directory as your source file):
java -cp . Main
The java
command starts a Java application. It does this by starting a JRE and invoking the main method inside the Main
class.
The -cp
parameter (classpath) specifies the location of user-defined classes and packages. The dot .
means the current terminal directory. We will consider it in detail in the next topics.
提示
Note: you should not specify the .class
extension when running a program.
The program should output the following text:
Hello, Java
Below is an animation that executes all these steps.
Congratulations! You have just started a simple program on your computer. Try to change this program, if you would like to get more interesting results.
提示
Since Java 11 it is possible to compile and run Java source code file using a single command java Main.java
. It will compile the file in-memory, so it does not produce a .class
file. Many developers don't know this small but interesting feature.