ProcessBuilder 不使用通配符执行命令
ProcessBuilder not executing command with wildcard
我正在尝试编写一个实用程序 java,它在 unix 框中调用 perl 脚本并显示脚本的输出。问题是当我执行 command2
、
String[] command2 = {"/archive/scripts/grep.pl", "/apps/ws/logs/api.log"};
输出正确。但是当我使用 command1
,
String[] command1 = {"/archive/scripts/grep.pl", "/apps/ws/logs/*"};
我收到以下异常:
Can't open /apps/ws/logs/*: No such file or directory at /archive/scripts/grep.pl line 160.
完整代码如下,供您参考:
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();
我无法理解问题所在。有什么办法可以做到这一点。
使用命令shell解释通配符
String[] command1 = {"bash", "-c", "/archive/scripts/grep.pl /apps/ws/logs/*"};
我正在尝试编写一个实用程序 java,它在 unix 框中调用 perl 脚本并显示脚本的输出。问题是当我执行 command2
、
String[] command2 = {"/archive/scripts/grep.pl", "/apps/ws/logs/api.log"};
输出正确。但是当我使用 command1
,
String[] command1 = {"/archive/scripts/grep.pl", "/apps/ws/logs/*"};
我收到以下异常:
Can't open /apps/ws/logs/*: No such file or directory at /archive/scripts/grep.pl line 160.
完整代码如下,供您参考:
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();
我无法理解问题所在。有什么办法可以做到这一点。
使用命令shell解释通配符
String[] command1 = {"bash", "-c", "/archive/scripts/grep.pl /apps/ws/logs/*"};