我想通过srcML解析器解析某些文件的内容,这是一个外部窗口程序。我做这件事的方式如下:
String command = "src2srcml.exe --language java";
Process proc = Runtime.getRuntime().exec(command);
InputStream fileInput = Files.newInputStream(file)
OutputStream procOutput = proc.getOutputStream();
IOUtils.copy(fileInput, procOutput);
IOUtils.copy()来自CommonsIO2.4库。
当我的文件很小(几KB)时,一切都正常。但是,当我试图复制一些比较大的文件(~72 KB)时,我的程序会挂起。
此外,当我在cmd中“手动”执行解析器时:
src2srcml.exe --language Java < BigFile.java
一切都很好。
知道为什么会这样吗?
发布于 2014-07-30 14:20:06
您应该缓冲OutputStream:
OutputStream procOutput = proc.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(procOutput);
IOUtils.copy(fileInput, bos);
此外,为什么不简单地将fileInput重定向为流程InputStream?
ProcessBuilder pb = new ProcessBuilder(command);
pb.redirectInput(file);
Process proc = pb.start();
proc.waitFor();
发布于 2014-07-30 14:35:11
问题很可能是,您没有在单独的线程中使用外部程序的输出。您需要启动一个单独的线程来使用输出,这样外部程序就不会被阻塞。
https://stackoverflow.com/questions/25038912
复制相似问题