从另一个类错误调用Spring主方法

时间:2016-06-09 22:43:01

标签: spring plugins openfire

我正在为一个OpenFire服务器插件工作。我试图将Spring集成到这个插件中。当插件初始化时,我想为我的Spring调用Main方法。

当我单独执行Spring时,它工作正常,但是当我从我的插件调用它的main方法时,我得到一个异常。 我怎么称为Spring Main方法。 我错过了什么任何帮助将不胜感激。谢谢。

Spring Main Class:

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        try {
            SpringApplication.run(Application.class, args);
            System.out.println("No error");
        } catch (Exception e) {
            System.out.println("Error " + e);

        }

    }
}

OpenFire插件:

public class FetchNewsPlugin implements Plugin {

    @Override
    public void initializePlugin(PluginManager manager, File pluginDirectory) {
        Runnable r = new Runnable() {
             public void run() {
                 String[] args = {};
                Application.main(args);
             }
         };

         new Thread(r).start();
         System.out.println("Plugin Intitialized");

    }

    @Override
    public void destroyPlugin() {
    }

}

日志输出:

  

线程“Thread-13”中的异常java.lang.NoClassDefFoundError:org / springframework / boot / SpringApplication       at hello.Application.main(Application.java:11)       在org.clinton.openfire.plugin.FetchNewsPlugin $ 1.run(FetchNewsPlugin.java:20)       在java.lang.Thread.run(Thread.java:745)   引起:java.lang.ClassNotFoundException:org.springframework.boot.SpringApplication       at java.net.URLClassLoader.findClass(URLClassLoader.java:381)       at java.lang.ClassLoader.loadClass(ClassLoader.java:424)       at java.lang.ClassLoader.loadClass(ClassLoader.java:357)       ......还有3个

抛出异常的地方:

 /**
     * Finds and loads the class with the specified name from the URL search
     * path. Any URLs referring to JAR files are loaded and opened as needed
     * until the class is found.
     *
     * @param name the name of the class
     * @return the resulting class
     * @exception ClassNotFoundException if the class could not be found,
     *            or if the loader is closed.
     * @exception NullPointerException if {@code name} is {@code null}.
     */
    protected Class findClass(final String name)
        throws ClassNotFoundException
    {
        final Class result;
        try {
            result = AccessController.doPrivileged(
                new PrivilegedExceptionAction>() {
                    public Class run() throws ClassNotFoundException {
                        String path = name.replace('.', '/').concat(".class");
                        Resource res = ucp.getResource(path, false);
                        if (res != null) {
                            try {
                                return defineClass(name, res);
                            } catch (IOException e) {
                                throw new ClassNotFoundException(name, e);
                            }
                        } else {
                            return null;
                        }
                    }
                }, acc);
        } catch (java.security.PrivilegedActionException pae) {
            throw (ClassNotFoundException) pae.getException();
        }
        if (result == null) {
            throw new ClassNotFoundException(name);
        }
        return result;
    }
SLF4J:类路径包含多个SLF4J绑定。 SLF4J:在[jar:file:/home/clinton/git/Openfire/bin/build/lib/ant/slf4j-simple.jar!/org/slf4j/impl/StaticLoggerBinder.class]中找到绑定 SLF4J:在[jar:file:/home/clinton/git/Openfire/bin/build/lib/dist/slf4j-log4j12.jar!/org/slf4j/impl/StaticLoggerBinder.class]中找到绑定 SLF4J:在[jar:file:/home/clinton/git/Openfire/build/lib/ant/slf4j-simple.jar!/org/slf4j/impl/StaticLoggerBinder.class]中找到绑定 SLF4J:在[jar:file:/home/clinton/git/Openfire/build/lib/dist/slf4j-log4j12.jar!/org/slf4j/impl/StaticLoggerBinder.class]中找到绑定 SLF4J:请参阅http://www.slf4j.org/codes.html#multiple_bindings以获取解释。 SLF4J:实际绑定的类型为[org.slf4j.impl.SimpleLoggerFactory] ​​

2 个答案:

答案 0 :(得分:0)

您似乎正在使用URLClassLoader加载所需的类。但是,URLClassLoader应该包含所有物品或罐子,它不仅取决于罐子本身。

换句话说,您最好提取您的jar并将所有必要的项目添加到URLClassLoader

这样它应该有效。如果有任何问题,请告诉我。

问题https://stackoverflow.com/a/37339725/5619827可能会有所帮助。

答案 1 :(得分:0)

感谢@Gemini的帮助,但我想我太愚蠢了,无法按照自己的方式行事。我找到了另一个解决方法:因为S​​pring可以在单独执行时成功启动。我将Spring捆绑到一个可运行的jar文件中,并在插件启动时使用以下命令执行:

private void startSpring() {
    try {
    ProcessBuilder processBuilder = new ProcessBuilder("/usr/lib/jvm/java-8-oracle/bin/java", "-jar", 
            "/home/clinton/git/Maven/target/mavenproject2-1.0-SNAPSHOT.jar");
    processBuilder.directory(new File("/home/clinton/git/Maven/Working"));
    File log = new File("log");
     processBuilder.redirectErrorStream(true);
     processBuilder.redirectOutput(Redirect.appendTo(log));
     Process p = processBuilder.start();
     assert processBuilder.redirectInput() == Redirect.PIPE;
     assert processBuilder.redirectOutput().file() == log;
     assert p.getInputStream().read() == -1;
    System.out.println("Started success");

        //Process p = processBuilder.start();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

我希望这对某人也有帮助。我总能找到一种方法:)

相关问题