First program in Java
Programming Example: Hello World
code:
public class HelloWorld {
public static void main( String[] args ) {
System.out.println( "Hello World!" );
System.exit( 0 ); //success
}
}
- Type this program up in your text editor and save it as
HelloWorld.java
- Go to your console window (in Windows, go to the start menu and click "run", then type "cmd.exe"; in Linux, open a terminal). Use the cd command (
cd dirname
or .. to go up one directory) to navigate to the directory where HelloWorld.java was saved.
- Compile the program using the command
javac HelloWorld.java
- To run the compiled version of the HelloWorld program, on the command line simply type"java HelloWorld"
Remember that Java is case-sensitive, so even when you compile your program, you have to make sure you are typing in the file name exactly.