Custom URLClassLoader, NoClassDefFoundError when run

前端 未结 3 1735
忘掉有多难
忘掉有多难 2021-01-21 03:03

I\'ve created my own URLClassLoader, and set it as the system classloader via java.system.class.loader. It\'s initialized and everything, but the clas

3条回答
  •  清酒与你
    2021-01-21 03:43

    It's visible from a (few) mile(s) away you are not using the custom classloader beside Class.forName

    The ClassNoDefFoundError occurs since the classloader that has loaded current class MyProgram attempts to load org.eclipse.swt.graphics.Point.

    You need to load another class (call it launcher) via Class.forName and then start from there - implement some interface (even runnable will do) and call it.


    edit

    How to do it, a simplistic scenario.
    1. Create another class called mp.loader.Launcher that implements Runnable like that.

    public class Launcher implements Runnable{
    public void run(){
      org.eclipse.swt.graphics.Point pt = new org.eclipse.swt.graphics.Point(0, 0);
      //whatever, start from here.
    }
    }
    

    2. Place it in another jar called swt-loader.jar.

    in MyProgram class use:

    loader.addJarToClasspath("swt-loader.jar");
    Runnable r = (Runnable) Class.forName("mp.loader.Launcher", true, loader);
    r.run();//there you have
    

提交回复
热议问题