By Alvin Alexander. Last updated: June 4, 2016
If you're just getting started with Java it may help to have a simple first class to look at. In the code below I'm showing a sample Java class named HelloWorld
.
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, world"); } }
To use this class, save it to your system in a file named HelloWorld.java
. Then, compile it to Java bytecode with this command:
javac HelloWorld.java
This javac
command creates a new file named HelloWorld.class
which contains the bytecode for this class. (It's a binary file you can't really look at with an editor.) You can think of the javac
command as meaning "java-compile" if that helps.
To run your new program just type this command:
java HelloWorld
This java
command starts up your JVM, loads your class file (HelloWorld.class
), and then runs the program. The result is that it should print the following output:
Hello, world