Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to set classpath in a program?

0 views
Skip to first unread message

Lion-O

unread,
May 30, 2006, 6:35:18 PM5/30/06
to
I'm trying to grok reflection (java.lang.reflection) and so far I've come a
long way. After reading up on Class.forName() I wrote a test program which went
over the class files in the current directory to check and print their
properties. My first version works like a charm, obviously the next step is to
allow specifying the directory to look in.

And this is where I can't get my fingers behind whats happening... Here's the
snipplet of my code:

---[ GetClass.java snipplet ]---

public class GetClass {

private static String nametoClass(String file) {
return new File(file).getName().replace(".class", "");
}

public static void main(String[] args) {

Class c;
File directory;
String[] classes; // Array filled with names of classfiles + path
Constructor[] cnsts;
...
if (args.length > 0) {
directory = new File(args[0]);
String cp = System.getProperty("java.class.path");
if (cp.equals(null)) {
System.setProperty("java.class.path", ".:" + directory);
} else System.setProperty("java.class.path", cp + ":" + directory);
}
...
try {
for (int i = 0; i < classes.length; i++) {
c = Class.forName(nametoClass(classes[i]));
System.out.print("Class " + c.getName() + " is part of package ");
if (c.getPackage() == null) System.out.print("default");
else System.out.print(c.getPackage().toString());
System.out.println(" and contains:");
cnsts = c.getConstructors();
System.out.print("Constructor(s): ");
...
}
} catch catch(ClassNotFoundException cnfe) {
System.err.println("Error: Expected class was not found.\n");
}
}

---[ EOT ]---

Now... When I run this program to look into a javabot subdirectory (containing
a file 'javabot.class' which is part of a bot written using the PircBot API) I
get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: \
org/jibble/pircbot/IrcException

at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:164)

This is quite logical since 'Class.forName()' is basicly equal to using
"Class.forName(className, true, currentLoader)" where 'true' means that the
class should be initialized, which basicly means that it will also try to
determine the classes used (imported / extended) in the classes which I'm
checking.

So I tried turning this behaviour off by changing the above forName() line
into "c = Class.forName(nametoClass(classes[i]),false,null);".

This results in a ClassNotFoundException as can be expected from the method
(thats why it should be caught ;-)). But why can't it find this class even
though I have changed the classpath? The reason this confuses me is because it
can obviously find the class in my first version.

Any hints would be appreciated.

--
Groetjes, Peter

.\\ PGP/GPG key: http://www.catslair.org/pubkey.asc

Dale King

unread,
May 30, 2006, 7:11:22 PM5/30/06
to

It isn't the classpath you would be dealing with after the program runs.
You need to create a classloader instance to read from where you want to
look for classes. The classpath is just used to set up the initial class
loaders and after that it isn't used anymore.

See URLClassLoader and give it some URL's pointing to the directories
you want to look at (note from the documentation you need to include a
trailing slash).

You can also use addURL to add URLs, but note there is no removeURL.

--
Dale King

Lion-O

unread,
May 31, 2006, 4:16:36 PM5/31/06
to
> It isn't the classpath you would be dealing with after the program runs. You
> need to create a classloader instance to read from where you want to look for
> classes.

Right, so my earlier assumptions that the classpath was involved were true. But
I overlooked that its not possible to alter the search path for the bootstrap
class loader.

> See URLClassLoader and give it some URL's pointing to the directories you
> want to look at (note from the documentation you need to include a trailing
> slash).

Thanks, this is indeed the solution but it seems I still have some reading to
do since this doesn't get rid of the error I mentioned earlier.

However, now it doesn't matter anymore if I run the program in the directory
itself or simply specify it as parameter. The error (ClassDefNotFound) is still
there either way, but that used to be 'ClassNotFound' whenever I specified a
directory on the commandline. So success indeed since the last error means it
couldn't find the class alltogether.

I've also discovered how I might overcome the 'ClassDefNotFound', but thats a
little too much over the top for me at this point. The solution would be
writing a class which extends ClassLoader in order to re-define certain methods
so that it can either ignore this or try to overcome it. But for now I'm
happy.

For the other people reading this thread here's how I solved the issue:

---[ Snipplet of GetClass' main method ]---

import java.net.URL;
import java.net.URLClassLoader;
import java.net.MalformedURLException;
...
URL url = null;
...


for (int i = 0; i < classes.length; i++) {

try { url = new URL("file://" + directory.getAbsolutePath() + "/"); }
catch (MalformedURLException mue) {
System.err.println("Error: wrong protocol used in URL.");
System.exit(1);
}
URL[] durl = {url};
URLClassLoader cl = new URLClassLoader(durl);
c = Class.forName(nametoClass(classes[i]),false,cl);


System.out.print("Class " + c.getName() + " is part of package ");

...

---[ EOT ]---

Now I'm going to change the program so that I can catch a 'ClassDef' or
'ClassNotFound' exception on a per-class level so that I can present a nice
error message untill I feel like writing up my own classloader.

Thanks again for the hint.

0 new messages