URLClassLoader interprets path as package

Hi,

I have to load a class from another directory. This class is in no package! I specify a path for the URLClassLoader but the compiler interprets this as package for the class and of course complains that the package is wrong.

the code:

publicclass Main{

publicstaticvoid main(String[] cmdArgs){

// load scanner class

URL[] urls ={new File("/home/bace/extern/").toURI().toURL()};

ClassLoader urlClassLoader =new URLClassLoader(urls);

Class scannerClass = urlClassLoader.loadClass("xyLexer");

// get scanner constructor

Class[] argTypes =new Class[1];

argTypes[0] = CharStream.class;

Constructor scannerConstructor = scannerClass.getConstructor(argTypes);

// create scanner object by constructor

Object[] args =new Object[1];

args[0] =new FileStream("/home/bace/extern/input.txt");

Object scannerObj = scannerConstructor.newInstance(args);

// create token stream from scanner

TokenStream ts =new CommonTokenStream((Lexer)scannerObj);

}

}

I got the following error:

Exception in thread"main" java.lang.NoClassDefFoundError:

xyLexer (wrong name: extern/xyLexer)

at java.lang.ClassLoader.defineClass1(Native Method)

at java.lang.ClassLoader.defineClass(ClassLoader.java:620)

at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)

at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)

at java.net.URLClassLoader.access$000(URLClassLoader.java:56)

at java.net.URLClassLoader$1.run(URLClassLoader.java:195)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(URLClassLoader.java:188)

at java.lang.ClassLoader.loadClass(ClassLoader.java:306)

at java.lang.ClassLoader.loadClass(ClassLoader.java:251)

at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Class.java:247)

at Main.main(Main.java:33)

What can I do that the whole path is interpreted as a path, and not as a package structure (after "/home/bace/")?

Thanks for any help in advance!!! Best, Markus.

[3007 byte] By [MarkusKuhlaa] at [2007-11-27 7:36:27]
# 1
put it in a package, then
georgemca at 2007-7-12 19:16:58 > top of Java-index,Core,Core APIs...
# 2

I think what you need to do is this:URL[] urls = {new File("/home/bace/").toURI().toURL()};

ClassLoader urlClassLoader = new URLClassLoader(urls);

Class scannerClass = urlClassLoader.loadClass("extern.xyLexer");

DrClapa at 2007-7-12 19:16:58 > top of Java-index,Core,Core APIs...
# 3

Hi,

thanks for your replies!

I'd like to load the generated class without changing its content (it's generated without any package statement.

Is there any possibility to load a class (without any packages) from an arbitrary directory?

DrClap, for your proposal I also have to change the class to the package extern :-(

Regards!

MarkusKuhlaa at 2007-7-12 19:16:58 > top of Java-index,Core,Core APIs...
# 4

> DrClap, for your proposal I also have to change the

> class to the package extern :-(

If I understand the error message, the class is already in that package (despite what you said about it not being in a package). Also, if the class is not in a package then the code you posted should not have produced that message (despite what you say).

So something doesn't add up correctly. Did you try the code I posted? If so, what was the result?

DrClapa at 2007-7-12 19:16:58 > top of Java-index,Core,Core APIs...
# 5

Hi,

as I posted first: The class contains NO package declaration, period. I also didn't understand why the compiler assumes that the path after /home/bace/ could be the package path in the file system. That's the reason of the post ;)

When I tried loadClass("extern.xyLexer"); then the compiler of course expects the package declaration in the xyLexer class. So it didn't work.

Best!

MarkusKuhlaa at 2007-7-12 19:16:58 > top of Java-index,Core,Core APIs...
# 6
In any case you should put the class into a package. The null package is not recommended for serious use, just for toys and tests.
ejpa at 2007-7-12 19:16:58 > top of Java-index,Core,Core APIs...
# 7
What a bummer ;) but seems that I'll have to do this.Thanks all !
MarkusKuhlaa at 2007-7-12 19:16:58 > top of Java-index,Core,Core APIs...