0

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

Community
  • 1
  • 1
LostReality
  • 657
  • 2
  • 8
  • 33
  • 3
    The error seems to be complaining that class `net.sf.latexdraw.LaTeXDraw` does not implement `Runnable`. Is that indeed the case? Note that most Java applications' main classes indeed do not implement `Runnable`. – John Bollinger Jan 09 '17 at 17:19
  • 3
    `Runnable` is not a *subclass* of `LaTeXDraw`, is it? Can't be, assuming you're referring to [`java.lang.Runnable`](https://docs.oracle.com/javase/8/docs/api/java/lang/Runnable.html). `asSubclass()` is for *down-casting*. If `LaTeXDraw` implements `Runnable`, that would be an up-cast, and you wouldn't need to cast the `Class` for that, especially considering that `Runnable`, being an interface, does actually *have* a constructor. – Andreas Jan 09 '17 at 17:25
  • Please share the class signature of LaTeXDraw class – Srikanth Balaji Jan 09 '17 at 17:43
  • Thanks for all your feedback, I kind just copy-paste the code from the thread without understand it. You all made it clearer ! – LostReality Jan 09 '17 at 22:32

1 Answers1

0

As mentioned in the comments, your problem is simply that net.sf.latexdraw.LaTeXDraw does not implement Runnable. I don't really know what you were expecting to happen.

Make your Robot class implement Runnable and use LaTeXDraw from within there.

public class Robot implements Runnable
{
    @Override
    public void run()
    {
       LaTeXDraw thing = new LaTeXDraw(); // or whatever
       //...
    }
}

//...
Class<?> clazz = Class.forName("package.Robot", true, loader);
Class<? extends Runnable> runClass = clazz.asSubclass(Runnable.class);
//...
Michael
  • 41,989
  • 11
  • 82
  • 128
  • Thank you for your answer, it was indeed a stupid error from myself. I will give return asap, now I understand a bit more how class loading works. – LostReality Jan 09 '17 at 22:30