怎样将java代码到linux上执行
1个回答
展开全部
以下方法支持Linux和windows两个系统的命令行调用。还用到了apache的lang工具包commons-lang3-3.1.jar来判断操作系统类型、也用到了和log4j-1.2.16.jar来打印日志。至于rm -rf 是否能成功删除文件,可以手动去调用命令行试试。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
private String callCmd(String cmd) throws InterruptedException, UnHandledOSException, ExecuteException {
if(SystemUtils.IS_OS_LINUX){
try {
// 使用Runtime来执行command,生成Process对象
Process process = Runtime.getRuntime().exec(
new String[] { "/bin/sh", "-c", cmd });
int exitCode = process.waitFor();
// 取得命令结果的输出流
InputStream is = process.getInputStream();
// 用一个读输出流类去读
InputStreamReader isr = new InputStreamReader(is);
// 用缓冲器读行
BufferedReader br = new BufferedReader(isr);
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
System.out.println(line);
sb.append(line);
}
is.close();
isr.close();
br.close();
return sb.toString();
} catch (java.lang.NullPointerException e) {
System.err.println("NullPointerException " + e.getMessage());
logger.error(cmd);
} catch (java.io.IOException e) {
System.err.println("IOException " + e.getMessage());
}
throw new ExecuteException(cmd + "执行出错!");
}
if(SystemUtils.IS_OS_WINDOWS){
Process process;
try {
//process = new ProcessBuilder(cmd).start();
String[] param_array = cmd.split("[\\s]+");
ProcessBuilder pb = new ProcessBuilder(param_array);
process = pb.start();
/*process=Runtime.getRuntime().exec(cmd);*/
int exitCode = process.waitFor();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
System.out.println(line);
sb.append(line);
}
is.close();
isr.close();
br.close();
return sb.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
throw new ExecuteException(cmd + "执行出错!");
}
throw new UnHandledOSException("不支持本操作系统");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
private String callCmd(String cmd) throws InterruptedException, UnHandledOSException, ExecuteException {
if(SystemUtils.IS_OS_LINUX){
try {
// 使用Runtime来执行command,生成Process对象
Process process = Runtime.getRuntime().exec(
new String[] { "/bin/sh", "-c", cmd });
int exitCode = process.waitFor();
// 取得命令结果的输出流
InputStream is = process.getInputStream();
// 用一个读输出流类去读
InputStreamReader isr = new InputStreamReader(is);
// 用缓冲器读行
BufferedReader br = new BufferedReader(isr);
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
System.out.println(line);
sb.append(line);
}
is.close();
isr.close();
br.close();
return sb.toString();
} catch (java.lang.NullPointerException e) {
System.err.println("NullPointerException " + e.getMessage());
logger.error(cmd);
} catch (java.io.IOException e) {
System.err.println("IOException " + e.getMessage());
}
throw new ExecuteException(cmd + "执行出错!");
}
if(SystemUtils.IS_OS_WINDOWS){
Process process;
try {
//process = new ProcessBuilder(cmd).start();
String[] param_array = cmd.split("[\\s]+");
ProcessBuilder pb = new ProcessBuilder(param_array);
process = pb.start();
/*process=Runtime.getRuntime().exec(cmd);*/
int exitCode = process.waitFor();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
System.out.println(line);
sb.append(line);
}
is.close();
isr.close();
br.close();
return sb.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
throw new ExecuteException(cmd + "执行出错!");
}
throw new UnHandledOSException("不支持本操作系统");
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询