简体   繁体   English

nodejs child_process exec'java -version'

[英]nodejs child_process exec 'java -version'

hey i'm running the following code in nodejs shell 嘿,我在nodejs shell中运行以下代码

 exec = require('child_process').exec 

now i have exec var as function. 现在我有exec var作为函数。 then i run 然后我跑

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

and

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

the first output is: 第一个输出是:

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

but the output continue and i get the real output from java ie : 但是输出继续,我从java得到真正的输出,即:

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

the same goes for the my 2nd try with 'java -version' i get 我对'java -version'的第二次尝试也是如此

   { [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 }

any ideas? 有任何想法吗? thanks oak 谢谢橡树

ps ps

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. 我在运行Windows 8 ,我有JAVA_HOME设置和我有%JAVA_HOME%\\bin在我的PATH如果我在命令行中运行java -version一切正常。 "node -version" v0.10.22 “节点-版本” v0.10.22

EDIT: 编辑:

from nodejs doc 从nodejs文档

"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." “成功时,错误将为null。错误时,错误将是Error的一个实例,而err.code将是子进程的退出代码,而err.signal将被设置为终止该进程的信号。”

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. 有趣的是,在研究了nodejs源代码之后,我看到exec函数使用了'cmd /s /c [command]'然后我尝试在不使用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. 原来,该错误与cmd有关,因此我进行了搜索并提出了解决方案。

for the right answer go here: https://stackoverflow.com/a/13343337/1211174 对于正确的答案,请转到此处: https : //stackoverflow.com/a/13343337/1211174

summary the answer: cmd has option of autorun. 总结答案:cmd具有自动运行的选项。 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 ... 然后将cmd写入stderr,然后许多nodejs失败-因为它们只是检查if (error != null)而没有检查error.code ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM