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

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

问题描述

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

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 应用程序退出没有问题.我有 java -version 报告的以下 Sun JVM 版本:

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天全站免登陆