如何使用 Process Builder 在 Java 中运行 NPM Command

问题描述 投票:0回答:2
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机器,我想在其他机器上运行相同的程序。

1) NPM 是一个命令,由

NodeJS
捆绑而成。 (我将 NodeJS 作为服务运行,我已经定义了环境变量,因此我可以从任何文件夹运行 npm update 命令)

2)我无法找到不使用

"cmd", "/c"
来运行 npm update 命令的解决方法。如果我这样做,我会收到以下错误

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

3)我们是否可以选择将 npm update 命令作为

Node.exe
的参数运行。如果是这样,任何人都可以为我提供适当的解决办法。

4)和我一样,我使用mocha框架来运行测试脚本,结果生成.xml文件。

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

java node.js npm processbuilder
2个回答
12
投票

问题在于

ProcessBuilder
不尊重 Windows 上的 PATHEXT 变量。

Windows 上确实没有

npm
二进制文件,但有一个
npm.cmd
。我最好的解决方案是检查平台。像这样的东西:

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

1
投票

在Unix或Linux操作系统中,PathBuilder采用默认的环境路径,因此我们必须更改环境路径并通过bash运行npm命令。

import java.io.File;
import java.util.Map;

public class CommandExecutor {
    public void executeCommand(
        String commandString,
        String directoryToExecuteCommand
    ) {
        try {
            ProcessBuilder processBuilder = new ProcessBuilder(new String{"bash", "-c", commandString});
            Map<String, String> env = processBuilder.environment();
            processBuilder.directory(new File(directoryToExecuteCommand));
            String envPath = "/home/admin123/.nvm/versions/node/v10.15.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin";
            env.put("PATH",envPath);
            processBuilder.start();
        } catch (Exception e) {
           e.printStackTrace();
        }
    }

    public static void main(String args[]) {
        CommandExecutor commandExecutor=new CommandExecutor();
        commandExecutor.executeCommand("npm install", "/home/admin123/Desktop");
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.