Table of Contents
1 Compile java source code with classpath
- If you are Java developer, you will waste a lots of hell time on classpath hell.
- Class files are not found.
- A package is not found.
- jar files are not found.
- Assume you have a java file called
/user/myfile.java
and myfile.java uses some packages in/lib/mylib.jar
- you need to compile the myfile.java as following:
-d specified where to store class files.
javac -classpath /lib/*:. -d /tmp/classfile /user/myfile.java
- jar file is in
/lib
and class file myfile.class will be in /tmp/classfile The following will work with double quotes
javac -classpath "/lib/*" -d /tmp/classfile /user/myfile.java
Following will NOT work under shell script or shell command
- The difference between
/lib/*
and"/lib/*"
javac -claspath /lib/* -d /tmp/classfile /usr/myfile.java
- The difference between
Show all the classpath at runtime, it is very useful.
- Check whether you include all the jar files.
String path = System.getProperty("java.class.path"); System.out.println(path);
-classpath
\( \Rightarrow \)/lib/*
\( \Rightarrow \) expand to/lib/* => /lib/myjar.jar:/lib/mylib.jar # MacOS or Linux => /lib/myjar.jar;/lib/mylib.jar # Windows
If your java file use jar and class file as a library, your class file called *MyClass.class" in
/user/MyClass/MyClass.class
Here is the javac compiler command line- The following command will search jar file under
/lib" and search *class file* under ~/user/MyClass
# It works. javac -classpath "/lib/*:/user/MyClass" -d /tmp/classfile /user/myfile.java # It will not work. javac -classpath "/lib/*:/user/MyClass/*.class" -d /tmp/classfile /user/myfile.java
- The following command will search jar file under
2 Javadoc command line Javadoc exmaple
# it works javadoc -classpath "/myjar/*:/user/MyClass" -d /htmlfile -sourcepath $b/javalib *.java # it works javadoc -classpath $b/javalib/jar/*:. -noqualifier all -d /Library/WebServer/Documents/zsurface/htmljavadoc -sourcepath $b/javalib *.java
- The following will NOT work, the difference is:
$b/javalib/*.java
\( \Rightarrow \)$b/javalib *.java
javadoc -classpath "/myjar/*:/user/MyClass" -d /htmlfile -sourcepath $b/javalib/*.java
3 Java generate directory from package name and without package name.
- Assume Hello.java is in /myfile/Hello.java
- If package com.xfido inside the Hello.java file
It will generate a directory
com/xfido
under current directory and Hello.class file is incom/xfido
package com.xfido javac -d . Hello.java
- Change root directory to
/tmp
of package name com/xfido
will be generated under/tmp
package com.xfido javac -d /tmp Hello.java
It will NOT generate any dir and Hello.class will be in the current dir.
javac Hello.java