Up Feedback Search                                                             



How to edit, compile and run Java programs? 


Edit: Open up your favorite text editor, (gedit, emacs, or vi, refer to this quick manual question 1), create a file with .java extension. For example, I could create a  simple java program like the following:  

public class Test


    public static void main(String[] args)


 System.out.println("Hollow World!");



Saved with a name: Test.java, note: your class name and the source file name has to have the same name before the extension if the class is the one which has the public access modifier. Also, by convention, your class name should start with an up case letter.


Compile: Our java installation is in /usr/local/jdk1.5.0/bin, which requires being included in user’s PATH environment variable. If you are a computer major student, your path should be already set up for using those java tools, (if not, refer to quick start HowTo manual question 2 to set up your property files to include java and other necessary executables).


Within the directory that you saved Test.java, type:  javac source_file.  For  example, you type: 

% javac Test.java 

This will create a java bytecode of our program in the same directory as the source code, named: “Test” to be ready to execute by JVM


Optional, you could use javac options to dynamically define or point out your sourcepath, and output directory for that particular session, something like the following:

% javac  –sourcepath  ~/Test.java  –d  classes

Suppose you are not exactly in your source file directory which is your home directory and you want the compiled file put in the classes directory.



Run: JVM needs to know where to load your bytecode, there are two ways to declare class locations to the JVM.  You could set it up in your CLASSPATH environment variables or declared with the “-cp’ flag option.


We could run our program with:

% java Test


% java -cp . Test

At the prompt if our bytecode or classes file in the current working folder (that is the case for us since we did not direct the output to a different place).  The only difference between those two commands is for the former, your current directory has already included in your CLASSPATH, for the latter is not.


You could set up the CLASSPATH for the current session with whatever the CLASSPATH value, for our example, we could do the following to dynamically set up the CLASSPATH variables and execute our program:


This sets our current directory in the classpath, and appends whatever the original classpath was.  

Now, the CLASSPATH issue is out of our way, we could just simply type:  

%  java  Test 

To run our sample java program, here comes the ubiquitous first exposure of programming:  

Hollow World!


Send mail to cscyzl@hofstra.edu with questions or comments about this web site.
Last modified: 09/16/04