简体   繁体   English

Java-Runtime.exec()无法运行

[英]Java - Runtime.exec() fails to run

When i try to run any Runtime.exec() command like this simple one to print the java version: 当我尝试运行任何Runtime.exec()命令(例如此简单命令)以打印Java版本时:

String [] cmd = { "java", "-version" };
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(cmd);

BufferedReader stdError = new BufferedReader( new InputStreamReader( process.getErrorStream() ) );
String s = null;

while ( ( s = stdError.readLine() ) != null )
    System.out.println(s);

It works just fine, but if i am using a JRE bundled with my Java application i always get errors saying that java is unknown: 它工作正常,但是如果我使用与Java应用程序捆绑在一起的JRE,我总是会收到错误消息,说明java是未知的:

java.io.IOException: Cannot run program "java": error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
    at java.lang.Runtime.exec(Runtime.java:620)
    at java.lang.Runtime.exec(Runtime.java:450)
    at java.lang.Runtime.exec(Runtime.java:347)
    at org.runner.Runner.main(Runner.java:11)

Why does this happen when using a bundled JRE? 为什么在使用捆绑的JRE时会发生这种情况?

After a lot of investigation i was able to fix this. 经过大量调查,我能够解决此问题。 I will add my working solution here, may be it will help someone having the same issue: 我将在此处添加我的工作解决方案,可能会帮助遇到相同问题的人:

This issue is caused due to this reported JDK bug: https://bugs.java.com/view_bug.do?bug_id=5049299 此问题是由于以下报告的JDK错误引起的: https : //bugs.java.com/view_bug.do?bug_id=5049299

And since i have this issue on Mac OS X, it was safe for me to use "fork" instead of "posix_spawn" by setting the Java System property: 由于在Mac OS X上存在此​​问题,因此通过设置Java System属性,可以安全地使用“ fork”而不是“ posix_spawn”:

if( isMacOSX() ) System.setProperty( "jdk.lang.Process.launchMechanism", "FORK" );

This might be a discouraged solution, but it has really fixed a huge problem for me on Mac OS X. 这可能是一个不受欢迎的解决方案,但对于Mac OS X而言,它确实为我解决了一个巨大的问题。

I ran into this problem on MacOS too. 我也在MacOS上遇到了这个问题。 It only affected my application when using my bundled jre. 它仅在使用捆绑的jre时影响我的应用程序。 While switching to 'fork' (as you did in your answer) worked for me. 在切换到“叉子”时(就像您在回答中所做的那样)对我有用。 I was curious that there must be a 'better' solution since the bug report you pointed to was filed and fixed years ago. 我很好奇,因为您指出的错误报告是在多年前提交并修复的,所以肯定有一个“更好”的解决方案。 So after a little more digging, I found that, on MacOS there is a executable in the bundled jre (../jre/Contents/Home/jre/lib/jspawnhelper) that needs to be executable in order for Runtime.exec() to work. 因此,在进行更多挖掘之后,我发现在MacOS上,捆绑的jre(../jre/Contents/Home/jre/lib/jspawnhelper)中有一个可执行文件,该文件必须是Runtime.exec()可执行文件。上班。 It was not, but as soon as I made it executable my processes ran without error. 并非如此,但是当我将其设置为可执行文件后,我的进程便运行无误。

To make it executable, I added this to the build.properties file in feature that bundles the jre: 为了使其可执行,我将其添加到捆绑了jre的功能的build.properties文件中:

root.macosx.cocoa.x86_64.permissions.755=jre/Contents/Home/jre/lib/*

You may need to make adjustments to that line depending on you arch and jre location. 您可能需要根据弓和jre的位置对该行进行调整。

Make sure java is added to PATH environment variable. 确保将java添加到PATH环境变量中。 If it is not, use absolute path of the executable. 如果不是,请使用可执行文件的绝对路径。 For eg: 例如:

String [] cmd = { "//usr//bin//java", "-version" };
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class PP { 公共课程PP {

    public void pop(){
    String [] cmd = { "java", "-version" };
    Runtime runtime = Runtime.getRuntime();
    Process process = null;
    try {
        process = runtime.exec(cmd);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    BufferedReader stdError = new BufferedReader( new InputStreamReader( process.getErrorStream() ) );
    String s = null;

    try {
        while ( ( s = stdError.readLine() ) != null )
            System.out.println(s);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }
        public static void main(String []args){
            PP pp=new PP();
        pp.pop();
        }

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM