①Linux编译执行java文件:
vim 在linux下编写名为RunTest.java的文件
通过javac RunTest.java
对该文件进行编译,该目录下生成RunTest.class文件
java RunTest
执行RunTest.class文件
②Linux下执行jar文件方法:
eclipse打包jar:
eclipse——项目export——jar file——Launch configuration中选择要打包的主类(mian方法)——Export destination中设置jar包的存储路径——finish
命令行下进入文件目录,执行java -jar file.jar
③java文件中执行Linux命令方法:
函数:executeLinuxCmd()
函数使用:
executeLinuxCmd(“cmd.exe /c dir”)//在win下测试
executeLinuxCmd(“pwd”)//在linux下测试
函数源码:
private static String executeLinuxCmd(String cmd) {
System.out.println("got cmd job : " + cmd);
Runtime run = Runtime.getRuntime();
try {
Process process = run.exec(cmd);
InputStream in = process.getInputStream();
BufferedReader bs = new BufferedReader(new InputStreamReader(in));
// System.out.println("[check] now size \n"+bs.readLine());
StringBuffer out = new StringBuffer();
byte[] b = new byte[8192];
for (int n; (n = in.read(b)) != -1;) {
out.append(new String(b, 0, n));
}
String result = out.toString();
//System.out.println("job result [" + out.toString() + "]");
in.close();
// process.waitFor();
process.destroy();
return result;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
网友评论