错误的方法:
//CPUID
private static final String cpuid="dmidecode -t processor | grep 'ID' | head -1";
Process p = Runtime.getRuntime().exec(puid);
原因:不会被再次解析,管道符失效
正确的办法:
linux下:
String[] command = { "/bin/sh", "-c", (puid };
Process ps = Runtime.getRuntime().exec(command );
windows下:
String[] command = { "cmd", "/c", (puid };
Process ps = Runtime.getRuntime().exec(command );
linux还有一种方法:
命令【ehco】就是向标准输出设备输出引号中的内容。这里将使用管道命令”|“将【echo】命令的输出作为【openssl】命令的输入。
在Java程序中调用Shell命令
/** * 数据加密处理 * * @param data 要加密的数据 * @param commonKey 加密口令文件名 * @return 加密数据 */ public static final synchronized String encryption(String data, String commonKey){ // 加密后的数据定义 String encryptionData = ""; try { // 加密命令 String encryption = "echo -E \"{0}\" | openssl aes-128-cbc -e -kfile {1} -base64"; // 替换命令中占位符 encryption = MessageFormat.format(encryption, data, commonKey); String[] sh = new String[]{"/bin/sh", "-c", encryption}; // Execute Shell Command ProcessBuilder pb = new ProcessBuilder(sh); Process p = pb.start(); encryptionData = getShellOut(p); } catch (Exception e) { throw new EncryptionException(e); } return encryptionData; }
在Java程序中捕获Shell命令的输出流,并读取加密数据
/** * 读取输出流数据 * * @param p 进程 * @return 从输出流中读取的数据 * @throws IOException */ public static final String getShellOut(Process p) throws IOException{ StringBuilder sb = new StringBuilder(); BufferedInputStream in = null; BufferedReader br = null; try { in = new BufferedInputStream(p.getInputStream()); br = new BufferedReader(new InputStreamReader(in)); String s; while ((s = br.readLine()) != null) { // 追加换行符 sb.append(ConstantUtil.LINE_SEPARATOR); sb.append(s); } } catch (IOException e) { throw e; } finally { br.close(); in.close(); } return sb.toString(); }
相关推荐
Runtime 执行bat
主要介绍了详解Java8与Runtime.getRuntime().availableProcessors(),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
windows环境下IDEA java代码Runtime.getRuntime.exec中shell的执行环境的解决方案前言解决办法后记 前言 在使用IDEA本地开发监控守护线程的后台,我遇上了执行环境不兼容的问题,爆出各种“xxx不是内部或外部命令,...
1,申请root权限Runtime.getRuntime().exec("su"); 2,通过数据输出流DataOutputStream写入pm install命令; 3,最后获取Process进程的返回值int i = process.waitFor();,如果i=0,则表明已获取root权限。
完美解决runtime.exec()执行进程block死锁以及为waitFor设置超时 不需要耗cpu的循环判断exitValue==0 开两个进程搞定
Java调用Linux命令 调用Runtime.exec方法将产生一个本地的进程,并返回一个Process子类的实例, (注意:Runtime.getRuntime().exec(command)返回的是一个Process类的实例), 该实例可用于控制进程或取得进程的...
这里不是通过view来截图,也不是通过底层的framebuffer实现截图,而是采用另外一种方法实现截图,通过Runtime.getRuntime().exec()来实现,并保存在sdcard上,代码很简单。
Runtime runtime = Runtime.getRuntime(); try { runtime.exec("cmd /c start " + url); } catch (IOException e) { e.printStackTrace(); } } /** * 鍦ㄥ欢杩熸寚瀹氱殑绉掓暟鍚庡叧鏈? * ...
直接看代码:方法一: 代码如下:Runtime.getRuntime().exec(“rundll32 url.dll,FileProtocolHandler //www.jb51.net”); 方法二: 代码如下://判断当前系统是否支持Java AWT Desktop扩展 if(java.awt....
java的Runtime.getRuntime().exec(commandStr)可以调用执行cmd指令。 cmd /c dir 是执行完dir命令后关闭命令窗口。 cmd /k dir 是执行完dir命令后不关闭命令窗口。 cmd /c start dir 会打开一个新...
//Process p = Runtime.getRuntime().exec("su"); //然后,在向这个进程的写入要执行的命令,即可达到以root权限执行命令: //dos.flush(); //或者用下面的方式: //Runtime.getRuntime().exec&#...
蜂鸣器exe,可用java调用 Runtime.getRuntime().exec("d:\\beep.exe");
主要介绍了Java编程使用Runtime和Process类运行外部程序的方法,结合实例形式分析了java使用Runtime.getRuntime().exec()方法运行外部程序的常见情况与操作技巧,需要的朋友可以参考下
利用Runtime call操作系统的命令,具体的命令取决于不同的操作系统,注意不要调用Runtime.getRuntime().exec(String)接口,要用Runtime.getRuntime().exec(String[])这个接口,不然复杂命令的执行会有问题。...
Process pro = Runtime.getRuntime().exec(str+" /InFile "+source+" /OutFile "+out); BufferedReader br = new BufferedReader(new InputStreamReader(pro.getInputStream())); //虽然cmd命令可以直接输出,但是...
动态权限工具类
System.out.println("Runtime.getRuntime().maxMemory()="+Runtime.getRuntime().maxMemory()); 而且确实,现有检测工具底层也是用这个语句来进行检测。要解决这个问题,首先我们需要一个可重复使用的测试用例。因此...
所以我们必须到Dos环境下去设置,在java中用Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath()+ """+ " +R")该方法可以实现。因为路径file.getAbsolutePath()中可能会还有空格,所以必须...
su = Runtime.getRuntime().exec("/system/bin/su"); /*String cmd = "chmod 777 " + device.getAbsolutePath() + "\n" + "exit\n";*/ String cmd = "chmod 777 /dev/s3c_serial0" + "\n" + ...