|
Creating and Running
Java Programs
in Unix
|
General Steps to Creating, Compiling and Running Java Programs
Applets and Applications in Unix
I. Applications
- Open a Unix window.
- Now, change to the directory of your choice from within the window.
For example:
- Using the editor of your choice,
create some Java source code
For example, suppose we want a Java class named "Test",
then we would start vi, emacs, pico,
or whatever editor you use
with the name of the Java file:
or
or
- The source code, Test.java, might look like this:
public class Test
{
public static void main(String args[])
{
System.out.println("We work!");
}
}
|
Don't forget to save the file!
- Double check the name of the Java program file you just saved
by listing out the files in the current directory:
- If the name of the Java program file listed does not end in
.java,
you need to rename or move (mv)
it so that it does
For instance, if the file appears as A.java.txt, type
prompt% mv A.java.txt A.java
|
Now if you list the files in the directory, you should see it named correctly
- Assuming we saved the Java source code (as in step 4) from within our
editor,
we can now go back to the command prompt and compile it.
This is done with the javac compiler command:
- If it doesn't compile, go back to the
editor and edit the A.java file
to remove the error.
Then repeat step 7 after saving the changes.
You may have to do this several times
before there are no more compile errors.
- When it compiles fine
(we know this by the fact we had no error messages
and the "Test.class" file exists),
we can run the application (found in Test.class)
using the java command
followed by the name of the class (without the extension):
- At this point you should see:
II. Applets
- Open a Unix window.
- Now, change to the directory of your choice from within the window.
For example:
- Using the editor of your choice,
create some Java source code.
For example, suppose we want a class named "A",
then we would start vi, emacs, pico, or whatever editor
with the name of the Java file:
or
or
- The source code, A.java, might look like this:
// This program draws a tall rectangle via the A.html applet tag.
// mlc
// 9/1999
import java.applet.*;
import java.awt.*;
public class A extends Applet
{
private int w, h;
public void init( )
{
w = 45;
h = 50;
}
public void paint(Graphics g)
{
g.drawRect(w, h, 20, 80);
}
}
|
Don't forget to save the file!
- Double check the name of the Java program file you just saved
by listing out the files in the current directory:
- If the name of the Java program file listed does not end in
.java,
you need to rename or move (mv)
it so that it does
For instance, if the file appears as A.java.txt, type
prompt% mv A.java.txt A.java
|
Now if you list the files in the directory, you should see it named correctly
- Now, since this is an applet, we'll need to make a HTML file.
We could call it A.html and create it
from the command line using your favorite editor again:
or
or
- The A.html file might look like this:
<html>
<p> This file launches the 'A' applet: A.class! </p>
<applet code="A.class" height=200 width=320>
No Java?!
</applet>
</html>
|
-
Again, be sure to save the file
Also be sure that the name of the file ends in .html
(not .txt)
- Assuming we saved the Java source code (as in step 4) from within your
editor,
we can now go back to the command prompt and compile it.
This is done with the javac compiler command:
- If there were compile errors, go back to the
editor and edit the A.java file
to remove the error.
Then repeat step 10 after saving the changes.
You may need to do this several times
before there are no more compile errors.
- If everything compiles fine
(we know this by the fact we had no error messages
and the "A.class" file exists),
we are ready to run it.
- At this point (assuming we saved the HTML file in step 5),
we can run either a Java-enabled browser or
simply use the appletviewer program from the command line:
prompt% appletviewer A.html
|
- The above command should bring up a window using the HTML file and
then launch (or run) the Java applet from within the viewer.
This file launches the 'A' applet: A.class!
|
You should see a tall rectangle. Good work!
A Possible Java J2SE Problem -- No Path to Java J2SE
If the system can't find javac,
java or appletviewer,
you should check your path by typing "$PATH"
at the command prompt:
It should return something like this:
/usr/openwin/bin:/usr/bin:/usr/ucb:/usr/local/bin:/usr/local/java/bin
|
If there is no java/bin in the path,
you won't have direct access to the Java commands.
You should check with your systems person.
They should consult the JDK or J2SE README file
and the location of where the Java J2SE was installed
to assure the correct path.
Another Possible Java J2SE Problem -- No Path to Java J2SE Standard Classes
If the system can't find any of the standard Java J2SE
classes, like java.text.*,
java.io.* or java.awt.*,
you should check your class path by typing "$CLASSPATH"
at the command prompt:
It should return something like this:
.:/usr/local/java/classes:/usr/local/java/lib/classes:/classes
|
This tells the Java compiler where to find possibly needed class files
in the system.
You should check with your systems person, if this has not been set.
They should consult the JDK or J2SE README file
and the location of where the Java J2SE was installed
to assure the correct path.
Back to Help Page
Back to CSU Home Page
Copyright © 1999-2003: Colorado State University for CS Department.
All rights reserved.