I have the following Code
public static void main(String args[]) throws Exception {
ClassLoader sysClassLoader = Thread.currentThread().getContextClassLoader();
URL[] urls = ((URLClassLoader)sysClassLoader).getURLs();
String cp = "";
for(int i=0; i< urls.length; i++)
{
String ffile = (String) urls[i].getFile();
}
}
And the class compiles very well – but this is stange. If I start the class this way, I catch a null pointer:
java -cp "foo.jar" FileUtils
Exception in thread "main" java.lang.NoClassDefFoundError: FileUtils
But when I start it this way everything is fine:
java -cp "foo.jar:" FileUtils
File:/home/u/wzhkit/java/foo.jar
Why is this and how can I get around this?
Thanks,
Chris
This a bit unexpected.
I suspect that what is happening is that
javais not finding theFileUtilsclass in your JAR file. Rather I suspect that it is finding it in the current directory, because thejavacommand is interpreting an empty classpath entry to mean the current directory.This is unexpected because according to the online documentation, you should use “.” to refer to the current directory.
(Note: this is only a theory … )