如何在Java的新进程中启动"main"?

Wal*_*lle 12 java multithreading program-entry-point process

问题很简单.如何在另一个java进程中启动main方法?现在我这样做:

startOptions = new String[] {"java", "-jar", "serverstart.jar"};
new ProcessBuilder(startOptions).start();
Run Code Online (Sandbox Code Playgroud)

但他们要求我不要使用外部.jar文件.serverstart.jar显然有一个main方法,但是可以在不调用.jar文件的情况下在另一个进程中调用该main方法吗?

我在考虑这样的事情:

new ProcessBuilder(ServerStart.main(startOptions)).start();
Run Code Online (Sandbox Code Playgroud)

但我不知道是否存在这样的事情.

亲切的问候,

dac*_*cwe 8

无法从java创建新的"java"进程,因为两个进程无法共享一个JVM.(见这个问题和接受的答案).


如果您可以使用创建新的Thread而不是a,Process您可以使用自定义ClassLoader.它离你很近,你可以进入一个新的过程.所有静态和最终字段都将重新初始化!

另请注意,"ServerStart类(对于下面的示例)必须位于当前执行JVM的类路径中:

public static void main(String args[]) throws Exception {
    // start the server
    start("ServerStart", "arg1", "arg2");
}

private static void start(final String classToStart, final String... args) {

    // start a new thread
    new Thread(new Runnable() {
        public void run() {
            try {
                // create the custom class loader
                ClassLoader cl = new CustomClassLoader();

                // load the class
                Class<?> clazz = cl.loadClass(classToStart);

                // get the main method
                Method main = clazz.getMethod("main", args.getClass());

                // and invoke it
                main.invoke(null, (Object) args);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();
}
Run Code Online (Sandbox Code Playgroud)

这是自定义类加载器:

private static class CustomClassLoader extends URLClassLoader {
    public CustomClassLoader() {
        super(new URL[0]);
    }

    protected java.lang.Class<?> findClass(String name) 
    throws ClassNotFoundException {
        try{
            String c = name.replace('.', File.separatorChar) +".class";
            URL u = ClassLoader.getSystemResource(c);
            String classPath = ((String) u.getFile()).substring(1);
            File f = new File(classPath);

            FileInputStream fis = new FileInputStream(f);
            DataInputStream dis = new DataInputStream(fis);

            byte buff[] = new byte[(int) f.length()];
            dis.readFully(buff);
            dis.close();

            return defineClass(name, buff, 0, buff.length, (CodeSource) null);

        } catch(Exception e){
            throw new ClassNotFoundException(e.getMessage(), e);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Cos*_*atu 7

假设一个带有新类加载器的新线程是不够的(我会投票支持这个解决方案),我知道你需要创建一个独特的进程来调用类中的main方法,而不必在其中声明为"jar main方法".清单文件 - 因为您不再拥有不同的serverstart.jar.

在这种情况下,您可以简单地调用java -cp $yourClassPath your.package.ServerStart,就像您在没有(或不想使用)清单Main-Class时运行任何Java应用程序一样.

  • 假设你的`user.dir`是maven项目根目录,`target/classes`不应该用作包前缀,而是添加到文件系统路径:`new String [] {"java"," - ct" ,System.getProperty("user.dir")+"/ target/classes",ServerStart.class.getName()}` (2认同)