Compiling and Launching
From Suhrid.net Wiki
Jump to navigationJump to searchjavac
- javac by default places the generated class files in the same directory as the source files.
- use the javac -d option to place the class files in some other directory.
- if the directory does not exist, there will be an error.
- the directories for the package structure will however be automatically created by javac.
There are four things to keep in mind while compiling:
- Which are the files that need to be compiled ?
- The directory where javac looks for other .java source files that are used by the file to be compiled.
- The directory where javac looks for class files that are used by the file to be compiled.
- The directory to place the generated class files in.
Options:
- the -sourcepath is the directory where the compiler looks for other java files.
- It has to be the directory that contains the hierarchy of the package structure of the source files.
for e.g. if we are compiling ClassB.java in directory src/net/suhrid/pkgB/ClassB.java which uses ClassA located in net/suhrid/pkgA/pkgA/ClassA.java then argument to sourcepath must be the "src" directory: javac -sourcepath src net/suhrid/pkgB/ClassB.java
- this option will also compile the files which are referenced by the file being compiled.
- -classpath and -cp can be either of the arguments to instruct the compiler to look for classes used by the compiled file.
- Typically used for pre-compiled third party classes/libraries.
- In case of directories, the directory which contains the hierarchy of the package structure of the class files. (similar to sourcepath)
- In case of jar files, the argument is the location of the jar file - till the file name. Here the jar file acts as the directory which contains the hierarchy of the package structure of the third party class files.
- Note multiple directories can be specified in the -sourcepath and the -classpath options (separated by ":" in unix and ";" in Windows). Typically the sourcepath will be a single directory whereas the classpath will have multiples directories or jar files for various third party libraries.