本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《
阿里云开发者社区用户服务协议
》和
《
阿里云开发者社区知识产权保护指引
》。如果您发现本社区中有涉嫌抄袭的内容,填写
侵权投诉表单
进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
import java.io.InputStream;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class CommandUtil {
public static String run(String command) throws IOException {
Scanner input = null;
String result = "";
Process process = null;
try {
process = Runtime.getRuntime().exec(command);
try {
//等待命令执行完成
process.waitFor(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
InputStream is = process.getInputStream();
input = new Scanner(is);
while (input.hasNextLine()) {
result += input.nextLine() + "\n";
result = command + "\n" + result; //加上命令本身,打印出来
} finally {
if (input != null) {
input.close();
if (process != null) {
process.destroy();
return result;
public static String run(String[] command) throws IOException {
Scanner input = null;
String result = "";
Process process = null;
try {
process = Runtime.getRuntime().exec(command);
try {
//等待命令执行完成
process.waitFor(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
InputStream is = process.getInputStream();
input = new Scanner(is);
while (input.hasNextLine()) {
result += input.nextLine() + "\n";
result = command + "\n" + result; //加上命令本身,打印出来
} finally {
if (input != null) {
input.close();
if (process != null) {
process.destroy();
return result;
Java 11 快要来了,编译 & 运行一个命令搞定!
Java 11 马上要来了,原定于 9 月发布,还有不到 3 个月了,敬请期待更多新功能被加入到 11 当中,本文本讲的是 JEP 330 这个新特性。
化繁为简,一个命令编译运行源代码
开发工程师