如何解决Java Runtime.exec使用GZIP返回1
我有以下代码,应提取位于同一目录中的一堆GZ文件。
Process process = Runtime.getRuntime().exec(new String[]{"gzip","-dr",gzipDirectory.getAbsolutePath()});
waitForOrKill(process,30000);
int returnCode = process.exitValue();
if (returnCode == 0) LOG.info("Success");
else throw new Exception("Exit code: " + returnCode);
该命令在我手动执行时(在Linux和Windows上)有效,因此有效。
waitForOrKill
方法非常基本:
public static void waitForOrKill(Process self,long numberOfMillis) {
ProcessRunner runnable = new ProcessRunner(self);
Thread thread = new Thread(runnable);
thread.start();
runnable.waitForOrKill(numberOfMillis);
}
protected static class ProcessRunner implements Runnable {
Process process;
private boolean finished;
public ProcessRunner(Process process) {
this.process = process;
}
public void run() {
try {
process.waitFor();
} catch (InterruptedException e) {
// Ignore
}
synchronized (this) {
notifyAll();
finished = true;
}
}
public synchronized void waitForOrKill(long millis) {
if (!finished) {
try {
wait(millis);
} catch (InterruptedException e) {
// Ignore
}
if (!finished) {
process.destroy();
}
}
}
}
当我尝试使用GZIP
运行以上Runtime
命令时,我得到的退出值为1
,并且文件没有被提取。
我的代码有问题吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。