Wednesday, October 13, 2010

Classpath Gotchas

Source :

  • If you use the -jar option, java.exe ignores the classpath. It will only look in that jar!! Try using the manifest Class-Path instead to get Java to look in auxiliary jars.
  • The elements of the classpath are platform specific OS directory names (with \ or / separators) or jar file names. The package/classnames on the command line are not; they are fully-qualified dotted names. Don’t try to concoct a chimera class name such as C:/com.mindprod.mypackage.MyClass
  • If you want a jar on the classpath, you must put the jar file name on the classpath, not just the name of the directory where the jar lives. This means you need whacking long classpaths, with every jar in a common directory each added to the classpath individually. Perhaps one day Sun will implement a JARPATH. In would not be hard to build a wrapper around javac.exe and Java.exe that faked in a JARPATH. In the meantime you

    rem how to fake jarpath
    rem separate jarnames with File.pathSeparatorChar, i.e. ; in Windows : in Unix
    java.exe -Djava.ext.dirs=usualextdir;anotherdir ...
    To add to the usual extdir. In Java version 1.6 you can use a wildcard to include all jars in a directory.
    rem use of wildcard in classpath to add all jars in C:\jardir and E:\morejars
    java.exe -classpath C:\jardir\*;E:\morejars\*
  • Beware of inserting extra semicolons (in general, FilepathSeparatorChar chars) in your CLASSPATH. Everything to the right will be ignored!, leading you to pull out your remaining hair in frustration wondering why java.exe can’t find the classes. You must have exactly one semicolon between elements, no lead/trail/extras.
  • Beware. Putting D: on the classpath is not the same thing as putting D:\. The first put the current directory of D: on the classpath. The second puts the root directory. They are often the same, but not always

No comments:

Post a Comment