Even though you may not be using Sun’s bare Java SDK (Software Development Kit) directly, you are most likely using it under the covers.

This means that you need to understand the basic principles behind the Sun Java SDK.

Path & Classpath

Easily the most troublesome areas you’ll encounter while developing in Java are PATH and (more likely) CLASSPATH problems.

PATH

  • Set the PATH environment variable to include the directory containing the Java SDK tools (the Java SDK’s /bin subdirectory).

CLASSPATH

  • Set the CLASSPATH environment variable to tell the various Java tools where to look for user-defined classes.
  • The entries should be directories or ZIP files that contain the classes.
  • On UNIX (colon ( : ) -separated):
setenv CLASSPATH .:/home/bryan/classes:/usr/local/javatools/classes.zip
  • On Windows (semicolon ( ; ) -separated):
set CLASSPATH=.;C:\bryan\classes;D:\local\javatools\classes.zip
  • You can specify an explicit class path to any of the Java utilities:
    java, javac, javadoc, javah, and javap:
-classpath path
  • Setting up CLASSPATH is often critical to getting things to work at all!

Note that if you use a Java IDE, it often removes most of the difficulties dealing with PATH and CLASSPATH.  That alone is a good reason to use one!

Java Filetypes

Here are the most commonly-used filetypes (a.k.a. file extensions) when dealing with Java:

FiletypeDescription
.javaJava source file
.classbytecode file (“class” file)
.zipZIP files containing .class (and other) files (do not UNZIP!)
.jarJava Archive files containing .class (and other) files (do not unjar!)

Note: Remember:

  • Long filenames/filetypes are required!
  • Java is case-sensitive, including filenames!

Using the Bare SDK

If you use only the bare Sun JDK, it’s very primitive, and you don’t get a visual debugger!

It has the UNIX command line look and feel — tends to be user-hostile.

You typically will want:

  • a window for your editor
  • a window for the compiler (javac)
  • a window for the resulting program to run in.
  • maybe a window for the debugger.
  • probably one or two windows to browse the Java javadoc files

…and you are responsible for setting up PATH and CLASSPATH, and dealing with the details of each tool’s command line interface and options.  Not for the faint of heart!

Compiling a Java Source

To compile a Java source file, you type the following to invoke the bare SDK Java compiler:

javac HelloWorld.java 

(the .java filetype is required — HelloWorld.java refers to the filename)

If successful, this produces a HelloWorld.class file from the HelloWorld.java file.

The general syntax for invoking the javac compiler is:

javac [ options ] [ sourcefiles ] [ @files ]

where:

Command line parameterMeaning
optionsCommand-line options.
sourcefilesOne or more source files to be compiled (such as MyClass.java).
@filesOne or more files that list source files.

Options include (this is a subset of the full set of options;  see the Java 2 SDK Tools and Utilities documentation for details):

OptionExplanation
-classpath 
path
Sets the user class path, overriding the user class path in the CLASSPATH environment variable. 
If neither CLASSPATH or -classpath is specified, the user class path consists of the current directory.
-d directorySets the destination directory for class files. If a class is part of a package, javac puts the class file in a subdirectory reflecting the package name, creating directories as needed. For example, if you specify -d c:\myclasses and the class is called com.mypackage.MyClass, then the class file is called c:\myclasses\com\mypackage\MyClass.class
If -d is not specified, javac puts the class file in the same directory as the source file.
-deprecationShows a description of each use or override of a deprecated member or class. Without -deprecationjavac shows the names of source files that use or override deprecated members or classes.
-gGenerates all debugging information, including local variables. By default, only line number and source file information is generated.
-sourcepath 
sourcepath
Specifies the source code path to search for class or interface definitions. As with the user class path, source path entries are separated by semicolons (;) and can be directories, JAR archives, or ZIP archives. If packages are used, the local path name within the directory or archive must reflect the package name.  
Note that classes found through the classpath are subject to automatic recompilation if their sources are found.
-verboseVerbose output. This includes information about each class loaded and each source file compiled.

Running a Java Application

To run a Java Application using the Java Virtual Machine (JVM), you type something like the following, to invoke the bare SDK JVM (Java Virtual Machine, a.k.a. The Java Application Launcher):

java HelloWorld

(the .class is not allowed — HelloWorld refers to the classname;  the JVM tries to find the specified class in the classpath.)

This runs the HelloWorld class.

The syntax for invoking the java application launcher (JVM) is:

java [ options ] class [ argument ... ] 
java [ options ] -jar jarfile [ argument ... ] 
javaw [ options ] class [ argument ... ] 
javaw [ options ] -jar jarfile [ argument ... ]

where:

Command line parameterMeaning
optionsCommand-line options.
className of the class to be invoked.
jarfileName of the jar file to be invoked.  Used only with the -jar option.
argumentArgument passed to the main function.

Options include (this is a subset of the full set of options;  see the Java 2 SDK Tools and Utilities documentation for details):

OptionExplanation
-classpath 
classpath
-cp

classpath
Specify a list of directories, JAR archives, and ZIP archives to search for class files. Class path entries are separated by semicolons (;). Specifying -classpath or -cp overrides any setting of the CLASSPATH environment variable.  
If -classpath and -cp are not used and CLASSPATH is not set, the user class path consists of the current directory (.).
-Dproperty
=value
Sets a system property value. (Multiple such options may be specified.)
-verboseDisplay information about each class loaded.
-versionDisplay version information and exit.
-?
-help
Display usage information and exit.

Running a Java Applet

To run a Java Applet from the bare SDK, type something like the following:

appletviewer myHTMLfile.html

This reads or downloads an HTML document. It reads or downloads all the applets referenced in the document, and displays them, each in their own window. If the HTML document has no <APPLET> tag, it does nothing.

The general form of the command is:

appletviewer url|file...

Applets run in this way may be controlled by menu-driven commands supplied in the appletviewer’s window.