1

I am trying to write an utility java program that calls a perl script in unix box and shows the output of the script. The issue is when I am executing command2,

String[] command2 = {"/archive/scripts/grep.pl", "/apps/ws/logs/api.log"};

the output is coming correctly. But when i am using command1,

String[] command1 = {"/archive/scripts/grep.pl", "/apps/ws/logs/*"};

i am getting the below exception:

Can't open /apps/ws/logs/*: No such file or directory at /archive/scripts/grep.pl line 160.

Below is the full code for your reference:

    StringBuffer output = new StringBuffer();
    try {
        ProcessBuilder processBuilder = new ProcessBuilder(command1);
        LOGGER.debug("Command: "+processBuilder.command());
        Process process = processBuilder.start();
        process.waitFor();
        BufferedReader br;
        if (process.exitValue() == 0) {
            LOGGER.debug("Inside if block. Exit value: "+process.exitValue());
            String line;
            br = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            while ((line = br.readLine()) != null) {
                output.append(line + "\n");
            }
        } else {
            LOGGER.debug("Inside Else block. Exit value: "+process.exitValue());
            br = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = br.readLine()) != null) {
                output.append(line + "\n");
            }
        }
    } catch (Exception e) {
        LOGGER.debug("Exception thrown"+e.getStackTrace());
        output.append(e.getStackTrace().toString());
        e.printStackTrace();
    }
    return output.toString();

I am not able to understand what is the issue. Is there any way to accomplish this.

Subhash
  • 63
  • 1
  • 6
  • Is there a file with the name of `*` in that directory? Filemasks are resolved by shells. If you execute a script directly, the shell is left out... – Usagi Miyamoto Aug 01 '17 at 09:55
  • there is no file by the name `*`. When i run the command `/archive/scripts/grep.pl /apps/ws/logs/*` in the box, i get proper output. – Subhash Aug 01 '17 at 09:58

1 Answers1

4

Use a command shell to interpret the wildcard

String[] command1 = {"bash", "-c", "/archive/scripts/grep.pl /apps/ws/logs/*"};
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • @Reimus Thanks for the suggestion. I tried the code you suggested. However, I am not getting the expected output. Though the error is not coming any more. Also noticed that the exitValue is coming as `255`. – Subhash Aug 01 '17 at 10:16
  • Check the error stream from `ProcessBuilder`. What happens when you run the command on the command line? – Reimeus Aug 01 '17 at 10:20
  • I get the below output: `Scanning...Overall found 0 entries` – Subhash Aug 01 '17 at 10:22
  • In the error stream I am getting this message `Usage: ./grep.pl [files to scan] e.g. ./grep.pl /apps/ws/logs/*` which is a comment inside the script like `die "Usage: ./grep.pl [files to scan]\ne.g. ./grep.pl /apps/tkt-ws/logs/*" unless defined @ARGV;` – Subhash Aug 01 '17 at 10:34
  • The `logs` directory is not empty and it will never be empty. – Subhash Aug 01 '17 at 10:52
  • 1
    The command needs to be interpreted as a whole by the shell as shown in the update... – Reimeus Aug 02 '17 at 00:26