Java执行Linux命令

简介: Java执行Linux命令
package org.ml.deployer.util;
 
import java.io.IOException;
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;
    }
}
相关文章
|
存储 运维 监控
“df -i” 以inode模式来显示磁盘使用情况--这是什么意思?
“df -i” 以inode模式来显示磁盘使用情况--这是什么意思?
1230 0
|
Java 数据库连接 Nacos
Nacos2.2.3支持达梦
Nacos2.2.3支持达梦
2223 2
|
Java 应用服务中间件 Maven
SpringBoot Maven 项目打包的艺术--主清单属性缺失与NoClassDefFoundError的优雅解决方案
SpringBoot Maven 项目打包的艺术--主清单属性缺失与NoClassDefFoundError的优雅解决方案
1676 0
|
数据库 数据库管理 关系型数据库
|
前端开发 Java Spring
掌握@ControllerAdvice配合RequestBodyAdvice/ResponseBodyAdvice使用,让你的选择不仅仅只有拦截器【享学Spring MVC】(中)
掌握@ControllerAdvice配合RequestBodyAdvice/ResponseBodyAdvice使用,让你的选择不仅仅只有拦截器【享学Spring MVC】(中)
掌握@ControllerAdvice配合RequestBodyAdvice/ResponseBodyAdvice使用,让你的选择不仅仅只有拦截器【享学Spring MVC】(中)
|
Linux 开发工具
详尽分享离线安装telnet
详尽分享离线安装telnet
2913 3
|
安全 关系型数据库 MySQL
MySQL非root安装-初始化数据库时unknown variable ‘defaults-file=**/my.cnf‘
解决安装过程中出现的问题通常需要仔细地检查错误日志、配置文件和执行命令,保证各项配置设置的精确无误是顺利完成安装的关键。通过上述的步骤分析和解决方案,非root用户安装MySQL时遇到"unknown variable 'defaults-file=**/my.cnf'"的问题应该可以得到妥善的解决。
1404 0
|
前端开发 JavaScript
Layui上传文件时choose事件只触发一次的问题(两种解决方案+最终解决方案源码)
Layui上传文件时choose事件只触发一次的问题(两种解决方案+最终解决方案源码)
913 0
|
消息中间件 Java Spring
Springboot 集成Rabbitmq之延时队列
Springboot 集成Rabbitmq之延时队列
608 0
|
消息中间件 Shell 数据库
RabbitMQ之延迟消息
RabbitMQ之延迟消息
399 0