Java exec can't run program, error = 2

2019-09-17 15:11发布

问题:

I'm trying to run R.exe from a Java application with Java exec. R is in the Environment Variables and if I execute it from cmd with "R" command it works. If I execute an example instruction in Java and try to open notepad it works:

Runtime rt = Runtime.getRuntime();
Process p = rt.exec("notepad");

But when I try to open R with this instruction:

Process p = rt.exec("R");

it gives me this kind of error:

java.io.IOException: Cannot run program "R": CreateProcess error=2

what could be? why cant I open R from Java?

回答1:

You need to ensure that the Windows PATH includes the directory that R resides in for this to work

CreateProcess error=2 indicates the file can not be found