尝试使用java获取退出时使用错误代码2执行r包

时间:2021-07-20 21:53:08

A small issue while trying to execute R package using Java.

尝试使用Java执行R包时的一个小问题。

Runtime run = Runtime.getRuntime(); 
Process pr = null;
String line = null;
BufferedReader input = null;

try {
    pr = run.exec("cmd /c R");
    input = new BufferedReader(new InputStreamReader(pr.getInputStream()));

    while((line = input.readLine()) != null) {
      System.out.println(line); 
    }

    int exitVal = pr.waitFor();
    System.out.println("Exited with error code " + exitVal);
} catch (Exception e) {         
    e.printStackTrace();
} 

I'm getting Exited with error code 2. Could any one help me?

我正在退出错误代码2.任何人都可以帮助我吗?

2 个解决方案

#1


0  

On Windows exit code 2 generally means "file not found". Check in which folder are you are running the "cmd /c R". You can test this by creating a new file and then search your computer where it was created, or by executing the dir.exe command and then checking the result.

在Windows退出代码2通常意味着“找不到文件”。检查您正在运行“cmd / c R”的文件夹。您可以通过创建新文件然后在计算机上创建它来搜索它,或者执行dir.exe命令然后检查结果来测试。

#2


0  

I suggest you specify the full path of the package R including extension. This is because when run from cmd , it assumes that the file is in the current working directory however, when run from java, the path has to be specified

我建议你指定包R的完整路径,包括扩展名。这是因为从cmd运行时,它假定该文件位于当前工作目录中,但是从java运行时,必须指定路径

Your code should look something like this:

您的代码应如下所示:

 pr = run.exec("cmd /c C:/test/R.exe");

Note: it doesn't have to be a .exe file I just put it as an example. for other files just change exe to the file's extension.

注意:它不必是.exe文件我只是把它作为一个例子。对于其他文件只需将exe更改为文件的扩展名。

Hope this helped.

希望这有帮助。

#1


0  

On Windows exit code 2 generally means "file not found". Check in which folder are you are running the "cmd /c R". You can test this by creating a new file and then search your computer where it was created, or by executing the dir.exe command and then checking the result.

在Windows退出代码2通常意味着“找不到文件”。检查您正在运行“cmd / c R”的文件夹。您可以通过创建新文件然后在计算机上创建它来搜索它,或者执行dir.exe命令然后检查结果来测试。

#2


0  

I suggest you specify the full path of the package R including extension. This is because when run from cmd , it assumes that the file is in the current working directory however, when run from java, the path has to be specified

我建议你指定包R的完整路径,包括扩展名。这是因为从cmd运行时,它假定该文件位于当前工作目录中,但是从java运行时,必须指定路径

Your code should look something like this:

您的代码应如下所示:

 pr = run.exec("cmd /c C:/test/R.exe");

Note: it doesn't have to be a .exe file I just put it as an example. for other files just change exe to the file's extension.

注意:它不必是.exe文件我只是把它作为一个例子。对于其他文件只需将exe更改为文件的扩展名。

Hope this helped.

希望这有帮助。