How to run NPM Command in Java using Process Builder

后端 未结 2 1410
遥遥无期
遥遥无期 2021-01-07 06:29
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import         


        
2条回答
  •  佛祖请我去吃肉
    2021-01-07 07:22

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

提交回复
热议问题