Home arrow static arrow Java Programming [Archive] - problems with Class.forName()
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - problems with Class.forName()
This topic has 7 replies on 1 page.

Posts:39
Registered: 8/11/03
problems with Class.forName()  
Jun 24, 2004 9:50 AM



 
hello everyone!

I use Class.forName() to load a class file. The problem is it's hard to locate where the class is. I use System.setProperty() to set java.class.path correctly (I've checked it) but it still throws ClassNotFoundException.

And, if I copy the class file into the same dir which holds the app's class files, everything's fine. Any one give me some advise? Thanks!
 

Posts:6,750
Registered: 1/25/04
Re: problems with Class.forName()  
Jun 24, 2004 9:56 AM (reply 1 of 7)



 
How did you check it? Is the class file in the right directory, that is in a directory structure that mirrors the package structure? Did you use the fully-qualified name including package name?
 

Posts:31,095
Registered: 4/30/99
Re: problems with Class.forName()  
Jun 24, 2004 9:58 AM (reply 2 of 7)



 
I use System.setProperty() to set java.class.path
correctly (I've checked it)

That's nice, but the JVM uses the classpath that was in effect when it started. Changing the system property has no effect on that.

Either set the classpath correctly before you start the JVM or use your own class loader (a URLClassLoader would probably do) to load classes that aren't in the classpath.

PC²
 

Posts:39
Registered: 8/11/03
Re: problems with Class.forName()  
Jun 24, 2004 10:02 AM (reply 3 of 7)



 
it's a simple app, so I used default package. the class file is H.class, and it's under E:\ (win32 platform).

I just set the classpath to E:\ and I'm sure H.class it's there.

I used

System.setProperty("java.class.path", donkeyGUI.getClasspath());

to see it's right.

H.java doesn't have any package declarations.
 

Posts:39
Registered: 8/11/03
Re: problems with Class.forName()  
Jun 24, 2004 10:05 AM (reply 4 of 7)



 
That's nice, but the JVM uses the classpath that was
in effect when it started. Changing the system
property has no effect on that.

Either set the classpath correctly before you start
the JVM or use your own class loader (a URLClassLoader
would probably do) to load classes that aren't in the
classpath.

PC�

Thanks! :-)
If a want to start a new JVM and use the new classpath, how to do it?
 

Posts:39
Registered: 8/11/03
Re: problems with Class.forName()  
Jun 24, 2004 10:08 AM (reply 5 of 7)



 
In fact I try to run H.class with a new JVM. It's an simple IDE for practise.
 

Posts:6,750
Registered: 1/25/04
Re: problems with Class.forName()  
Jun 24, 2004 10:37 AM (reply 6 of 7)



 
If a want to start a new JVM and use the new
classpath, how to do it?

Take the First Cup of Java tutorial. Secondly, don't use the default package, it will just make your life harder.
 

Posts:39
Registered: 8/11/03
Re: problems with Class.forName()  
Jun 24, 2004 10:55 AM (reply 7 of 7)



 
well, i'd like to take some look back, thank u all.
 
This topic has 7 replies on 1 page.