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

发布于 2024-07-12 22:24:36 字数 161 浏览 8 评论 0原文

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

这是 Java 1.5 上的

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.

This is on Java 1.5

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如痴如狂 2024-07-19 22:24:36

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

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).

浅忆流年 2024-07-19 22:24:36

我知道我在这个线程中迟到了,但如果有人需要它,根据我的经验,使用 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);
}
}

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:

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);
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文