Java Tools for Development and Testing:
javac and java
The Java compiler is named javac and is typically invoked from the command line with a command similar to
javac MyClass.javain which the file MyClass.java will contain Java source code, including a public class which must also called MyClass. Note that when compiling with javac, the file extension of the source code file (.java) must be supplied.
If all goes well (i.e., if there are no compile-time errors), this command will produce a file of byte codes called MyClass.class, which can then be interpreted by the java interpreter, a process which results in the "running" of your program.
If we have a file of Java byte codes, MyClass.class say, and that file represents a complete program (or is the "invoking class", or "starter class", for a complete program), then we can run this program using the Java byte code interpreter (also called, more simply, just the Java interpreter, as well as the Java Virtual Machine or JVM). This name of this interpreter is java, and is invoked as illustrated by this command:
java MyClassNote that when interpreting with java, the file extension of the byte code file (.class) must be omitted.
When compiling a program, the javac compiler must be able to locate the classes it needs, and it uses a special object called a class loader to do this. The class loader begins by searching the standard Java classes that are bundled with whatever version of the Java SDK (Software Development Kit) you are using. Then it searhes any optional packages that have been provided via Java's extension mechanism.
If the class sought is not found in the standard Java classes or in the extension classes, the class loader begins to search the classpath. The classpath is a list of locations at which classes may be stored. By default the classpath consists simply of the current directory, but this may be changed. For example, the classpath to be used on a particular occasion can be supplied as a command-line option, as in
javac -classpath mypath MyClass.javaor the environment variable CLASSPATH can be set to a particular classpath if that classpath is to be used repeatedly for some time to come.
Unfortunately, the syntax of a multi-part classpath is platform-dependent. For example, under Unix, Linux, and the Mac OS X, the parts are colon-separated, as shown in this example:
javac -classpath myfiles:test:current MyClass.javaOn the other hand, under Windows they are semicolon-separated, as shown in this (analogous) example:
javac -classpath myfiles;test;current MyClass.java
The java interpreter uses the same procedure to locate classes for executing a program that the javac compiler uses when locating classes to compile the program.
The value of the classpath given as a command-line option overrides the value as given by CLASSPATH, if both are present.