Building Java projects from the CLI – Part 2

In this posts we will use the java cli with multiple packages. This takes a few more steps than the previous posts but its not hard to do either. We will use the same project as in the previous posts. Our project directory will now look like below to include our new utility package and Math class.

project structure

Here is the new code in the Math class and the new code in the ClasspathDemo class.

Math class

ClasspathDemo class

ClasspathDemo now has a dependency on the Math class so that class better be available.


With this new setup we now need to compile the source code in the classpathtutorial package and utility package. Navigate back the root of the classpath-demo-pt2 directory and run this command.

This command uses the sourcepath option of the javac command to tell us where all the source files are. More specifically it says start at the com directory and look two directories down and compile all .java files you find, then put them in the classes directory.  Now we can find all our compiled source files in the classes directory in their respective packages.

java project structure 2



Navigate to the root of the classes directory and run the following command. The output should be “Hello World” and “3”!

That concludes part 2 of our Java CLI overview.

In part 3 we will finish up by adding a third party jar to our project and utilizing it. This is super helpful because we want to be able to use the great code that other developers have written!


Add a Comment

Your email address will not be published. Required fields are marked *