Discussions

XML & Web services: How to run a java class is inside the ear file

  1. Hi,

       have a ear file that is stored in c:\myw folder
     
    then inside ear file i want to run the schedule.class file..
     
    This class file is stored inside ear file in mywebidea\WEB-INF\classes\jsp
    \sch\ folder
     
    In this Schedule class i import two jar files. that's also already inside this ear file..
     
    i already set the classpath for these 3 files....
     
    But i am getting this error
    Exception in thread "main" java.lang.NoClassDefFoundError: mywebidea/jsp/sch/Sch
    edule (wrong name: jsp/sch/Schedule)
            at java.lang.ClassLoader.defineClass0(Native Method)
            at java.lang.ClassLoader.defineClass(ClassLoader.java:537)
            at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
    3)
            at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
            at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
            at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
            at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
            at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
      
    can anybody help for this
  2. First the URLClassLoader can't load classes from a jar that is inside another jar... I assume that you have packaged the class inside the war file. The war file has its own structure. It has the WEB-INF/classes directory. The URLClassloader thinks that even these are the part of the package structure and that is the reason you are getting a bad name error. Two solutions I can think of.
    1. Create a separate jar with this Schedule.class and other dependent classes and put that in the classpath. If it is required by the web app also, then put the same jar in the web-inf/lib of the war file. Need to develop some good ant scripts to do that.

    2. Write a custom class loader that loads the class files as required from the ear file and use this class loader as the Thread.currentThread().setContextClassLoader( theLoader). If you can write the class loader with the new 1.2 delegation model, the normal jdk classes get loaded from the system class loader and the Schedule.class gets loader by ur custom loader.

    Hope it helps!