6/11/2006

New Options in Javac 1.5/5: -classpath==-cp

4 new options were added to Javac in JDK 1.5/5:

-cp<path>    Specify where to find user class files
-version Version information
-X Print a synopsis of nonstandard options
-J<flag> Pass <flag> directly to the runtime system
I didn't notice them until recently, more than 2 years after Tiger was first released. The official Javac 1.5 docs page doesn't mention -cp and -version, either. I stumbled upon them when I looked at the output from javac -help.

It seems -cp was added in response to bug 4626998 (RFE) Allow "-cp" option to javac to set classpath.

What is the difference between -cp and -classpath options? Prior to JDK 1.5, -cp can be used on java command, but not on javac, whereas -classpath can be used with both java and javac. I tested Javac 1.4.2 with -cp option on both Windows and Solaris, and it's not recognized. Starting from JDK 1.5, -cp and -classpath are synonymous, and I confirmed that on Windows and Solaris.

There is no -version option in javac from 1.0 to 1.4.2. That's a fact I find hard to believe. To get version info from pre-5.0 javac, you will need to -J-version option to get the underlying JVM version, assuming the two versions are the same:
/usr/jdk/142/bin/javac -J-version Test.java
java version "1.4.2_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_06-b03)
Java HotSpot(TM) Client VM (build 1.4.2_06-b03, mixed mode)
You can use the same trick in JDK 1.5, but the new -version option is better:
C:\tools\jdk5\bin\javac -version -cp %JAVAEE_HOME%\lib\javaee.jar HelloWorldServlet.java
-J is not really a new option. It was just not listed by javac -help prior to 1.5. This option allows you to pass flags/options to the underlying JVM. Note it's not a standard javac option. For example,
javac -J-Xms512m -J-Xmx512m -J-showversion -classpath lib/foo.jar -d build/classes Test.java