【问题标题】:Execute an exe file using node.js使用 node.js 执行一个 exe 文件
【发布时间】:2013-11-14 18:06:33
【问题描述】:

我不知道如何在node.js 中执行exe 文件。这是我正在使用的代码。它不工作,不打印任何东西。有没有可能使用命令行执行exe 文件的方法?

var fun = function() {
  console.log("rrrr");
  exec('CALL hai.exe', function(err, data) {

    console.log(err)
    console.log(data.toString());
  });
}
fun();

【问题讨论】:

    标签: javascript node.js


    【解决方案1】:

    如果您使用的是 Node Js 或任何支持 Node JS(React 或 Vue)的前端框架

    const { execFile } = require('child_process');
    
    const child = execFile('chrome.exe', [], (error, stdout, stderr) => {
      if (error) {
        throw error;
      }
      console.log(stdout);
    });
    

    如果 .exe 文件位于机器中的某个位置,请将 chrome.exe 替换为您要执行的应用程序的路径 例如"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"

    const child = execFile('C:\Program Files (x86)\Google\Chrome\Application\chrome.exe', [], (error, stdout, stderr) => {
      if (error) {
        throw error;
      }
      console.log(stdout);
    });
    

    【讨论】:

      【解决方案2】:

      你有没有想过在这个过程中使用批处理文件?我的意思是使用 node.js 启动一个 .bat 文件,它会同时启动一个 .exe 文件?

      只是使用顶部的答案我得到了这个:

      1. 在 exe 文件的目录中创建一个 .bat 文件

      2. 输入bat文件 START <full file name like E:\\Your folder\\Your file.exe>

      3. 输入您的 .js 文件: const shell = require('shelljs') shell.exec('E:\\Your folder\\Your bat file.bat')

      【讨论】:

        【解决方案3】:

        如果您要执行的exe 位于其他目录中,并且您的exe 与其所在的文件夹有一些依赖关系,请尝试在选项中设置cwd 参数

        var exec = require('child_process').execFile;
        /**
         * Function to execute exe
         * @param {string} fileName The name of the executable file to run.
         * @param {string[]} params List of string arguments.
         * @param {string} path Current working directory of the child process.
         */
        function execute(fileName, params, path) {
            let promise = new Promise((resolve, reject) => {
                exec(fileName, params, { cwd: path }, (err, data) => {
                    if (err) reject(err);
                    else resolve(data);
                });
        
            });
            return promise;
        }
        

        Docs

        【讨论】:

          【解决方案4】:

          你可以试试node.js中子进程模块的execFile函数

          参考: http://nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback

          您的代码应该类似于:

          var exec = require('child_process').execFile;
          
          var fun =function(){
             console.log("fun() start");
             exec('HelloJithin.exe', function(err, data) {  
                  console.log(err)
                  console.log(data.toString());                       
              });  
          }
          fun();
          

          【讨论】:

          • 如何在某个文件夹中运行 exce,比如我想通过使用文件夹结构 exec('C:\Program Files\ImageMagick-6.9.1-Q16-HDRI/convert.exe convert 来运行 imagemagik 命令-version', function(){ })
          • 向命令添加两个参数,例如'/t'和'600'添加exec('HelloJithin.exe', ['/t', '600'], function(err, data) { console.log(err) console.log(data.toString()); });
          猜你喜欢
          • 2016-09-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-06
          • 1970-01-01
          • 1970-01-01
          • 2022-06-27
          相关资源
          最近更新 更多