I am trying to launch an application at runtime from a .jar file. So I followed the code in this thread : How to load a jar file at runtime
But I am having this error :
Exception in thread "main" java.lang.ClassCastException: class net.sf.latexdraw.LaTeXDraw
at java.lang.Class.asSubclass(Unknown Source)
at LatexLauncher.<init>(LatexLauncher.java:17)
at LatexLauncher.main(LatexLauncher.java:26)
Here is the code, which is quite the same from the above thread :
File path = new File("pathToMyJAR/lib/LaTeXDraw.jar");
System.out.println(path.exists()); //return true
ClassLoader loader = URLClassLoader.newInstance(
new URL[] { path.toURI().toURL() },
getClass().getClassLoader()
);
Class<?> clazz = Class.forName("net.sf.latexdraw.LaTeXDraw", true, loader);
Class<? extends Runnable> runClass = clazz.asSubclass(Runnable.class);
Constructor<? extends Runnable> ctor = runClass.getConstructor();
Runnable doRun = ctor.newInstance();
doRun.run();
The above code in the LatexLauncher constructor. The line throwing the error is :
Class<? extends Runnable> runClass = clazz.asSubclass(Runnable.class);
It is an Eclipse project, the .jar file I want to run is in the lib
folder. The required jars for the one I want to run (LaTeXDraw) are in lib/lib/
folder of the project. I have no idea how to fix this.
My final goal is to launch the .jar application and make a Robot
class performs actions onto the UI to make tests.
Any help is welcome, Thank you for your time