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.