dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: ClassLoading error with ClassNotFound

  1. #1
    Join Date
    Aug 2006
    Posts
    18

    ClassLoading error with ClassNotFound

    Hello,
    I have a problem here regarding classloading.
    I have one main class inside a jar, and another jar. I want the main class to load the other jar file at runtime.

    this is the procedure of my classloading:
    Code:
    URL url = new URL("jar:file://" + file.getAbsolutePath() + "!/");
    URLClassLoader ucl = new URLClassLoader(new URL[] { url });
    Class c = Class.forName("com.mvc.FirstClass", true, ucl);
    the package of the main class is com.mvc

    inside the jar file, there is one class that is FirstClass. This is the content of that file:
    Code:
    package com.mvc;
    import org.hibernate.Session;
    public class FirstClass {
    ....
    }
    the error states that the program couldn't find class "org.hibernate.Session", but actually, the jar that contains org.hibernate.Session is included inside the jar of the main class/in classpath

    does anyone here know what happen?

  2. #2
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    want the main class to load the other jar file
    Programs don't "load" jar files. jar files contain class files or data that the program can use.
    jar that contains org.hibernate.Session is included inside the jar
    If a jar file is an entry inside of another jar file, java won't see it as a library to search to find things. Move the jar file out of the other jar file and include it in the list of jar files on the classpath.

  3. #3
    Join Date
    Aug 2006
    Posts
    18
    Hi Norm,

    Yes, actually the program doesn't "load" the jar. It loads the classes inside, by using URLClassLoader.

    If I don't import org.hibernate.Session in a class within the external jar, then the loading goes fine. The main class could access all methods and functions in that class.

    I don't want to include that external jar in the classpath. Because I want to load that jar anytime I want when the main class is running.

  4. #4
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    I want to load that jar
    then the loading goes fine
    I don't understand your definition of what "load" a jar is.

    import org.hibernate.Session in a class
    The import statement only has effect at compile time.

    It the external jar file has class files used by the JVM when the main class is running, then that jar file must be on the classpath for the JVM to be able to find the classes.

    Can you explain how many jar files there are, when they are used: compile time and execute time and why you don't want to put the external jar file on the classpath?

  5. #5
    Join Date
    Aug 2006
    Posts
    18
    hi Norm,

    you don't have to specify that jar location in classpath, in fact, you can load that jar while your application is running, thanks to the ClassLoader / URLClassLoader.

    What I mean by "loading that jar", is to read the jar, inject the classes to the running
    program, so that I can execute the methods in those classes. So far, loading the jar,
    reading the classes, and execute the methods go with no problem. The problem is when
    any class in that jar has import to other external class (which is actually imported in the running class)

  6. #6
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    Not sure what import means in this context. Do you mean load a class?
    For one class to execute code that refers to another class, that other class must be available to the JVM. This is often done by putting the other class on the classpath.

    If you have a jar that refers to classes not in that jar, then you'll need to find where those other classes are and using some class loader, load them.

    the problem is when any class in that jar has import to other external class (which is actually imported in the running class)
    What does this say? What does the "actually imported" mean here?
    The import statement is a compiler directive helping it to find class definitions. Nothing to do with executing the code.

  7. #7
    Join Date
    May 2004
    Posts
    40
    The problem here (I think) is that you two are working from different assumptions toward different goals.

    efm, for some reason (simplicity of installation?) wants to package everything in the application into a single jar file. To do this, either the program will have to duplicate much of the JVM class loader functionality, or the other jars will have to be unpackaged and their contents placed correctly in the class path.

    If I understand you correctly, you've got part of what you want working, but not completely. Take look at this reference, which I found in quick order by googling "dynamically load jar file" In this the author creates a class loader file which adds each class pulled from the jar files onto the class path.

    Hope this helps.

  8. #8
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    class loader file which adds each class pulled from the jar files onto the class path.
    Thanks for the link. Looks interesting. I've written several tools for these kinds of problems.

    We'll have to wait for the OPs next post to see if you've gotten it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center