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机器,我想在其他机器上运行相同的程序。如何使用Process Builder在Java中运行NPM命令
1)NPM是一个命令来作为捆绑NodeJS
。 (我运行NodeJS作为服务,我已经定义了环境变量,所以我可以从任何文件夹运行npm update命令)
2)我无法找到解决方法来运行npm update命令而不使用"cmd", "/c"
。如果我这样做,我得到以下错误
线程“main”中的异常java.io.IOException:无法运行程序“npm update”(在目录“E:\ TestScript”中):CreateProcess error = 2,系统找不到
3在java.lang.ProcessBuilder.start(来源不明)指定 文件)我们有没有为Node.exe
参数运行NPM更新命令的选项。如果有的话,任何人都可以提供适当的解决方案。
4)和我喜欢的一样,我使用摩卡框架来运行测试脚本,结果生成.xml文件。
5)我想使用进程生成器调用mocha命令。
是的谢谢@Wollnyst。它正常工作。 – Vasanthraj
它应该是代码 – thatsIch
中的“npm.cmd”而不是“npm.cm”'谢谢,我已经改变了它 – wollnyst