Forum Stats

  • 3,853,209 Users
  • 2,264,192 Discussions
  • 7,905,287 Comments

Discussions

ClassNotFoundException

User_WL6FP
User_WL6FP Member Posts: 6 Red Ribbon
edited Apr 25, 2018 7:59AM in Java Programming

Hi,

I'm getting ClassNotFoundException error in this code below.

String url ="http://<some-path>/app.jar";

URL[] classLoaderUrls = new URL[]{new URL(url)};

                           

ClassLoader cl = new URLClassLoader(classLoaderUrls);

Class<?> beanClass = cl.loadClass("com.app.MyClass"); // Throwing ClassNotFoundException although this class is present in the JAR in the  fully qualified package name.

Any clue what to fix here ?

Answers

  • Unknown
    edited Apr 24, 2018 1:14PM
    Any clue what to fix here ?

    Haven't used that class to load jars.

    See if you can get it to work using the JarClassLoader and the url syntax shown in The Java Tutorials examples.

    https://docs.oracle.com/javase/tutorial/deployment/jar/jarclassloader.html

    The JarURLConnection class and JAR URLs

    The getMainClassName method uses the JAR URL format specified by the java.net.JarURLConnection class. The syntax for the URL of a JAR file is as in this example:jar:http://www.example.com/jarfile.jar!/ 

    The terminating !/ separator indicates that the URL refers to an entire JAR file.

  • User_WL6FP
    User_WL6FP Member Posts: 6 Red Ribbon
    edited Apr 25, 2018 7:59AM

    Okay ..here is where I'm stuck in JarURLConnection.

    String className="com.app.MyClass"   // This class  has no main method but it is in the JAR

    String JAR_URL="jar:"+<some_http_url>+"!/";

    URL jarUrl = new URL(JAR_URL);

    JarURLConnection jarURLConnection = (JarURLConnection)jarUrl.openConnection();

    Class<?> beanClass = jarURLConnection.loadClass(className); // This does not even compile since there is no loadClass method in JarURLConnection

    I'd appreciate if you provide  tested  solution to this problem.  I have researched in google and tested with other stream reading approaches but none was able to load class.

    my goal is :   I  want to load a specific given class from a JAR.  and this JAR  file is accessible  in http .

    Is it possible at all ?  any working solution  ?

This discussion has been closed.