java代码中执行liunx命令
2024-09-01 22:51:58
public static String runShell(String command){
try{
String[] commands = isLinux()?new String[]{"/bin/sh","-c",command}:new String[]{"cmd","/c",command};
Process process = Runtime.getRuntime().exec(commands);
InputStreamReader ir = new InputStreamReader(process.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String ln;
StringBuilder result = new StringBuilder();
while ((ln = input.readLine()) != null){
result.append(ln);
result.append("\n");
}
input.close();
ir.close();
return result.toString();
}
catch(Exception e){
e.printStackTrace();//log
return null;
}
}
public static boolean isLinux(){
String os = System.getProperty("os.name").toLowerCase();
if(null != os && os.contains("windows")){
return false;
}
return true;
}
try{
String[] commands = isLinux()?new String[]{"/bin/sh","-c",command}:new String[]{"cmd","/c",command};
Process process = Runtime.getRuntime().exec(commands);
InputStreamReader ir = new InputStreamReader(process.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String ln;
StringBuilder result = new StringBuilder();
while ((ln = input.readLine()) != null){
result.append(ln);
result.append("\n");
}
input.close();
ir.close();
return result.toString();
}
catch(Exception e){
e.printStackTrace();//log
return null;
}
}
public static boolean isLinux(){
String os = System.getProperty("os.name").toLowerCase();
if(null != os && os.contains("windows")){
return false;
}
return true;
}
1
public static String runShell(String command){
2
try{
3
String[] commands = isLinux()?new String[]{"/bin/sh","-c",command}:new String[]{"cmd","/c",command};
4
Process process = Runtime.getRuntime().exec(commands);
5
InputStreamReader ir = new InputStreamReader(process.getInputStream());
6
LineNumberReader input = new LineNumberReader(ir);
7
String ln;
8
StringBuilder result = new StringBuilder();
9
while ((ln = input.readLine()) != null){
10
result.append(ln);
11
result.append("\n");
12
}
13
input.close();
14
ir.close();
15
return result.toString();
16
}
17
catch(Exception e){
18
e.printStackTrace();//log
19
return null;
20
}
21
}
22
public static boolean isLinux(){
23
String os = System.getProperty("os.name").toLowerCase();
24
if(null != os && os.contains("windows")){
25
return false;
26
}
27
return true;
28
}
最新文章
- 凭吊一下ASP.NET 5,然后跨平台,越跨越开心
- CI框架入门1
- ViewPager
- shell脚本实现拷贝大文件显示百分比的代码分享
- Python入门笔记(9):元组
- ajax提交特殊字符的处理
- wp控件
- Build AssetBundle, missing shader.
- cocos2d-x之 CCSpriteBatchNode 用法总结
- linux 已有目录挂载磁盘
- Jenkins 十二: 集成 selenium 测试
- Broadcom网卡linux系统下无法连接到网络问题(某种情况- -||)的解决办法
- flume 自己定义 hbase sink 类
- python学习笔记之一:列表与元组
- jeecg入门操作—一对多表单开发
- Android Studio多渠道打包(二)
- Coding语言强弱类型且动静态类型简单解析。附图解
- [蓝点zigBee] CC2530 实用教程总览
- Django 操作Mysql数据库
- 关于新版OPENWRT拔PPTP的619错或PPTPD无法连接问题笔记