我正在为我的网络应用程序使用 Node Webkit,我真的是一个使用 Node Webkit 的新手。我想在我的应用程序中运行我的 exe,但我什至无法使用“child_process”打开简单的记事本。我在网站上看到了一些例子,但我仍然发现运行 notepad.exe 很困难,请提前帮助并非常感谢。
var execFile = require
('child_process').execFile, child;
child = execFile('C:\Windows\notepad.exe',
function(error,stdout,stderr) {
if (error) {
console.log(error.stack);
console.log('Error code: '+ error.code);
console.log('Signal received: '+
error.signal);
}
console.log('Child Process stdout: '+ stdout);
console.log('Child Process stderr: '+ stderr);
});
child.on('exit', function (code) {
console.log('Child process exited '+
'with exit code '+ code);
});
我还尝试使用 meadco-neptune 插件运行 exe 并添加插件,我将代码放在 package.json 文件中,但它显示无法加载插件。我的 package.json 文件是这样的
{
"name": "sample",
"version": "1.0.0",
"description": "",
"main": "index.html",
"window": {
"toolbar": false,
"frame": false,
"resizable": false,
"show": true,
"title": " example"
},
"webkit": {
"plugin": true
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "xyz",
"license": "ISC"
}
最佳答案
在 node.js 中,有两种方法可以使用标准模块 child_process
启动外部程序:exec
和 spawn
。
当使用 exec
时,您会在外部程序退出时获得 stdout 和 stderror 信息。只有那时数据才返回到 node.js,正如 Mi Ke Bu 在评论中正确指出的那样。
但是如果你想以交互方式从外部程序接收数据(我怀疑你真的不会启动 notepad.exe),你应该使用另一种方法 - spawn
。
考虑这个例子:
var spawn = require('child_process').spawn,
child = spawn('C:\\windows\\notepad.exe', ["C:/Windows/System32/Drivers/etc/hosts"]);
child.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
child.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
child.on('close', function (code) {
console.log('child process exited with code ' + code);
});
你还需要在路径名中使用双反斜杠:C:\\Windows\\notepad.exe
,否则你的路径被评估为
C:windows
记事本.exe
(带回车)当然不存在。
或者您可以只使用正斜杠,如示例中的命令行参数。
关于node.js - 如何在 Node Webkit 中运行外部 exe?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34174124/