DEV Community

Cover image for 3. Exercise: Write, compile and run a Java program
Raghav Khanna
Raghav Khanna

Posted on

3. Exercise: Write, compile and run a Java program

3.1. Write source code
The following Java program is developed under Linux using a text editor and the command line. The process on other operating system should be similar, but is not covered in this description.

Select or create a new directory which will be used for your Java development. In this description the path \home\vogella\javastarter is used. On Microsoft Windows you might want to use c:\temp\javastarter. This path is called javadir in the following description.

Open a text editor which supports plain text, e.g., gedit under Linux or Notepad under Windows and write the following source code.

// a small Java program
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}

Save the source code in your javadir directory with the HelloWorld.java filename. The name of a Java source file must always equal the class name (within the source code) and end with the .java extension. In this example the filename must be HelloWorld.java, because the class is called HelloWorld

Top comments (0)