使用 Runtime.exec 从 Java 应用程序启动 JVM 进程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/480433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:11:30  来源:igfitidea点击:

Launch JVM process from a Java application use Runtime.exec?

javaprocesslaunch

提问by Berlin Brown

I want to be able to launch a Java server process from an existing java application and monitor the stdoutput and stderror and redirect that output to a file. Is the best approach to use 'Runtime.exec' and treat the app like any other OS process or is there something more suited for new JVMs.

我希望能够从现有的 Java 应用程序启动 Java 服务器进程并监视 stdoutput 和 stderror 并将该输出重定向到文件。是使用“Runtime.exec”并像对待任何其他操作系统进程一样对待应用程序的最佳方法,还是有更适合新 JVM 的方法。

This is on Java 1.5

这是在 Java 1.5 上

采纳答案by Fabian Steeg

Instead of Runtime, you should probably use ProcessBuilder, though I don't know if something else is even more appropriate in your case (running a Java process in particular).

而不是Runtime,您可能应该使用ProcessBuilder,但我不知道其他东西是否更适合您的情况(特别是运行 Java 进程)。

回答by Mel T.

I know I am late in this thread, but in case someone needs it, in my experience, it is easier to use ANT to launch a Java application. This has the benefit of being platform independent. Here is a sample class that does that:

我知道我在这个线程中迟到了,但如果有人需要它,根据我的经验,使用 ANT 启动 Java 应用程序更容易。这具有独立于平台的好处。这是一个执行此操作的示例类:

package com.trilliantnetworks.scheduler.quartz.test;

import java.io.File;
import java.io.PrintStream;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.DemuxOutputStream;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Echo;
import org.apache.tools.ant.taskdefs.Java;
import org.apache.tools.ant.types.Path;

public class MyLaunchTest {

public static void main(String[] args) {

    Project project = new Project();
    project.setBaseDir(new File(System.getProperty("user.dir")));
    project.init();
    DefaultLogger logger = new DefaultLogger();
    project.addBuildListener(logger);
    logger.setOutputPrintStream(System.out);
    logger.setErrorPrintStream(System.err);
    logger.setMessageOutputLevel(Project.MSG_INFO);
    System.setOut(new PrintStream(new DemuxOutputStream(project, false)));
    System.setErr(new PrintStream(new DemuxOutputStream(project, true)));
    project.fireBuildStarted();

    System.out.println("running");
    Throwable caught = null;
    try {
        Echo echo = new Echo();
        echo.setTaskName("Echo");
        echo.setProject(project);
        echo.init();
        echo.setMessage("Launching Some Class");
        echo.execute();

        Java javaTask = new Java();
        javaTask.setTaskName("runjava");
        javaTask.setProject(project);
        javaTask.setFork(true);
        javaTask.setFailonerror(true);
        javaTask.setClassname(MyClassToLaunch.class.getName());
        Path path = new Path(project, new File(System.getProperty("user.dir") + "/classes").getAbsolutePath());
        javaTask.setClasspath(path);
        javaTask.init();
        int ret = javaTask.executeJava();
        System.out.println("java task return code: " + ret);

    } catch (BuildException e) {
        caught = e;
    }
    project.log("finished");
    project.fireBuildFinished(caught);
}
}