Introduction to Programming in Java
5. "Hello World"
Anytime you learn a computer programming language, it is tradition that the first program you write should be to make your computer say, "Hello World". This is a small feat, but is a good opportunity to make sure that you installed the JDK properly.
The Java compiler reads basic text files. Open up a text editor like Notepad (Don't use a complex program like Word for this). Type this code, remembering that Java is case-sensitive:
public class HelloWorld { public static void main (String[] args) { System.out.println("Hello World!"); } }
Save this file as HelloWorld.java. Start your system's command line and navigate to the folder that you saved HelloWorld.java to. Type javac HelloWorld.java
.
This runs the java compiler, javac, and creates a class file, which contains the bytecode for the application. Next type java -cp . HelloWorld
.
This runs the class file that was just created by the compiler. Your console should print:
Hello World!