java执行cmd命令
起因:项目需要实现jar包上传到远程私服仓库nexus,查询得有两种方法实现:1.调用nexus rest api 进行发行http请求,这个目前在研究;2.调用maven中的命令将本地仓库中的jar包推送到远程仓库;第二种方法虽然简单,但是扩展性不强,需要部署的服务器上有maven环境,并且在linux和Windows上若都要部署则需要进行系统适配。但不论如何,还是打算学习java执性cmd命令及打开其他程序的操作。本文参考了:https://www.jb51.net/article/80829.htm,https://www.cnblogs.com/zhufu9426/p/7928570.html
Java调用cmd命令,并输出显示信息:
/**
* execute cmd in current directory
* then get the inputStream and print on console
* the Most commonly used
* @throws InterruptedException
*/
@Test
public void executeDirCMD() throws InterruptedException {
try {
Runtime rt = Runtime.getRuntime();
// Process pr = rt.exec("cmd /c dir");
// Process pr = rt.exec("D:/APP/Evernote/Evernote.exe");//open evernote program
Process pr = rt.exec("D:/APP/Tim/Bin/QQScLauncher.exe");//open tim program
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream(),"GBK"));
String line = null;
while ((line = input.readLine())!=null){
System.out.println(line);
}
int exitValue = pr.waitFor();
System.out.println("Exited with error code "