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/ and uses some packages in /lib/mylib.jar
  • you need to compile the as following:
  • -d specified where to store class files.

    javac -classpath /lib/*:.  -d /tmp/classfile /user/
  • 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/
  • Following will NOT work under shell script or shell command

    • The difference between /lib/* and "/lib/*"
    javac -claspath /lib/* -d /tmp/classfile /usr/
  • 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");
  • -classpath \( \Rightarrow \) /lib/* \( \Rightarrow \) expand to

    => /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/
    # It will not work.
    javac -classpath "/lib/*:/user/MyClass/*.class" -d /tmp/classfile /user/

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 is in /myfile/
  • If package com.xfido inside the file
  • It will generate a directory com/xfido under current directory and Hello.class file is in com/xfido

    package com.xfido
    javac -d .
  • Change root directory to /tmp of package name
  • com/xfido will be generated under /tmp

    package com.xfido
    javac -d /tmp
  • It will NOT generate any dir and Hello.class will be in the current dir.


Author: cat

Created: 2019-12-05 Thu 19:11