Thank you for visiting Blazing Games

Using javac

While many readers may be planning on using one of the many Integrated Development Environments (IDEs) that are available for Java development, it is still useful to be familiar with the command line tools. Being able to compile java from the command line is useful if you are ever in a situation where you don't have access to your IDE. The command line compiler is also very vital to consultants and independent contractors, as many companies want to be able to independently build a project. By having the command line compiler, all that the consultant needs to do is write a very simple batch file or shell script that executes the compiler and include that as part of the source code. Doing that is probably far more useful to a company than including an IDE's project file if the company isn't using that IDE, and certainly more affordable than having to buy the IDE that the company you wish to do work for uses.

The javac tool is the command line compiler. It is used to compile java source files (which are text files that have a .java extension) into class files which can be ran as java applications or applets. Javac is a very simple tool to use. The basic format is javac source_file.java , which compiles the indicated file. Multiple files can be compiled at the same time by using wild card characters. If you have a large number of files that you wish to compile, you can use the @filelist option. This option reads the file named after the @ symbol and processes each line as a separate file to compile.

To better explain this feature, lets say we were compiling three separate files, each in their own directory. Lets say the files were one/One.java, two/Two.java and three/Three.java. The first thing we would have to do is create the list file, which we will call filelist.list. This file would consist of the following lines:

one/One.java
two/Two.java
three/Three.java

To then compile all three files we would simply use the command javac @filelist.list . This would compile all three of the indicated files as well as any dependencies.

As different people like to keep files in different places, the javac compiler lets you specify three different types of paths. You can specify where the source files path is by using the -sourcepath path switch. You specify the destination path with the -d path switch. And finally, if dependent class files are located elsewhere you can use the -classpath path switch.

Now, some practical examples. In chapter 3 I will be introducing a sample program named Chapter3. If you were to run javac in the root of the source directory, you would use the following command to compile chapter 3's application example:
javac com/blazinggames/jgp/chapter3/Chapter3.java

Applets are just as easy to compile. To compile the sample applet from chapter 4, for instance, we would simply use the following:
javac com/blazinggames/jgp/chapter4/Chapter4.java

Previous Chapter 2 Contents Page 5 of 7 Book Contents Next

About - Privacy Policy - Contact - Links - FAQ
Copyright © 2004-2006 Blazing Games Inc. All Rights Reserved