Excute shell and read the output

本文提供了一个使用Java执行外部CMD命令的简单示例。通过Runtime.getRuntime().exec()方法调用CMD并获取输出,展示了如何读取命令执行后的返回结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Test1 {

	public static void main(String args[])
	{
		try {
			Process p = Runtime.getRuntime().exec("cmd ver");
			InputStreamReader isr = new InputStreamReader(p.getInputStream());
			BufferedReader br = new BufferedReader(isr);
			System.out.println(br.readLine());
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

在inputTextByCMD这个方法中类似这种语句,self.adb.shell("input keyevent KEYCODE_MOVE_END"),我可不可以用这个方法中的shell代替,方法如下: class ADBHelper(object): def __init__(self,device_sn=udid): self.device_sn = device_sn self.device_id = self.get_device_id(device_sn) # @classmethod # def get_device_id(cls, remote_ip="", remote_port="", device_sn=""): # if remote_ip == "" or remote_port == "": # if device_sn == "": # device_id = "" # else: # device_id = "-s %s" % device_sn # else: # if device_sn == "": # device_id = "-H %s -P %s" % (remote_ip, remote_port) # else: # device_id = "-H %s -P %s -s %s" % ( # remote_ip, remote_port, device_sn) # return device_id @classmethod def get_device_id(cls, device_sn): if device_sn != '': device_id = "-s %s" % device_sn return device_id ########################################################################### # 封装的命令操作 def execute_command(self, command, print_flag=True, timeout=120): try: if print_flag: print("command: " + command) if subprocess.call(command, shell=True, timeout=timeout) == 0: print("results: successed") return True except Exception as error: print("Exception: %s" % str(error)) print("Warning: %s failed" % command) return False def execute_command_with_output(self, command, print_flag=True): if print_flag: print("command: " + command) proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) try: stdout_data, stderr_data = proc.communicate() if isinstance(stdout_data, bytes): stdout_data = stdout_data.decode('utf-8', 'ignore') finally: proc.stdout.close() proc.stderr.close() return stdout_data def execute_command_with_list_output(self, command, print_flag=True): if print_flag: print("command: " + command) return os.popen(command).readlines() def execute_command_with_content_output(self, command, print_flag=True): if print_flag: print("command: " + command) return os.popen(command).read() def adb(self, command="devices"): return self.execute_command("%s %s %s" % ( USB_TOOLS, self.device_id, command)) def adb_with_output(self, command=""): return self.execute_command_with_output("%s %s %s" % ( USB_TOOLS, self.device_id, command)).strip() def shell(self, command=""): return self.execute_command("%s %s shell %s%s%s" % ( USB_TOOLS, self.device_id, QUOTATION_MARKS, command, QUOTATION_MARKS)) def shell_with_output(self, command=""): return self.execute_command_with_output("%s %s shell %s%s%s" % ( USB_TOOLS, self.device_id, QUOTATION_MARKS, command, QUOTATION_MARKS)).strip() def pull(self, src_path="", dst_path=""): return self.execute_command("%s %s pull %s%s%s %s%s%s" % ( USB_TOOLS, self.device_id, QUOTATION_MARKS, src_path, QUOTATION_MARKS, QUOTATION_MARKS, dst_path, QUOTATION_MARKS))
最新发布
07-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值