java执行linux命令

本文介绍了在Java中如何使用Runtime.exec方法执行Linux命令,包括通过CommandUtil工具类进行命令执行和输出捕获。然而,由于exec方法对特殊字符支持有限,如遇到|>&gt等,需通过/bin/sh来执行命令。需要注意的是,频繁调用Java执行Linux命令可能影响性能,应谨慎使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近工作中需要在Java中执行Linux命令,对遇到的坑做一个小结。

API

img

把要执行的命令作为exec方法的参数,返回一个Process对象代表命令执行的进程。由于执行完命令通常要获取输出显示出来,因此对执行命令并获取输出的过程封装为一个工具类:

CommandUtil

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;



    }



}

示例:

执行启动tomcat的脚本:

img

执行结果:

img

执行带特殊字符的命令

exec方法无法执行带 | > 等特殊字符的命令,如 ps -ef | grep java 。

此时要把整个命令作为 /bin/sh 的参数执行,如:

img

注意

exec方法通过创建一个子进程执行Linux命令,因此频繁地通过Java调用Linux命令对性能影响非常大,不要在程序中过度使用这种方式。

以上就是良许教程网为各位朋友分享的Linux相关知识。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值