执行带参数的可运行JAR文件从JavaFx应用程序中

huangapple go评论79阅读模式
英文:

Execute Runnable Jar with args from JavaFx application

问题

我需要从Java FX应用程序运行可执行JAR文件。
我正在尝试以下代码:

public void runJar() {
    try {
        //String serverIp = oRMIServer.getServerIP();
        String arg1 = "\"arg1\"";
        String arg2 = "\"arg2\"";
        String command = "java -jar "  + "path/of/jar.jar " + arg1 + " " + arg2;
        Runtime run = Runtime.getRuntime();
        Process proc = run.exec(command);
    } catch (Exception e) {
        System.out.println("Exception occurred "+e);
    }
}

令人惊讶的是,如果我在Eclipse中创建一个新类并编写此代码,它会在我的Eclipse中运行,但是从我的Java FX应用程序中,此代码根本不运行。我无法看到任何东西,似乎它被跳过了。
请有人帮忙解决如何从Java FX应用程序执行带有参数的Java可执行JAR文件。

尽管我现在使用了以下代码

String command = "cmd /c start \"\" \"" + classPath + "\\jre\\bin\\javaw.exe\" -jar \"" + classPath + "\\jarName.jar\" " + runTimeArg1 + " " + runTimeArg2;

Runtime run = Runtime.getRuntime();
Process proc = run.exec(command);

即使在这之后仍然没有任何显示。请帮忙!

英文:

I need to run executable jar from java fx application.
I am trying with following code :

public void runJar() {
    try {
        //String serverIp = oRMIServer.getServerIP();
        String arg1 = "\"arg1\"";
        String arg2 = "\"arg2\"";
        String command = "java -jar "  + "path/of/jar.jar " + arg1 + " " + arg2;
        Runtime run = Runtime.getRuntime();
        Process proc = run.exec(command);
    } catch (Exception e) {
        System.out.println("Exception occured "+e);
    }

Surprisingly its running in my eclipse if I am creating a new class and writing this code, but frm my Java FX application this code is not running at all. I am not able to see anything looks ike it got skipped.
Can someone please help on how to execute java executable jar with args from Java FX application.

Although I've used now

String command = "cmd /c start \"\" \"" + classPath + "\\jre\\bin\\javaw.exe\" -jar \"" + classPath + "\\jarName.jar\" " + runTimeArg1 + " " + runTimeArg2;

Runtime run = Runtime.getRuntime();
Process proc = run.exec(command);

After this also nothing is coming up. Please help!

答案1

得分: 1

确保你的代码在 try-catch 块中运行,并打印出堆栈跟踪。你应该使用 String[] command,这样就不会使用字符串连接,并使用 ProcessBuilder 调用,以便可以查看子进程的标准输出/错误输出。以下是一个示例:

try {
    String java = Path.of(System.getProperty("java.home"), "bin", "javaw.exe").toAbsolutePath().toString();
    String[] command = new String[] {java, "-jar", "\\path\\to\\yourName.jar", "runTimeArg1", "runTimeArg2"};
    exec(command); // 或者 Runtime.getRuntime().exec(command);
} catch (Exception e) {
    System.out.println("Exception occurred " + e);
    e.printStackTrace();
}

public static int exec(String[] cmd) throws InterruptedException, IOException {
    System.out.println("exec " + Arrays.toString(cmd));

    ProcessBuilder pb = new ProcessBuilder(cmd);

    Path tmpdir = Path.of(System.getProperty("java.io.tmpdir"));
    Path out = tmpdir.resolve(cmd[0] + "-stdout.log");
    Path err = tmpdir.resolve(cmd[0] + "-stderr.log");
    pb.redirectOutput(out.toFile());
    pb.redirectError(err.toFile());
    // 或者 pb.redirectErrorStream(true);

    Process p = pb.start();
    long pid = p.pid();
    System.out.println("started PID " + pid);
    int rc = p.waitFor();

    System.out.println("Exit PID " + pid + ": RC " + rc + " => " + (rc == 0 ? "OK" : "**** ERROR ****"));
    System.out.println("STDOUT: \"" + Files.readString(out) + "\"");
    System.out.println("STDERR: \"" + Files.readString(err) + "\"");
    System.out.println();
    return rc;
}
英文:

Make sure your code is run in try catch and print the stack trace. You should use the String[] command so that string concatenation is not used and call with ProcessBuilder so that you can see STDOUT/ERR of your subprocess. Here is an example:

try {
String java = Path.of(System.getProperty("java.home"),"bin", "javaw.exe").toAbsolutePath().toString();
String[] command = new String[] {java, "-jar", "\\path\\to\\yourName.jar", "runTimeArg1", "runTimeArg2"};
exec(command); // or Runtime.getRuntime().exec(command); 
} catch (Exception e) {
System.out.println("Exception occurred "+e);
e.printStackTrace();
}
public static int exec(String[] cmd) throws InterruptedException, IOException
{
System.out.println("exec "+Arrays.toString(cmd));
ProcessBuilder pb = new ProcessBuilder(cmd);
Path tmpdir = Path.of(System.getProperty("java.io.tmpdir"));
Path out = tmpdir.resolve(cmd[0]+"-stdout.log");
Path err = tmpdir.resolve(cmd[0]+"-stderr.log");
pb.redirectOutput(out.toFile());
pb.redirectError(err.toFile());
// OR pb.redirectErrorStream(true);
Process p = pb.start();
long pid = p.pid();
System.out.println("started PID "+pid);
int rc = p.waitFor();
System.out.println("Exit PID "+pid+": RC "+rc +" => "+(rc == 0 ? "OK": "**** ERROR ****"));
System.out.println("STDOUT: \""+Files.readString(out)+'"');
System.out.println("STDERR: \""+Files.readString(err)+'"');
System.out.println();
return rc;
}

huangapple
  • 本文由 发表于 2020年9月18日 00:11:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63942097.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定