How to compile and run Java programs

Compile the source

Run the compiler in the folder containing HelloWorld.java. Success creates HelloWorld.class.

javac HelloWorld.java

Run the class

Pass the class name without .class to the Java launcher.

java HelloWorld

Use source-file mode

Modern Java can compile and run a single source file in one command, which is convenient for small exercises.

java HelloWorld.java

Recognize the two stages

  • javac reports compile-time syntax and type errors.
  • java starts the JVM and may report runtime errors.
  • Recompile after changing source before running the class again.