Search code examples
javaglassfishejbwarear

ClassNotFoundException for skinny war


I'm facing class loading problem with class loading in my skinny war packaged within ear. Where there are also other ejb modules packaged. Some can be loaded via spring while some not.

Imagine following scenario:

someApp.ear
|- someEJBs1.jar
|- someEJBs2.jar
...
|- someEJBsX.jar
|- someWar.war

If I try from the someWar.war to access via spring some classes present in:

  • someEJBs1.jar it works
  • but for those in someEJBs2.jar I'm getting java.lang.ClassNotFoundException.

No clue where the problem is. My MANIFEST.MF doesn't contain any classpath information at all.

I was already thinking if there there any way to see during runtime (maybe during debugging) the classes loaded, or possibly the classpath provided for the particular war file?

I'm running in glassfish 3.1.1 (that is a requirement, no change on this one is possible).


Solution

  • Try the following debug functions in your code:

        //Prints the Root directory
        public static void printPWD() {
            File dir1 = new File(".");
            try {
                System.out.println("Current dir : " + dir1.getCanonicalPath());
                dir1 = null;
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    
    
         // Prints all files on the ClassPath to the system.out
    
        public static void printClasspathFiles() {
            ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader();
    
            // Get the URLs
            URL[] urls = ((URLClassLoader) sysClassLoader).getURLs();
    
            for (int i = 0; i < urls.length; i++) {
                System.out.println(urls[i].getFile());
            }
        }
    
    //prints the classpath to system.out
    print printClasspath(){
           System.out.println(System.getProperty("java.class.path"));
    }
    

    To get all loaded classes, the getAllLoadedClasses Method would be useful. See Here