mercredi 1 février 2017

Java : URLClassLOader Unable to load class using reflection from executable jar

I have a java application in which we are calling method of a class using Reflection, the jar is placed in some particular location from where we are loading that jar into java application.

In the java eclipse project we have 5 to 6 other external jar reference and one of them are getting used in the above jar(which are getting loaded runtime).

When we run the java application from eclipse it works fine. but when we create executable jar file using option "Package required libraries into generated JAR" and execute that then it throw exception.

Exception text :

    java.lang.ClassNotFoundException: executionSuite.Common
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at  Helpers.ExecuteSelenium.evalReflectionFunction(ExecuteMethod.java:546)
    at Helpers.ExecuteSelenium.runTest(ExecuteExecute.java:280)
    at BackgroundWorker.doInBackground(BackgroundWorker.java:32)
    at BackgroundWorker.doInBackground(BackgroundWorker.java:1)
    at javax.swing.SwingWorker$1.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at javax.swing.SwingWorker.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)


as above exception stating that executionSuite.Common class is not present but in that jar Common class is present inside executeSelenium package.

please refer the following code :

 String jarpath = this.appPath + "test/"+this.AssemblyName+".jar";
 File someJarPath = new File(jarpath);
 URLClassLoader seleniumClassLoader = new URLClassLoader(new URL[]{seleniumJarFile.toURI().toURL()});
 if(!this.htObjClass.containsKey(varClassName))
 {
        // ** exception is getting thrown by below line. ** //
       Class<?> dynamicClass = (Class<?>) seleniumClassLoader.loadClass("executionSuite."+varClassName);
       Constructor<?> cons = dynamicClass.getConstructor(RunEnvironment.class);
       htObjClass.put(varClassName, cons.newInstance(this.objRunEnvClass));
 }
 Object obj2 = this.htObjClass.get(varClassName);
 Method method = obj2.getClass().getDeclaredMethod(methodName, LinkedHashMap.class)

If anyone ever faced this issue and have some solution or suggestion then please suggest. Thanks in advance.





Aucun commentaire:

Enregistrer un commentaire