编程语言
首页 > 编程语言> > 从Java程序运行grep

从Java程序运行grep

作者:互联网

我花了3天时间在谷歌上没有太多运气如何从Java中运行grep进程.

我有以下代码来运行grep进程,但是,我只获得响应的第一行.

package com.example.parser;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) {
        try {
            Process process = new ProcessBuilder("grep", "-rni", "\"public static void main(\"", "/home/user/dev/java/").start();

            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));

            String line = "";
            while((line = br.readLine()) != null) {
                System.out.println(line);
            }

            System.out.println("Exit Code: " + process.exitValue());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

我只收到以下回复:

Binary file /home/user/dev/java/Parser/parser/bin/com/example/parser/Main.class matches
Exit Code: 0

当我得到以下回复时:

Binary file /home/user/dev/java/Parser/parser/com/example/parser/Main.class matches
/home/user/dev/java/Parser/parser/src/com/example/parser/Main.java:10:  public static void main(String[] args) {
/home/user/dev/java/Parser/parser/src/com/example/parser/Main.java:12:          Process process = new ProcessBuilder("grep", "-rni", "\"public static void main(\"", "/home/user/dev/java/Parser/parser").start();
Exit Code: 0

我想知道为什么我只得到第一个发现的输出?是grep分叉几个进程来运行搜索,我只是在第一个处理?

我也试过从线程运行进程:

package com.example.parser;

public class Main {

    public static void main(String[] args) {
        try {
            Analyzer analyzer = new Analyzer();
            analyzer.start();
            analyzer.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}


package com.example.parser;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Analyzer extends Thread {

    public Analyzer() {
    }

    @Override
    public void run() {
        try {
            Process process = new ProcessBuilder("grep", "-rni", "\"public static void main(\"", "/home/user/dev/java/Parser/parser").start();
            process.waitFor();
            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));

            String line = "";
            while((line = br.readLine()) != null) {
                System.out.println(line);
            }

            System.out.println("Exit Code: " + process.exitValue());

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

以及以下内容:

package com.example.parser;

import java.io.IOException;

public class Main {

    public static void main(String[] args) {
        try {
            Process process = new ProcessBuilder("grep", "-rni", "\"public static void main(\"", "/home/user/dev/java/Parser/parser").start();
            process.waitFor();

            Analyzer analyzer_is = new Analyzer(process.getInputStream());
            Analyzer analyzer_es = new Analyzer(process.getErrorStream());

            analyzer_is.start();
            analyzer_es.start();

            analyzer_is.join();
            analyzer_es.join();

            System.out.println("Exit Code: " + process.exitValue());

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

package com.example.parser;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Analyzer extends Thread {

    InputStream is = null;

    public Analyzer(InputStream is) {
        this.is = is;
    }

    @Override
    public void run() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(this.is));

            String line = "";
            while((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

如下文所述:http://www.javaworld.com/jw-12-2000/jw-1229-traps.html

解决方法:

我能够通过启动带-c标志的shell来解决问题.以下代码执行我最初的预期:

package com.example.parser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        try {
            List<String> commands = new ArrayList<String>();
            commands.add("/bin/sh");
            commands.add("-c");
            commands.add("grep -rni --include \"*.java\" \"public static void main(\" /home/user/dev/java/Parser/parser");

            Process process = new ProcessBuilder(commands).start();
            Analyzer analyzer_is = new Analyzer(process.getInputStream());
            Analyzer analyzer_es = new Analyzer(process.getErrorStream());

            analyzer_is.start();
            analyzer_es.start();

            process.waitFor();

            analyzer_is.join();
            analyzer_es.join();

            System.out.println("Exit Code: " + process.exitValue());

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}


package com.example.parser;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Analyzer extends Thread {

    InputStream is = null;

    public Analyzer(InputStream is) {
        this.is = is;
    }

    @Override
    public void run() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(this.is));

            String line = "";
            while((line = br.readLine()) != null) {
                  System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

标签:java,linux,grep,process,processbuilder
来源: https://codeday.me/bug/20190629/1330571.html