如何从Java程序启动完全独立的进程? [英] How do I launch a completely independent process from a Java program?

查看:82
本文介绍了如何从Java程序启动完全独立的进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个用Java编写的程序,用于某些操作,使用用户配置的命令行启动外部程序。目前它使用 Runtime.exec()并且不保留 Process 引用(启动的程序是文本编辑器或存档实用程序,因此不需要系统输入/输出/错误流)。

I am working on a program written in Java which, for some actions, launches external programs using user-configured command lines. Currently it uses Runtime.exec() and does not retain the Process reference (the launched programs are either a text editor or archive utility, so no need for the system in/out/err streams).

但是,当Java程序退出时,存在一个小问题,在退出所有已启动的程序之前,它并没有真正退出。

There is a minor problem with this though, in that when the Java program exits, it doesn't really quit until all the launched programs are exited.

如果启动的程序完全独立于启动它们的JVM,我会更喜欢它。

I would greatly prefer it if the launched programs were completely independent of the JVM which launched them.

目标操作系统是多个,Windows,Linux和Mac是最小的,但任何带有JVM的GUI系统都是最理想的(因此实际命令行的用户可配置性)。

The target operating system is multiple, with Windows, Linux and Mac being the minimum, but any GUI system with a JVM is really what is desired (hence the user configurability of the actual command lines).

有谁知道如何使启动的程序完全独立于JVM执行?

Does anyone know how to make the launched program execute completely independently of the JVM?

编辑以回复评论

启动代码如下。代码可以启动位于特定行和列的编辑器,也可以启动存档查看器。配置的命令行中的引用值被视为ECMA-262编码,并被解码并且引号被剥离以形成所需的exec参数。

The launch code is as follows. The code may launch an editor positioned at a specific line and column, or it may launch an archive viewer. Quoted values in the configured command line are treated as ECMA-262 encoded, and are decoded and the quotes stripped to form the desired exec parameter.

启动发生在EDT上。

static Throwable launch(String cmd, File fil, int lin, int col) throws Throwable {
    String frs[][]={
        { "$FILE$"  ,fil.getAbsolutePath().replace('\\','/') },
        { "$LINE$"  ,(lin>0 ? Integer.toString(lin) : "") },
        { "$COLUMN$",(col>0 ? Integer.toString(col) : "") },
        };
    String[] arr; // array of parsed tokens (exec(cmd) does not handle quoted values)

    cmd=TextUtil.replace(cmd,frs,true,"$$","$");
    arr=(String[])ArrayUtil.removeNulls(TextUtil.stringComponents(cmd,' ',-1,true,true,true));
    for(int xa=0; xa<arr.length; xa++) {
        if(TextUtil.isQuoted(arr[xa],true)) {
            arr[xa]=TextDecode.ecma262(TextUtil.stripQuotes(arr[xa]));
            }
        }
    log.println("Launching: "+cmd);
    Runtime.getRuntime().exec(arr);
    return null;
    }






这似乎正在发生只有从我的IDE启动程序时。我正在关闭这个问题,因为问题只存在于我的开发环境中; 这不是生产中的问题。从其中一个答案中的测试程序,以及我已经进行的进一步测试,我感到满意的是,任何平台上的程序的任何用户都不会看到这个问题。


This appears to be happening only when the program is launched from my IDE. I am closing this question since the problem exists only in my development environment; it is not a problem in production. From the test program in one of the answers, and further testing I have conducted I am satisfied that it is not a problem that will be seen by any user of the program on any platform.

推荐答案

如果您发布重现问题所需的最小代码的测试部分,则可能会有所帮助。我在Windows和Linux系统上测试了以下代码。

It may help if you post a test section of minimal code needed to reproduce the problem. I tested the following code on Windows and a Linux system.

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        Runtime.getRuntime().exec(args[0]);
    }
}

并在Linux上测试以下内容:

And tested with the following on Linux:

java -jar JustForTesting.jar /home/monceaux/Desktop/__TMP/test.sh

其中test.sh如下所示:

where test.sh looks like:

#!/bin/bash
ping -i 20 localhost

以及Linux上的这个:

as well as this on Linux:

java -jar JustForTesting.jar gedit

并在Windows上对此进行了测试:

And tested this on Windows:

java -jar JustForTesting.jar notepad.exe

所有这些都启动了他们想要的程序,但Java应用程序没有问题退出。我有以下版本的Sun的JVM,由 java -version 报告:

All of these launched their intended programs, but the Java application had no problems exiting. I have the following versions of Sun's JVM as reported by java -version :


  • Windows:1.6.0_13-b03

  • Linux:1.6.0_10-b33

我有还没有机会在我的Mac上测试。也许与项目中的其他代码发生了一些可能不太清楚的交互。您可能想尝试这个测试应用程序并查看结果。

I have not had a chance to test on my Mac yet. Perhaps there is some interaction occuring with other code in your project that may not be clear. You may want to try this test app and see what the results are.

这篇关于如何从Java程序启动完全独立的进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆