Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

hi i have a code that's loads a class file into my program. when i run/debug it from eclipse i always chose the class file from my bin directory. when i compiled my program into a JAR file the loading doesn't work. those are the lines that load the class file

public void load_dynamic_tag(String file_path)
            throws MalformedURLException, ClassNotFoundException,
            InstantiationException, IllegalAccessException {
        File f = new File(file_path);// some folder's path
        URI uri = f.toURI();
        URL url = uri.toURL();
        URL[] urls = new URL[] { url };
        // create a new class loader for this directory
        ClassLoader cl = new URLClassLoader(urls);
        // load the class file "MyAlgo.class"
        String name = f.getName();
        name = name.substring(0, name.lastIndexOf("."));
        name = "Tagging." + name;
        @SuppressWarnings("unchecked")
        Class<TaggingStrategy> c = (Class<TaggingStrategy>) cl.loadClass(name);
        // TaggingStrategy tag=c.newInstance();
        tag = c.newInstance();
    }

this is the error i get

java.lang.ClassNotFoundException: Tagging.Tag_by_lenght_filename
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at Tagging.Tags_all.load_dynamic_tag(Tags_all.java:233)
        at Command.LoadTagCommandExecutor.execute(LoadTagCommandExecutor.java:24
)
        at GUI.DropBox$2.handleEvent(DropBox.java:248)
        at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
        at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
        at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4066)
        at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3657)
        at GUI.DropBox.showGUI(DropBox.java:168)
        at GUI.Try_control$5.handleEvent(Try_control.java:135)
        at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
        at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
        at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4066)
        at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3657)
        at GUI.Try_control.<init>(Try_control.java:161)
        at GUI.NewSWTApp$2$1$2.run(NewSWTApp.java:121)
        at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
        at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:134)
        at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:4041)
        at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3660)
        at Service.Main.main(Main.java:79)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)

what am i doing wrong? tnx allot

share|improve this question
add comment

2 Answers

My guess is that the file path no longer works, your class is inside the .jar file, and thus an ordinary File won't do. What you need to do is to locate the class file within the .jar file:

File myFile = new File(YourClass.class.getProtectionDomain().getCodeSource().getLocation().toURI()));
JarFile myJar = new JarFile(myFile);

Then get entries inside the jar as follows:

Enumeration entries = jar.entries();

Now, see documentation on Enumeration how to list class files. (Loop over all files, select those ending in .class) From here, you know class name, and can therefore load it.

share|improve this answer
 
and if i want to add a class out of the jar file? –  rafi wiener Aug 13 '11 at 20:33
 
if the class file is NOT in the jar file, you should be able to just provide with an absoulute path. Note that the package that class is inside must match the directory structure, see example on exampledepot.com/egs/java.lang/LoadClass.html (Also, maybe you need to substitute the slashes in the path with a dot), className = classPath.replace('/','.') –  Paxinum Aug 13 '11 at 21:22
add comment

A URLClassLoader looks in directories and jar files for a class. Whatever you pass in as "file_path" would need to be a path to either a jar or directory containing the class you're looking for. If a directory, it should end with a "/". Maybe you're using a relative file path, which would have different meanings based on where it's running from. What's being passed in, and where does the class live?

share|improve this answer
 
i tried sendin a full path using Path p = Paths.get(class_file_path); File class_file = new File(p.toAbsolutePath().toString()); but it didn't work –  rafi wiener Aug 13 '11 at 20:31
 
Yes, read my answer again. The path to the class file is useless. Give it a directory or jar file, exactly as if you were adding a directory or jar to the classpath for compiling or running. E.g. if your class is com.foo.Bar, the file will be at <some dir>/com/foo/Bar.class. You'd need to pass "<some dir>" to the ClassLoader in order to be able to find class Bar. –  Ryan Stewart Aug 14 '11 at 2:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.