Java笔记-使用processBuilder调用shell(Linux)

Posted IT1995

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java笔记-使用processBuilder调用shell(Linux)相关的知识,希望对你有一定的参考价值。

背景

环境是这样的,前端发起一个post请求,里面的信息带有要启动的进程,java后端接收到这个进程名后,使用linux的shell调用指定的程序。

代码

相关代码如下:

package xx.xx.xx.service;

import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.Semaphore;

@Service
public class ProcessService 

    static Boolean isSomeProcessRun = false;
    Semaphore semaphore = new Semaphore(1);

    public String runProcess(String processName)

        System.out.println(isSomeProcessRun);
        if(isSomeProcessRun)

            return "有程序正在计算," + processName + " 无法启动";
        

        try 

            isSomeProcessRun = true;
            semaphore.acquire();
            run(processName);
        
        catch (InterruptedException e) 

            e.printStackTrace();
        

        return processName + "后台已调用";
    

    protected void run(String processName)

        Thread thread = new Thread()

            @Override
            public void run() 

                ProcessBuilder processBuilder = new ProcessBuilder();
                processBuilder.command("/root/program/" + processName);
                processBuilder.redirectErrorStream(true);
                try 

                    Process start = processBuilder.start();
                    InputStream inputStream = start.getInputStream();
                    InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "gbk");

                    char[] chs = new char[1024];
                    int len = -1;
                    while ((len = inputStreamReader.read(chs)) != -1)

                        String str = new String(chs, 0, len);
                        System.out.println(str);
                    

                    inputStreamReader.close();
                    inputStream.close();
                
                catch (IOException e) 

                    e.printStackTrace();
                

                semaphore.release();
                isSomeProcessRun = false;
            
        ;
        thread.start();
    

这里有几点要注意的:

调用shell的关键代码:

Thread thread = new Thread()

    @Override
    public void run() 

        ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.command("/root/program/" + processName);
        processBuilder.redirectErrorStream(true);
        try 

            Process start = processBuilder.start();
            InputStream inputStream = start.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "gbk");

            char[] chs = new char[1024];
            int len = -1;
            while ((len = inputStreamReader.read(chs)) != -1)

                String str = new String(chs, 0, len);
                System.out.println(str);
            

            inputStreamReader.close();
            inputStream.close();
        
        catch (IOException e) 

            e.printStackTrace();
        

        semaphore.release();
        isSomeProcessRun = false;
    
;

启动一个进程,让这个进程调用shell,用这种方式来做,比较合理。

以上是关于Java笔记-使用processBuilder调用shell(Linux)的主要内容,如果未能解决你的问题,请参考以下文章

Java使用ProcessBuilder类调用外部程序

[Java][Android][Process] ProcessBuilder与Runtime区别

Java笔记:运行时

Java/python 使用 processBuilder

java操作进程

在 Java 中使用 ProcessBuilder 的问题