Algorithms and OOD (CSC 207 2013F) : Labs
Summary: We explore a variety of issues in compiling and running Java from the command line.
Prerequisite Knowledge: Objects, interfaces, generics.
Fork and clone the repository at https://github.com/Grinnell-CSC207/command-line.
Scan through the files to make sure that you understand the purpose of each file.
javac is used to compile Java files.
javac to compile
What kind of file is produced?
ISort.java. Do you get anything
in addition to
ISort.class? What are the
c. You can run a Java
.class file with the
java command and the name of a class that contains
main method. Type the following command (without
the dollar sign prompt) to run our sample sorting program.
java ISort 6 1 2 9 6 0
Right now, the
.java files are in the default
package. As a good Java programmer, you know that every class should
really be in an appropriate package. So let's put these files in
a. Using your favorite text editor (Emacs, vi, vi-mangled, GEdit),
add the line
to the top of each file.
b. What happens when you try to compile
c. As you've discovered, the Java compiler can no longer find the
.java files. What do we do? Two steps,
believe it or not. First, we need to use the appropriate directory
structure. In particular, if the pacakge is
username.sorting, the code is
supposed to be in the directory
.java files to that directory.
d. The other important thing to do is to set up your
variable, which governs where Java looks for code files. Type the
For example, if the Java code is in
/home/username/Java as the directory in the command above.
e. What do you expect to have happen if you try to run
ISort? Check your answer experimentally.
f. The full name of
ISort is now
See if you can run it with that filename.
As you may recall from your work in C, we often use library files to store utility code. In Java, library files are called “jar files” or “jars”. You build a “jar” with
$jar cf File.jar classfiles
a. Create a jar file named
with the utility filename (
b. Remove those three
c. What do you expect to have happen when you try to run
ISort with the following command?
java username.sorting.ISort 10 5 1 2 4 9 1
d. Check your answer experimentally.
e. As you probably expected, Java doesn't automatically recognize
the files in the
.jar. However, we can solve that
problem: You can add the
.jar to your classpath.
Try the following command:
f. Now try to run
What do we do when we want to ship a Java program? Usually, we
put all of the files, including the class with the
method, in the same
.jar file. We then have
to specify where to find
main. We do that with
a “manifest file” that looks somethign like the following.
a. Create a file,
manifest.txt with that line.
b. Create a jar file,
ISort.jar, with the
jar cvfm ISort.jar ISort.class Sorter.class BuiltinSorter.class StandardIntegerComparator.class
c. You can run the jar file with the following command. Try it with your own set of values.
java -jar ISort.jar 5 4 1 2 3
a. What do you expect to have happen if you compile the unit test, which
you can find in
b. Check your answer experimentally.
c. As you probably discovered, the Java compiler is upset that it
doesn't know where the JUnit code is. But you can tell it.
The JUnit code is in
Add that to your
CLASSPATH and see if you can get
the test to compile.
d. Of course, you also want to run the unit tests. But where's
main method? Somewhere in JUnit. There are a variety
of ways to run unit tests. Here's one that should work.
java org.junit.runner.JUnitCore username.sorting.BISTest
Give it a try.
We've seen how to compile Java files, run the compiled files, put them into libraries, and test them. What's left? Generating documentation!
You can create Javadoc documentation with the
command. Try creating the documentation with
Right now, we're doing everything in the same directory. But that clogs the directory. Really, we should have separate diretories for our main source code, our tests, our compiled files, our Javadoc, and perhaps other things.
As you've discovered, Ant provides a way to organize all of those files. Set up an appropriate directory structure and write an Ant build file to put things in the appropriate place.
Copyright (c) 2013 Samuel A. Rebelsky.
This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.