Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 476
  • Last Modified:

ClassPath problem

Hello, I am having trouble with a java application. The following code generates a ClassNotFoundException:

Class c = Class.forName(class_name);


The problem is that I do not have the classpath set correctly. However, i need to set this classpath dynamically based on input from an eclipse program.

How can I set the classpath in java code?
0
jameswalt
Asked:
jameswalt
  • 9
  • 6
  • 5
  • +2
2 Solutions
 
sciuriwareCommented:
You don't have to; for instance you could load the class from the very right directory.

;JOOP!
0
 
CEHJCommented:
You need to modify your approach to classloading if you don't have the classpath set:

http://javaalmanac.com/egs/java.lang/LoadClass.html
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mayank SAssociate Director - Product EngineeringCommented:
>> i need to set this classpath dynamically based on input from an eclipse program.

Meaning? Eclipse uses its own class-path than the system class-path. It depends whether you are running the class inside Eclipse or outside (where are you compiling/ running it from?).

See this for a brush-up of class-path:

http://www.mindprod.com/jgloss/classpath.html
0
 
jameswaltAuthor Commented:
"You don't have to; for instance you could load the class from the very right directory."

Could you provide a quick snippet of an example, to make sure we're on the same page.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Code remains the same as posted on the links above. You can load the class if it is in the same directory where the application is running from (if you have no class-path) is what sciuriware meant, probably?
0
 
jameswaltAuthor Commented:
The problem is, the classes can't be in the same directory. They could be in any directory, as specified by the user. Is there any way to specify the full path to the classes during runtime?
0
 
CEHJCommented:
The full path should be supplied by the user, as you say. Use the example link i posted
0
 
jameswaltAuthor Commented:
The second problem here is that my code isn't loading the class, another imported set of code is. The code requires that the "classpath is set blah blah" before things work...

Although your link is very helpful, I can't control this part of things (i wish i could). Any other ideas anyone?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> They could be in any directory, as specified by the user.

Why do you need a directory path for loading the class? You should probably just make sure they are in the class-path. See the link I posted. Or do you want to load a remote class (on some other machine/ URL)?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Tell us what is the (full-)name of the class and the directory where it is located, and we can help set up the class-path.
0
 
gauravkrtomarCommented:
U can try using:-
String classPath = System.getProperty("java.class.path",".");
System.out.println(classPath);
classPath=classPath.concat(";d:\\");
System.setProperty("java.class.path",classPath);
classPath = System.getProperty("java.class.path",".");
System.out.println(classPath);

Not sure that the jvm ClassLoader works with dynamically changing the files.
0
 
jameswaltAuthor Commented:
Basically the code must be able to locate, modify, compile, and run the java classes in the directory that the developer specifies during runtime. The code can do this from a standalone implementation, but I'm converting it into an eclipse plugin.

The directory is on Windows (or Unix too), something like

C:/Documents and Settings/eclipse_runtime/Project_name/class_directory_name
(or ANYWHERE the developer wants it to be)

the url can have spaces, stupid windows. it really depends on the developer's workspace settings and directory structure. The developer will provide the directory to me during runtime.

I don't know if that last suggestion will work, but I'm certainly happy to try.
0
 
CEHJCommented:
>>I don't know if that last suggestion will work

It won't i'm afraid
0
 
jameswaltAuthor Commented:
I suppose I could try to launch the file by instantiating a new jvm, but that really feels like I'm going out of my way to do it.. Let me play around and get back to you guys - thank you so much for your help.
0
 
CEHJCommented:
What problem are you having implementing it per the Sun example i posted?
0
 
jameswaltAuthor Commented:
>> What problem are you having implementing it per the Sun example i posted?

I am not able to do the classloading myself. The code we're importing is actually loading the classes, and I cannot modify this code.
0
 
jameswaltAuthor Commented:
I am running the code from a launch configuration inside eclipse. My code is the LaunchConfigurationDelegate and a number of other classes.
0
 
jameswaltAuthor Commented:
I'm getting this exception when trying the URL loader example (the class name of the test file is Calculator:

java.lang.ClassNotFoundException: Calculator
      at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
      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 mujava.MutationSystem.getClassTypeFromURL(MutationSystem.java:162)
        ....

getClassTypeFromURL is a method derived from CEHJ's URLClassLoader link. I specify the directory where the classes can be found from a new File(directoryName).toURL() call
0
 
Mayank SAssociate Director - Product EngineeringCommented:
It needs to be present in Eclipse's class-path, I guess.
0
 
CEHJCommented:
>>from a new File(directoryName).toURL() call

That wouldn't work. It would have to be a path to a class, not a directory
0
 
jameswaltAuthor Commented:
CEHJ, after a little tweaking I got it to work. The directory did actually work .. but the code sample they provided wasn't correct for my needs.

I just followed the URLClassLoader API and made a few minor changes, you guys were all very helpful.
0
 
CEHJCommented:
:-)

>>The directory did actually work ..

Sorry about that
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 6
  • 5
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now