Unable to get Eclipse to recognize binaries using ProcessBuilder located on $PATH on mac os x

倖福魔咒の 提交于 2019-12-11 16:18:13

问题


I have just switched over to working on a mac and I am trying to determine why I am unable to get Eclipse to recognize the binary I am trying to run via a ProcessBuilder.

I have tried to run it both as a Java Application in Eclipse and as a TestNG test.

If I compile the class with java and run it directly from the command line it will work but not through Eclipse which leads me to believe the configuration for the $PATH is not setup correctly in my TestNG configuration.

Question

I am sure this is a configuration issue within Eclipse but after searching for a day and coming up short I wanted to post for some help. I have tried to set $PATH on the configuration but it does not seem to work.

Thank you

Update /Answer

It turned out that the PATH I had set on the shell shown below was not the same that Java had which I checked using the code below. After verifying that I then added the proper path to my environment on the ProcessBuilder and executed the script as shown in the answer.

Map<String, String> env = processBuilder.environment();
        for (String key : env.keySet())
             System.out.println(key + ": " + env.get(key));

        Map<String, String> envs = processBuilder.environment();
        System.out.println("Path " + envs.get("PATH"));
        envs.put("PATH", "/usr/local/bin");
        System.out.println("PATH " + envs.get("PATH"));

Code

        File logsDir = new File(logDirectory);
        if (!logsDir.exists()) {
            logsDir.mkdirs();
        }

        // run process directly
        ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.command("appium");

        processBuilder.redirectError(new File(logsDir, "appiumError.txt"));
        processBuilder.redirectOutput(new File(logsDir, "appiumOutput.txt"));

        process = processBuilder.start();

Output (it cannot find node to run appium hence the No such file or directory)

Caused by: java.io.IOException: Cannot run program "appium": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at AppiumService.startAppium(AppiumService.java:77)

Path (The bin for node and appium is in /usr/local/bin)

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:
/usr/local/opt/ant/bin:/usr/local/opt/maven/bin:
/usr/local/opt/gradle/bin

回答1:


The PATH variable of the java-process might be explicitly set by Eclipse not containing the paths you need. You can call the command using the absolute path to the corresponding directory or you might try using a shell to start the process by creating the process with

processBuilder.command("/bin/sh", "-c", "appium");


来源:https://stackoverflow.com/questions/48831773/unable-to-get-eclipse-to-recognize-binaries-using-processbuilder-located-on-pat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!