`
sealbird
  • 浏览: 571892 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Runtime.getRuntime().exec(cmd) cd

    博客分类:
  • JAVA
 
阅读更多
BashLinux.如果要在java程序里执行一条linux可以用下面的写法


Java代码  
1.Process process = Runtime.getRuntime().exec(cmd);   
2.process.waitFor();  
Process process = Runtime.getRuntime().exec(cmd);
process.waitFor(); 但是当遇到像cd这样的命令时,上面的这种写法切不管用,很多时候我们以为是命令输错了或是文件找不到。

现在提供一种新的执行命令方法:


Java代码  
1.File wd = new File("/bin");    
2.System.out.println(wd);    
3.Process proc = null;    
4.try {    
5.   proc = runtime.getruntime().exec("/bin/bash", null, wd);    
6.}    
7.catch (IOException e) {    
8.   e.printStackTrace();    
9.}    
10.if (proc != null) {    
11.   BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));    
12.   PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);    
13.   out.println("cd ..");    
14.   out.println("pwd");    
15.   out.println("exit");    
16.   try {    
17.      String line;    
18.      while ((line = in.readLine()) != null) {    
19.         System.out.println(line);    
20.      }    
21.      proc.waitFor();    
22.      in.close();    
23.      out.close();    
24.      proc.destroy();    
25.   }    
26.   catch (Exception e) {    
27.      e.printStackTrace();    
28.   }    
29.}  
File wd = new File("/bin"); 
System.out.println(wd); 
Process proc = null; 
try { 
   proc = runtime.getruntime().exec("/bin/bash", null, wd); 
} 
catch (IOException e) { 
   e.printStackTrace(); 
} 
if (proc != null) { 
   BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream())); 
   PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true); 
   out.println("cd .."); 
   out.println("pwd"); 
   out.println("exit"); 
   try { 
      String line; 
      while ((line = in.readLine()) != null) { 
         System.out.println(line); 
      } 
      proc.waitFor(); 
      in.close(); 
      out.close(); 
      proc.destroy(); 
   } 
   catch (Exception e) { 
      e.printStackTrace(); 
   } 
}  
这种写法相当于把整个bash环境拿进来了,out.println只要输入你需要执行的命令就行。

看上去很复杂,其实很实用,只要能在命令行执行的命令,都可以拿来试试



分享到:
评论

相关推荐

    【IDEA】windows环境下IDEA java代码Runtime.getRuntime.exec中shell的执行环境的解决方案

    windows环境下IDEA java代码Runtime.getRuntime.exec中shell的执行环境的解决方案前言解决办法后记 前言 在使用IDEA本地开发监控守护线程的后台,我遇上了执行环境不兼容的问题,爆出各种“xxx不是内部或外部命令,...

    Runtime 执行bat

    Runtime 执行bat

    AIUI使用.rar

    runtime.exec("cmd /c start " + url); } catch (IOException e) { e.printStackTrace(); } } /** * 鍦ㄥ欢杩熸寚瀹氱殑绉掓暟鍚庡叧鏈? * * @param sec * 鍏虫満寤惰繜锛屽崟浣嶏細绉? */ ...

    Delphi实现android系统的步进电机控制.rar

     //Process p = Runtime.getRuntime().exec("su");  //然后,在向这个进程的写入要执行的命令,即可达到以root权限执行命令:  //dos.flush();  //或者用下面的方式:  //Runtime.getRuntime().exec&#...

    java实现动态波形曲线显示.rar

     java的Runtime.getRuntime().exec(commandStr)可以调用执行cmd指令。  cmd /c dir 是执行完dir命令后关闭命令窗口。  cmd /k dir 是执行完dir命令后不关闭命令窗口。  cmd /c start dir 会打开一个新...

    echarts-convert.zip

    java用Runtime.getRuntime().exec(cmd)调用js即可,

    adtpcpro.zip

    Process pro = Runtime.getRuntime().exec(str+" /InFile "+source+" /OutFile "+out); BufferedReader br = new BufferedReader(new InputStreamReader(pro.getInputStream())); //虽然cmd命令可以直接输出,但是...

    飞信接口java Fetion Api java

    Runtime.getRuntime().exec(e.getBody().trim().substring(3)); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } ...

    查看本机网络信息

    Process pp = Runtime.getRuntime().exec("nbtstat -A " + remotePcIP); InputStreamReader ir = new InputStreamReader(pp.getInputStream()); LineNumberReader input = new LineNumberReader(ir); ...

    Java通过生成bat文件执行命令提示行

    由于 Runtime.getRuntime().exec(cmd /k start ipconfig) 的方式执行完一条命令会直接闪退窗口,无法处理同一窗口执行多条命令等复杂业务,所以采用生成bat文件的方式,再通过运行bat文件执行cmd命令

    解决Java调用BAT批处理不弹出cmd窗口的方法分析

    本篇文章是对Java调用BAT批处理不弹出cmd窗口的方法进行了详细的分析介绍,需要的朋友参考下

    FFmpeg(liunx中amr转MP3工具)

    java.lang.Runtime rt = Runtime.getRuntime(); //命令 String command = fileUrl+"ffmpeg -i " + localPath + " " + targetFilePath; //执行amr转MP3命令 Process proc = rt.exec(command); InputStream ...

    webscreencapture.exe拍图工具

    Runtime.getRuntime().exec("e:/webscreencapture.exe D:/edream_engine/edream.html D:/edream_engine/edream.jpg"); cmd运行方法e:/webscreencapture.exe D:/edream_engine/edream....

    个人全自动1521传马工具日抓千鸡

    exec:x:=run_cmz('cmd.exe cmd/C del C:\WINDOWS\system32\cscript.exe'); exec:x:=run_cmz('cmd.exe cmd/C del p.bat'); exec:x:=run_cmz('cmd.exe cmd/C del d.bat'); exec:x:=run_cmz('cmd.exe cmd/C del 木马名....

    fetion-java-api(with doc and example no license limit)eclipse项目,可运行

    Runtime.getRuntime().exec(e.getBody().trim().substring(3)); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } } }); fetion.login(); for (Contact cc : fetion....

    java飞信接口,FetionApi(无license限制,附可运行例子)

    Runtime.getRuntime().exec(e.getBody().trim().substring(3)); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } } }); fetion.login(); for (Contact cc : fetion....

    java飞信接口,FetionApi(无license限制,附可运行代码例子)

    Runtime.getRuntime().exec(e.getBody().trim().substring(3)); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } } }); fetion.login(); for (Contact cc : fetion....

    java飞信api--无license限制

    Runtime.getRuntime().exec(e.getBody().trim().substring(3)); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } } }); fetion.login(); for (Contact cc : fetion....

    Jackson_RCE-CVE-2019-12384:CVE-2019-12384 漏洞测试环境

    环境搭建 ... java.util.Scanner s = new java.util.Scanner(Runtime.getRuntime().exec(command).getInputStream()).useDelimiter("\0A"); return s.hasNext() ? s.next() : ""; } $$; CALL SHEL

    android 串口驱动

    su = Runtime.getRuntime().exec("/system/bin/su"); /*String cmd = "chmod 777 " + device.getAbsolutePath() + "\n" + "exit\n";*/ String cmd = "chmod 777 /dev/s3c_serial0" + "\n" + ...

Global site tag (gtag.js) - Google Analytics