java如何用cmd命令

发布时间:2025-07-04 点击:10
java如何用cmd命令的方法:1、使用runtime中【exec(string command)】方法执行cmd命令;2、先将执行的cmd命令写入到文件中后再执行,这是如果执行错误日志可以打印,线程也不会卡死。
【相关学习推荐:java基础教程】
java如何用cmd命令的方法:
1.使用runtime中exec(string command)方法执行cmd命令,如下:
process p = runtime.getruntime().exec(cmd);此方法会抛出ioexception,但是在项目中遇到没有出现异常,命令也没有执行的情况。
2.此方法可以达到大多的cmd调用的期望结果,但有些时候回出现命令卡死在p.waitfor();上,造成线程阻塞
public static boolean runcmd(string cmd) throws ioexception, interruptedexception { final string method_name = "runcmd"; process p = runtime.getruntime().exec(cmd); bufferedreader br = null; try { br = new bufferedreader(new inputstreamreader(p.geterrorstream())); string readline = br.readline(); stringbuilder builder = new stringbuilder(); while (readline != null) { readline = br.readline(); builder.append(readline); } logger.debug(method_name "#readline: " builder.tostring()); p.waitfor(); int i = p.exitvalue(); logger.info(method_name "#exitvalue = " i); if (i == 0) { return true; } else { return false; } } catch (ioexception e) { logger.error(method_name "#errmsg=" e.getmessage()); e.printstacktrace(); throw e; } finally { if (br != null) { br.close(); } } }3.使用以下方法不会出现和2一样情况下得阻塞的问题,与2的区别就是获取流不同,将geterrorstream换成getinputstream就好了
public static boolean runcmd(string cmd) throws ioexception, interruptedexception { final string method_name = "runcmd"; // process p = runtime.getruntime().exec("cmd.exe /c " cmd); process p = runtime.getruntime().exec(cmd); bufferedreader br = null; try { // br = new bufferedreader(new inputstreamreader(p.geterrorstream())); br = new bufferedreader(new inputstreamreader(p.getinputstream())); string readline = br.readline(); stringbuilder builder = new stringbuilder(); while (readline != null) { readline = br.readline(); builder.append(readline); } logger.debug(method_name "#readline: " builder.tostring()); p.waitfor(); int i = p.exitvalue(); logger.info(method_name "#exitvalue = " i); if (i == 0) { return true; } else { return false; } } catch (ioexception e) { logger.error(method_name "#errmsg=" e.getmessage()); e.printstacktrace(); throw e; } finally { if (br != null) { br.close(); } } } 4.对于3方法有个缺点是执行错误时无法将错误消息打印出来,还有一个方法是先将执行的cmd命令写入到文件中后再执行,这是如果执行错误日志可以打印,线程也不会卡死。
a.将执行的命名写入到文件中。fileutils.java
public static boolean writefile(file exportfile, final string content) { if (exportfile == null || stringutils.isempty(content)) { return false; } if (!exportfile.exists()) { try { exportfile.getparentfile().mkdirs(); exportfile.createnewfile(); } catch (ioexception e) { e.printstacktrace(); logger.error("create local json file exception: " e.getmessage()); return false; } } bufferedwriter bufferedwriter = null; try { fileoutputstream os = new fileoutputstream(exportfile); filedescriptor fd = os.getfd(); bufferedwriter = new bufferedwriter(new outputstreamwriter(os, "utf-8")); bufferedwriter.write(content); //flush the data from the streams and writes into system buffers //the data may or may not be written to disk. bufferedwriter.flush

抗疫进行时 联想向中小企业提供Office商用版免费授权
教你10分钟设置免费远程桌面
蔡文胜解疑 个人网站怎样赚钱?
自助建站优势
腾讯云服务器配置nginx
手机网页如何制作,有哪些步骤和要点?
阿里云如何续费服务器
CentOS6搭建NFS服务器