java - 无法使用 Runtime.getRuntime().exec() 执行脚本

标签 java node.js

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/

相关文章:

java - C# BitConverter 到 Java

javascript - JS 中处理 Object 的嵌套属性

javascript - 如果重新初始化, module.export.variable 始终是一个空对象

java - GridBagLayout 问题

node.js - 类型错误 : Cannot read property 'showMessageBox' of undefined electron in node js

javascript - Nunjucks 不渲染模板

javascript - MongoDB(可能的 _id 数量)

java - Docker-从 tar 文件安装 Java 8 和 Tomcat 8

java - Spring配置将属性传递给引用的bean

java - 下面如何编写多线程程序的最佳方式