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只要输入你需要执行的命令就行。
看上去很复杂,其实很实用,只要能在命令行执行的命令,都可以拿来试试
详细解决方案
Runtime.getRuntime().exec(cmd) cd
热度:99 发布时间:2024-01-15 22:06:26.0
相关解决方案
- Runtime.getRuntime().freeMemory()关于这个有些疑问
- java.lang.Runtime.getRuntime().exec执行cmd命令异常
- system类里的gc()函数内容Runtime.getRuntime().gc();解决思路
- Runtime.getRuntime().exec("cmd copy G:\\a\\ C:\\goods\\");没有作用解决办法
- Runtime.getRuntime().exec();如何写
- System.Runtime.InteropServices.ExternalException: GDI+ 中发生一般性异常
- Runtime Error 有关问题
- 无法定位Kern exec 3出错的地方解决办法
- 没法定位Kern exec 3出错的地方
- 请问一上为什么QT在symbian会出Panic KERN-EXEC 3的异常?
- 关于UIQ中的查询对话框在手机处于屏保状态之前用户没有对其进行处理导致的KERN-EXEC 3异常有关问题
- KERN-EXEC 3异常
- 关于CActiveSchedulerWait的KERN-EXEC 3异常,遇到过的进
- linux脚本“exec <& 2>&”是什么意思?该如何处理
- linux脚本“exec <& 2>&”是什么意思?解决办法
- QSqlQuery:exec: database not open,该如何处理
- QSqlQuery:exec: database not open,该怎么处理
- 运用exec函数,为什么总报undefined reference to `exec
- Silverlight runtime 1.0,该如何解决
- 各操作系统下的Runtime.getRuntime().exec("")方法
- php exec()某个shell结果失误,原因是php中的 grep只能处理文本流,二进制流不行
- 在table中运用innerHTML IE下报错unkown runtime error
- PDO:exec()返回值的有关问题
- exec 返回127,该如何解决
- ArcGIS Runtime SDK for Android 10.1.1新特性及工程迁徙
- ArcMap.exe启动异常 与ARCGIS Runtime error 解决方法)
- ArcGIS Runtime SDK for Android 2.0公布啦
- ArcGIS Runtime SDK for Windows Phone 3.0正式版公布啦,赶快来体验
- 正则表达式中 test、exec、match 步骤区别
- JS正则表达式(二)-exec()方法的使用