String[] cmd = { "node", "/Users/pramod/Desktop/sample.js"};
Runtime r = Runtime.getRuntime();
Process p = null;
p = r.exec(cmd);
当我尝试在 java 程序中运行此代码时,出现异常。
Exception in thread "main" java.io.IOException: Cannot run program "node": error=2, No such file or directory
最佳答案
指定 Node 的完整路径,例如在我的机器上:
String[] cmd = { "/opt/local/bin/node", "./test.js"};
Runtime r = Runtime.getRuntime();
Process p = r.exec(cmd);
p.waitFor();
System.out.println(p.exitValue());
给我退出代码 0,因为一切都很顺利。
关于java - 无法使用 Runtime.getRuntime().exec() 执行脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38804695/