Question

What is the convention that is followed for compiling and executing Java apps from the shell? As a specific use case, I'm teaching some kids how to read Java source and picked minicraft (which is a commented version of Notch's Ludum Dare 22 entry). This is pretty exciting for them, but I've had my feet out of the Java realm for about 10 years and frankly I'd like to simultaneously teach them how to use the shell and less robust (ie, non IDE) editors before jumping into Eclipse and such. I'd like to understand:

A) What shell command would you execute to compile and run the app (and from which project folder; ie, miniventure or miniventure/src or miniventure/src/com/mojang/ld22) and

B) what is the rationale to follow in knowing how to craft projects and the shell java and javac args for future reference when digging into other projects from src such as this?

Était-ce utile?

La solution

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:

  • source which includes source file debugging information
  • lines which includes line number within the source file debugging information, and
  • vars which includes local variable debugging information
  • 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 -

    Licencié sous: CC-BY-SA avec attribution
    Non affilié à StackOverflow
    scroll top