Step 2. Compile the Source File.
The Java compiler, javac, takes your source file and translates its text into instructions that the Java Virtual Machine (Java VM) can understand. The compiler puts these instructions into a byte code file.
Now, bring up another shell window. To compile your source file, change your current directory to the directory where your file is located. For example, if your source directory is /home/smith/java, you would type the following command at the prompt and press Return:
% cd /home/smith/java
If you enter pwd at the prompt, you should see the current directory, which in this example has been changed to /home/smith/java.
If you enter ls at the prompt, you should see your file: FatCalories.java.
Now you can compile. At the prompt, type the following command and press Return: javac FatCalories.java
If you see this error message:
javac: Command not found
then Unix cannot find the Java compiler, javac.
Here's one way to tell Unix where to find javac. Suppose you installed the Java 2 Platform (J2SE) in /usr/java/jdk1.4. At the prompt, type the following command and press Return:
The compiler now has generated a Java byte code file: FatCalories.class.
At the prompt, type ls to verify the new file is there.
Step 3. Run the Program
The Java VM is implemented by a Java interpreter called java. This interpreter takes your byte code file and carries out the instructions by translating them into instructions that your computer can understand.
In the same directory, enter at the prompt:
When you run the program you need to enter two numbers when the black command line window appears. The program should then write out those two numbers plus the percentage computed by the program.
When you receive the error message:
Exception in thread "main" java.lang.NoClassDefFoundError: FatCalories
It means: java cannot find your byte code file, FatCalories.class.
What to do: One of the places java tries to find your byte code file is your current directory. For example, if your byte code file is in /home/smith/java, you should change your current directory to that by typing the following command at the prompt and hit Return:
If you enter pwd at the prompt, you should see /home/smith/java. If you enter ls at the prompt, you should see your FatCalories.java and FatCalories.class files. Now enter java FatCalories again.
If you still have problems, you might have to change your CLASSPATH variable. To see if this is necessary, try "unsetting" the classpath with the following command:
Now enter java FatCalories again. If the program works now, you'll have to change your CLASSPATH variable.