博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
执行 bash/sh 命令
阅读量:7023 次
发布时间:2019-06-28

本文共 3267 字,大约阅读时间需要 10 分钟。

网址:“https://www.webmasterworld.com/linux/3613813.htm”

我的测试代码:

1         Button btnShellCommand01 = (Button)findViewById(R.id.btnShellCommand01); 2         btnShellCommand01.setOnClickListener(new View.OnClickListener() 3         { 4             @Override 5             public void onClick(View v) 6             { 7                 // https://www.webmasterworld.com/linux/3613813.htm 8                 File file = new File("/bin"); 9                 MsgAppendLn02("file : "+file.toString());10                 if (file.exists())11                     MsgAppendLn02(file.toString() + " exists");12                 else13                     MsgAppendLn02(file.toString() + " not exists");14                 //Log.i("zzz", file);15                 Process process = null;16                 MsgAppendLn02("01");17                 try18                 {19                     //process = Runtime.getRuntime().exec("/bin/bash", null, file);// no such file or directory20                     process = Runtime.getRuntime().exec("/system/bin/sh", null, file);21                 }22                 catch(Exception ex)23                 {24                     StringWriter sw = new StringWriter();25                     PrintWriter pw = new PrintWriter(sw);26                     ex.printStackTrace(pw);27                     String strStackTrace = sw.toString();28                     MsgAppendLn02(strStackTrace);29                 }30                 MsgAppendLn02("02");31                 if (process != null)32                 {33                     MsgAppendLn02("03");34                     BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));35                     PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(process.getOutputStream())), true);36                     37                     out.println("cd ..");38                     out.println("pwd");39                     out.println("exit");40                     try41                     {42                         MsgAppendLn02("04");43                         String line;44                         while((line = in.readLine()) != null)45                         {46                             MsgAppendLn02(line);47                         }48                         process.waitFor();49                         in.close();50                         out.close();51                         process.destroy();52                         53                         MsgAppendLn02("05");54                     }55                     catch(Exception ex)56                     {57                         StringWriter sw = new StringWriter();58                         PrintWriter pw = new PrintWriter(sw);59                         ex.printStackTrace(pw);60                         String strStackTrace = sw.toString();61                         MsgAppendLn02(strStackTrace);62                     }63                 } // if64                 65                 Toast.makeText(MainActivity.this, "Shell Command 01 finish .", Toast.LENGTH_SHORT).show();66             }67         }); // btnShellCommand01

 

 

网页内容保存于:百度云 CodeSkill33 --> “ >  > Java_Linux” --> Java_bash_command_Linux__Work

 

X

 

转载于:https://www.cnblogs.com/codeskilla/p/4961582.html

你可能感兴趣的文章
Preprocessor directives
查看>>
DOM 综合练习(二)
查看>>
php-redis的配置与使用
查看>>
mysql中(存储)函数
查看>>
Carthage 的使用
查看>>
linux系统盘扩容操作
查看>>
CDQ分治与整体二分学习笔记
查看>>
qt 常见问题记录
查看>>
Day24 中间件 自定义分页 ModelForm 序列化 缓存 信号
查看>>
codeforces 700A As Fast As Possible 二分求和?我觉得直接解更好
查看>>
POJ 2299 Ultra-QuickSort 求逆序数 线段树或树状数组 离散化
查看>>
让linux好用起来--操作使用技巧
查看>>
816:Abbott's Revenge
查看>>
JQuery选择器
查看>>
nmcli 使用记录---fatt
查看>>
【技巧】EasyUI分页组件pagination显示项控制
查看>>
POJ 3989 A hard Aoshu Problem
查看>>
ubuntu系统的谷歌浏览器的安装
查看>>
在JavaScript中"+"什么时候是链接符号,什么时候是加法运算?
查看>>
POJ1179 Polygon
查看>>