您的位置:首页 > 编程语言 > Java开发

Java运行cmd命令

2015-06-21 17:44 465 查看
public void apktool(){
		apkFile = new File(apkFilePath);
		String debugFilePath = apkFile.getParent() + "\\debug";
		debugFile = new File(debugFilePath);
		if(!debugFile.exists()){
			debugFile.mkdir();
		}
		String commanApktool = String.format("cmd /c apktool d -f %s -o %s", apkFilePath,debugFilePath); 
		System.out.println(commanApktool);
		runCmd(commanApktool);
	}
<span style="color: rgb(84, 84, 84); font-family: Verdana, Arial; font-size: 14px; line-height: 18px;">cmd /c dir 是执行完dir命令后关闭命令窗口。 </span><br style="color: rgb(84, 84, 84); font-family: Verdana, Arial; font-size: 14px; line-height: 18px;" /><br style="color: rgb(84, 84, 84); font-family: Verdana, Arial; font-size: 14px; line-height: 18px;" /><span style="color: rgb(84, 84, 84); font-family: Verdana, Arial; font-size: 14px; line-height: 18px;">cmd /k dir 是执行完dir命令后不关闭命令窗口。 </span><br style="color: rgb(84, 84, 84); font-family: Verdana, Arial; font-size: 14px; line-height: 18px;" /><br style="color: rgb(84, 84, 84); font-family: Verdana, Arial; font-size: 14px; line-height: 18px;" /><span style="color: rgb(84, 84, 84); font-family: Verdana, Arial; font-size: 14px; line-height: 18px;">cmd /c start dir 会打开一个新窗口后执行dir指令,原窗口会关闭。 </span><br style="color: rgb(84, 84, 84); font-family: Verdana, Arial; font-size: 14px; line-height: 18px;" /><br style="color: rgb(84, 84, 84); font-family: Verdana, Arial; font-size: 14px; line-height: 18px;" /><span style="color: rgb(84, 84, 84); font-family: Verdana, Arial; font-size: 14px; line-height: 18px;">cmd /k start dir 会打开一个新窗口后执行dir指令,原窗口不会关闭。</span>
/**
	 * 运行cmd命令
	 * @param cmd
	 */
	public void runCmd(String cmd)
	{

		Process p = null;
		try {
			p = Runtime.getRuntime().exec(cmd);
			//获取进程的正确流
			final InputStream is1 = p.getInputStream();
			//获取进程的错误流
			final InputStream is2 = p.getErrorStream();

			//处理正确的流信息
			new Thread() {
				public void run() {
					BufferedReader br1 = new BufferedReader(
							new InputStreamReader(is1));
					try {
						String line1 = null;
						while ((line1 = br1.readLine()) != null) {
							if (line1 != null) {
							}
						}
					} catch (IOException e) {
						e.printStackTrace();
					} finally {
						try {
							is1.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
			}.start();

			//获取错误的流信息
			new Thread() {
				public void run() {
					BufferedReader br2 = new BufferedReader(
							new InputStreamReader(is2));
					try {
						String line2 = null;
						while ((line2 = br2.readLine()) != null) {
							if (line2 != null) {
							}
						}
					} catch (IOException e) {
						e.printStackTrace();
					} finally {
						try {
							is2.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
			}.start();
			p.waitFor();
			p.destroy();
		} catch (Exception e) {
			try {
				p.getErrorStream().close();
				p.getInputStream().close();
				p.getOutputStream().close();
			} catch (Exception ee) {
				ee.printStackTrace();
			}
		}
	}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: