Compiling Java Code Using Swing 1.2

 

This document should get you up to speed compiling and running your java programs for the Person machine Communications course.


Some Basics

Java code is compiled into a platform independant form called 'bytecodes'. These bytecodes are stored in .class files, which are created, one for each of your objects, byt the java compiler. These class files are then read in and executed by the java program. Read more about java on Sun's Java site.

The original Java had objects for making buttons and user interfaces, and the collection of these objects was called the AWT (Abstract Windowing Toolkit). Since then, these objects have been revised and a new collection (or package, as they are called in javaese) called Swing has been created by Sun.


Compiling Your Code

Compiling your code is a matter of letting the javac compiler program know where it can find all the pieces (.class files) that are used by your program. There are two steps:

  1. Making sure you are importing the right packages in your source code. In the CS setup the Swing package is called: com.sun.java.swing.*. In the newer jdk, the Swing package is called javax.swing.*, so you will have to change source code written for the newer jdk to import the correct package.
  2. Let the javac compiler program know where it can find the Swing classes. On the CS linux machines (in the McConnell CS labs) the correct classpath is:
    /usr/local/pkgs/swing-1.1beta2/swing.jar

You can use the following script we have written to automatically set up your classpath and compile your code. Download the script from here, save it into the directory containing your source files, and run it as follows:

% swingjavac [options] myjavafile1.java myjavafile2.java

Where options are optional flags to the javac compiler.


Running the Code

To run the code, you have to use the java program, and tell it where all the classes to run are, not only the default and Swing classes, but also the .class files for your code that you just compiled. Basically, this just involves setting up your classpath correctly. The right classpath is:
./usr/local/pkgs/swing-1.1beta2/swing.jar

Again, we've made a handy script for you, which you can download here, and put in the same directory as your .class files to run them. Run as follows:

% swingjava [options] myclassname

Where options are flags to the java program, and myclassname is just that, the name of the class file containing your main() function.


Ms. T.A., it won't work!

Compare the following (approximately worded) error messages with the problems you are having:


Home



Please mail to Xing Yu for questions , Feb. '00