Java Tools

The Java SDK contains a number of tools that are executed from the command line. If an IDE is being used, then many of these tools will be incorporated within the IDE. We have already encountered javac and java; we will describe these in a little more detail here. Other tools such as javadoc and jar will be below in the site.

1.2.1 javac

javac compiles Java source code and produces bytecode class files. The source code file must have a .java suffix; the resulting class files have a .class suffix. The file must contain one public class with the class name the same as the file name. Other nonpublic classes can be included in the same file. We can include more than one source file in a single command, for example

javac Class1.java Class2.java

We can also group several source files in a single command file. For example, Myfiles could contain
Class1.java
Class2.java
Class3.java

These can all be compiled with the command

javac @Myfiles
A program will usually have references to other classes. javac will search for a corresponding class file. If a class file is found, but no corresponding source file, javac will use the class file. If a corresponding source file is found, but no class file, javac will compile the source file and use the resulting class file. If both source file and class file are found, the class file is used unless the source file is more recent than the class file, in which case the source file is recompiled and the resulting class file is used. javac has a number of associated options: we will cover just a few.

-d destination

This sets the destination directory for the resulting class file. The default is to place the class file in the same directory as the source file.

-verbose

This outputs details about each class loaded and each source file compiled.
javac, as well as java, also have a classpath option. We will discuss this option in later

1.2.2 Java

The java interpreter runs Java applications. It loads the application's class file and invokes the specified class's main method, which must be public, void, and static. There are a number of options for java, including the verbose option that we have seen for the javac compiler. The format of the command is
java [options] classname [program parameters]
Note you do not include the .class suffix in the class name.

No comments:

Post a Comment