How to run NPM Command in Java using Process Build

2019-04-15 04:13发布

问题:

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"));
    }
}

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 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) 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

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) 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) Same as I like, I use mocha framework to run the test script and result generates the .xml file.

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

回答1:

The problem is that ProcessBuilder does not respect the PATHEXT variable on Windows.

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()
}