Given the constraint to only work within a command shell, the best way to handle compiling is to run javac from the root of your classpath, right at the directory that contains whatever the top level package you have defined for your source code (looks like it is probably miniventure/src
in your case). You can just run javac and use the -cp
(short for classpath) command line argument to define the root of your classpath. Since you are teaching new programmers, you could use the long form of the same option, -classpath
to reinforce that they are setting a Java classpath.
If you run javac from the root of your classpath, it's customary to just use a dot to reference that same directory: javac -cp .
; because javac will traverse the directory (package) structure to find all source files (anything that has the .java filename extension), but the docs for javac recommend a more structured approach:
There are two ways to pass source code file names to javac:
- For a small number of source files, simply list the file names on the command line.
- For a large number of source files, list the file names in a file, separated by blanks or line breaks. Then use the list file name on the javac command line, preceded by an @ character.
If you run javac this way, it will place the class files next to the source files, but then the class files are all mixed in with the source files and may be scattered through the directory/package structure. The customary way to handle this is through the use of the javac -d
command line argument, which defines the directory where compiled classes should be written; javac will preserve the package structure and create whatever directory structure is necessary during compilation. The standard way to define this option always was a directory named: classes
that is a sibling to the source (or src
in your case) directory. So you would create a new directory: miniventure/classes
and then your command line would include: -d ./classes
.
Since you're teaching new programmers, you will want to include debugging information during the compile, so when they run into errors or exceptions, the stack trace will include detailed debugging information. The javac command line option for this is: -g
which will generate the class files with all debugging options turned on; this will include the following debugging options:
And finally, since it may be helpful to let the new programmers see everything in detail, you may want to include the -verbose
option, which tells javac to output very detailed messages, including each class that is loaded and each source code file that is compiled.
So, bringing that all together, your javac command line would look like:
javac -classpath . -d ./classes -g -verbose
With the understanding that I have not included the option to list each source code file on the command line or used the @ argument to reference a file that contains a list of the filenames. I personally have never used that option and it feels a little overly complicated I guess. Hope this is helpful -