如何使用Process Builder在Java中运行NPM Command [英] How to run NPM Command in Java using Process Builder

查看:1762
本文介绍了如何使用Process Builder在Java中运行NPM Command的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.*;

public class TestUnZip {
    public static void main(String[] args) throws IOException, InterruptedException{
        String destFolder="E:\\TestScript";
        /*
        *  Location where the Nodejs Project is Present
        */
        System.out.println(destFolder);

        String cmdPrompt="cmd";
        String path="/c";
        String npmUpdate="npm update";
        String npm="npm";
        String update="update";

        File jsFile=new File(destFolder);
        List<String> updateCommand=new ArrayList<String>();
        updateCommand.add(cmdPrompt);
        updateCommand.add(path);
        updateCommand.add(npmUpdate);

        runExecution(updateCommand,jsFile);

    }
    public static void runExecution(List<String> command, File navigatePath) throws IOException, InterruptedException{

        System.out.println(command);

        ProcessBuilder executeProcess=new ProcessBuilder(command);
        executeProcess.directory(navigatePath);
        Process resultExecution=executeProcess.start();

        BufferedReader br=new BufferedReader(new InputStreamReader(resultExecution.getInputStream()));
        StringBuffer sb=new StringBuffer();

        String line;
        while((line=br.readLine())!=null){
            sb.append(line+System.getProperty("line.separator"));
        }
        br.close();
        int resultStatust=resultExecution.waitFor();
        System.out.println("Result of Execution"+(resultStatust==0?"\tSuccess":"\tFailure"));
    }
}

以上程序运行正常,但此程序依赖在Windows机器上,我也希望在其他机器上运行相同的程序。

The Above Program works fine, but this program is depend on Windows Machine, I want to run the same program in other Machine as well.

1)NPM是一个命令来自 NodeJS 。 (我运行NodeJS作为服务,我已经定义了环境变量,所以我可以从任何文件夹运行npm update命令)

1) NPM is a Command comes as a bundle of NodeJS. (I run NodeJS as a service, I have defined the Environment Variable, so I can run npm update command from any folder)

2)我找不到工作在不使用cmd,/ c的情况下运行npm update命令。如果我这样做我得到以下错误

2) I can't find a work around to run the npm update command without using the "cmd", "/c". If I do I get following error


线程main中的异常java.io.IOException:无法运行程序npm update(在目录E:\ TestScript):CreateProcess error = 2,系统找不到指定的文件
在java.lang.ProcessBuilder.start(未知来源)

Exception in thread "main" java.io.IOException: Cannot run program "npm update" (in directory "E:\TestScript"): CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessBuilder.start(Unknown Source)

3)我们是否可以选择运行npm update命令作为 Node.exe 的参数。如果是这样,任何人都可以为我提供适当的工作。

3) Do we have option of Running the npm update command as a parameter of Node.exe. If so can anyone provide me the proper work around.

4)与我喜欢的相同,我使用mocha框架来运行测试脚本,结果生成.xml文件。

4) Same as I like, I use mocha framework to run the test script and result generates the .xml file.

5)我希望使用流程构建器调用mocha命令。

5) I want mocha command also being invoked using process builder.

推荐答案

问题是 ProcessBuilder 不尊重 PATHEXT 变量。

Windows上没有 npm 二进制文件,有一个 npm.cmd 。我最好的解决方案是检查平台。这样的事情:

It's true there is no npm binary on Windows, there's a npm.cmd. My best solution is to check the platform. Something like this:

static boolean isWindows() {
    return System.getProperty("os.name").toLowerCase().contains("win");
}

static String npm = isWindows() ? "npm.cmd" : "npm";

static void run() {
    Process process = new ProcessBuilder(npm, "update")
            .directory(navigatePath)
            .start()
}

这篇关于如何使用Process Builder在Java中运行NPM Command的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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