Run a Java program directly, without using Eclipse

Follow these directions if you’d rather type your Java programs with a familiar application such as Notepad, and run your Java programs from the Windows Command Prompt.

  1. Find the name of the folder that contains the application javac.exe (the Java compiler). On my machine, the folder was C:\Program Files\Java\jdk1.5.0_06\bin. The application java.exe (the Java Virtual Machine) should be in the same directory.
    start → Search → For Files or Folders… All files and folders → All or part of the file name: javac.exe
    Right-click on javac.exe → Open Containing Folder
    
  2. Put the name of the directory that holds javac.exe and java.exe into the PATH environment variable. This variable contains names of directories, separated by semicolons.
    Right-click on My Computer → Properties → Advanced → Environment Variables → System variables
    Select PATH and press Edit
    At the right end of the Variable value,
    insert a semicolon and C:\Program Files\Java\jdk1.5.0_06\bin
    Press the three OK buttons.
    
  3. After pressing the three OK buttons, open the Windows Command Prompt. Say PATH to verify that the directory holding javac.exe and java.exe is now in your PATH environment variable. Then go to the C:\ directory, create a subdirectory named myjava, and go there.
    start → Programs → Accessories → Command Prompt
    PATH
    
    
    cd C:\
    mkdir myjava
    dir
    
    
    cd myjava
    dir
    
  4. Open your myjava folder.
  5. Double-click on My Computer → Local Disk (C:) → myjava
    
    Use Notepad to create a file named HelloWorld.java in the myjava folder. It should contain the following Java program. If you want to edit this file again, you’ll have to right-click on it and select Open With → Notepad.
    public class HelloWorld {
    	public static void main(String[] args) {
    		System.out.println("Hello, world!");
    	}
    }
    
  6. In the command prompt, verify that the directory myjava now contains a file named HelloWorld.java. (If the filename ends with .txt, use the RENAME command to remove the .txt. This should not be necessary.) Run javac.exe to create a new file named HelloWorld.class.
    dir
    rename HelloWorld.java.txt HelloWorld.java
    dir
    
    javac HelloWorld.java
    dir
    
  7. Run java.exe to execute the HelloWorld.class file and send its standard output to the Command Prompt window. You can also use > to save the standard output in a .txt file.
    java HelloWorld
    Hello, world!
    
    java HelloWorld > HelloWorld.txt
    dir
    
  8. If your Java program uses the SWT (for example, HelloWorldSWT.java), download the swt-….zip file as in this step 3 and place it in the C:\myjava folder. Open the swt-….zip file with WinZip, creating a subdirectory named something like swt-M20070212-1330-win32-win32-x86. This subdirectory will contain a file named swt.jar as well as several .dll files. Then type the following javac and java commands. The java command is so long that you might want to split it onto two lines with the ^ character; you will be prompted with More?. The java.library.path tells java where the .dll files are; see Arnold, Gosling, Holmes, p. 663.
    cd C:\myjava
    javac -classpath swt-M20070212-1330-win32-win32-x86\swt.jar HelloWorldSWT.java
    dir
    
    java -classpath .;swt-M20070212-1330-win32-win32-x86\swt.jar ^
    More? -Djava.library.path=swt-M20070212-1330-win32-win32-x86 HelloWorldSWT
    
    Of course, if you move the swt.jar file and the .dll files up one level into the C:\myjava directory, the javac and java commands can be simplified to the following.
    cd C:\myjava
    javac -classpath swt.jar HelloWorldSWT.java
    dir
    
    java -classpath .;swt.jar HelloWorldSWT