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)我无法找到解决方法来运行 npm update 命令而不使用"cmd", "/c"
。如果我这样做,我会收到以下错误
线程“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 命令。
问题是ProcessBuilder
不尊重PATHEXTWindows 上的变量。
确实没有npm
Windows 上的二进制文件,有一个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()
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)