我正在将Node Webkit用于我的Web应用程序,而我真的是使用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信息。就像Mi Ke Bu在注释中正确指出的那样,数据才返回到node.js。
但是,如果您希望以交互方式从外部程序接收数据(我怀疑您真的不会启动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 notepad.exe
(带有换行符),这当然不存在。
或者,您可以仅使用正斜杠,如示例中的命令行参数中所示。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句