请问 Java Runtime.getRuntime().exec(command) 有没有办法得到执行的结果

2020-07-02 17:17:11 +08:00
 rqxiao

比如去 ftp 上下载一个压缩文件,用 Runtime.getRuntime().exec(command)去解压

得到解压成功的结果后 ,再去读取文件

有时候 读文件的时候,文件还没有解压完.........

1344 次点击
所在节点    程序员
6 条回复
lff0305
2020-07-02 17:41:45 +08:00
Process p = Runtime.getRuntime().exec(.....);
p.waitFor();
wujieyuan
2020-07-02 17:44:01 +08:00
public static CommandResult exec(String cmd) {
int result = -1;
BufferedReader successResult = null;
BufferedReader errorResult = null;
StringBuilder successMsg = null;
StringBuilder errorMsg = null;
try {
Process process = Runtime.getRuntime().exec(cmd);
result = process.waitFor();
successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String s;
successMsg = new StringBuilder();
while ((s = successResult.readLine()) != null) {
successMsg.append(s).append("\n");
}
errorMsg = new StringBuilder();
while ((s = errorResult.readLine()) != null) {
errorMsg.append(s).append("\n");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (successResult != null)
successResult.close();
if (errorResult != null)
errorResult.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String success = successMsg == null ? "" : successMsg.toString();
if (success.endsWith("\n"))
success = success.substring(0, success.length() - 1);
String error = errorMsg == null ? "" : errorMsg.toString();
if (error.endsWith("\n"))
error = error.substring(0, error.length() - 1);
return new CommandResult(result, success, error);
}
jjianwen68
2020-07-02 17:46:14 +08:00
hutool 封装的一些小工具还是很方便的 RuntimeUtil
DoodleSit
2020-07-02 17:50:31 +08:00
```
private static Logger logger = LoggerFactory.getLogger(ShellCmd.class.getName());

private static final String COMMAND_SH = "sh";
private static final String COMMAND_LINE_END = "\n";
private static final String COMMAND_EXIT = "exit\n";
private static ExecutorService pool;

private static Process process(String command) {
Runtime rt = Runtime.getRuntime();
Process p = null;
try {
p = rt.exec(command);
} catch (IOException e) {
e.printStackTrace();
p = null;
}
return p;
}

private static void initPool() {
if (pool != null) {
pool.shutdownNow();
pool = null;
}
pool = Executors.newCachedThreadPool();
}

public static String exec(String command) {
if (RegexHelper.isEmpty(command)) return "nil";
int runningStatus = -1;
try {
initPool();
Process process = process(COMMAND_SH);
try {
DataOutputStream dos = new DataOutputStream(process.getOutputStream());
dos.write(command.getBytes());
dos.writeBytes(COMMAND_LINE_END);
dos.flush();
dos.writeBytes(COMMAND_EXIT);
dos.flush();
dos.close();
InputStream err = process.getErrorStream();
InputStream normal = process.getInputStream();
List<Future<String>> futureList = new ArrayList<>();
futureList.add(pool.submit(new StreamReaderWorker(normal, StreamType.NORMAL)));
futureList.add(pool.submit(new StreamReaderWorker(err, StreamType.ERROR)));
runningStatus = process.waitFor();
for (Future<String> future : futureList) {
try {
String str = future.get();
logger.warn(str);
} catch (ExecutionException e) {
e.printStackTrace();
}
}
process.destroy();
} catch (InterruptedException e) {
e.printStackTrace();
}

} catch (IOException e) {
e.printStackTrace();
}
return "" + runningStatus;
}
```

很早之前写的,用着没啥问题
DoodleSit
2020-07-02 17:52:26 +08:00
public interface StreamType {
static int NORMAL = 1;
static int ERROR = 2;
}

public class StreamReaderWorker implements StreamType, Callable<String> {

private int type;
private InputStream is;

public StreamReaderWorker(InputStream is, int type) {
this.is = is;
this.type = type;
}


@Override
public String call() throws Exception {
if (is == null) return null;
BufferedReader br = null;
InputStreamReader isr = null;
boolean isReadSth = false;
try {
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String line = null;
StringBuffer stringBuffer = new StringBuffer();
while ((line = br.readLine()) != null) {
isReadSth = true;
stringBuffer.append(line);
}
return stringBuffer.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) br.close();
if (isr != null) isr.close();
} catch (IOException e) {
e.printStackTrace();
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return "";
}


}
misaka19000
2020-07-02 17:58:23 +08:00
搜索引擎能搜到的对象为什么还要问一遍呢?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/686618

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX