nodejs child_process exec'java -version' [英] nodejs child_process exec 'java -version'

查看:687
本文介绍了nodejs child_process exec'java -version'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

他在nodejs shell中运行下面的代码

hey i'm running the following code in nodejs shell

 exec = require('child_process').exec 

现在我有exec var函数。然后我运行

now i have exec var as function. then i run

 exec('java',function (error, stdout, stderr) {console.log(error)});

 exec('java -version',function (error, stdout, stderr) {console.log(error)})

第一个输出是:

  { [Error: Command failed: The system cannot find the path specified.

但输出继续,我得到java的真正输出ie:

but the output continue and i get the real output from java i.e :

     Usage: java [-options] class [args...]
     (to execute a class)
     or  java [-options] -jar jarfile [args...] 
     (etc....)

对我的第二次尝试使用'java -version'
i get

the same goes for the my 2nd try with 'java -version' i get

   { [Error: Command failed: The system cannot find the path specified.
     java version "1.7.0_10"
     Java(TM) SE Runtime Environment (build 1.7.0_10-b18)
     Java HotSpot(TM) 64-Bit Server VM (build 23.6-b04, mixed mode)
     ] killed: false, code: 1, signal: null }



想法?
thanks
oak

any ideas? thanks oak

ps

c> Windows 8 我有 JAVA_HOME 设置,我有%JAVA_HOME%\bin 在我的 PATH
如果我从命令行运行java -version一切正常。
node -version v0.10.22

i'm running on Windows 8 i have JAVA_HOME set and i have %JAVA_HOME%\bin in my PATH if i run java -version from command line everything is ok. "node -version" v0.10.22

编辑:

来自nodejs doc

from nodejs doc


成功时,错误将为null。 Error和err.code将是子进程的退出代码,err.signal将被设置为终止进程的信号。

"On success, error will be null. On error, error will be an instance of Error and err.code will be the exit code of the child process, and err.signal will be set to the signal that terminated the process."


推荐答案

有趣的是,调查nodejs源代码后,我看到exec函数使用'cmd / s / c [command]'
然后我尝试运行相同的命令,没有cmd。发现我还是得到这个消息。
证明这个bug与cmd相关,所以我搜索它,并提出了解决方案。

funny, after investigating nodejs source code i saw exec function use 'cmd /s /c [command]' then i try to run the same command without cmd. found out that i still get this message . turns out that this bug is related to cmd so i searched for it and came up with solution.

为正确的答案在这里:http://stackoverflow.com/a/13343337/1211174

for the right answer go here: http://stackoverflow.com/a/13343337/1211174

摘要答案:cmd有选项autorun。和这个自动运行faild。然后cmd写入stderr然后许多nodejs faild - 因为他们只是检查 if(error!= null),而不是检查error.code ...

summary the answer: cmd has option of autorun. and this auto run faild. then cmd write to stderr and then many nodejs faild - because they just check for if (error != null) and not check for error.code ...

这篇关于nodejs child_process exec'java -version'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆