我想从我的java /WEB-INF/lib
运行位于servlet
的可执行jar并获取输出。
尝试过:
Process procesSH;
BuffererdReader br = null;
ProcessBuilder pb = new ProcessBuilder("/WEB-INF/lib/jarName.jar", "-jar", "parma01 parma02");
procesSH = pb.start(); // start pb
br = BuffererdReader(new InputStreamReader(procesSH.getInputStream()));
system.out.println(br.readLine()); // read just one line for now
我得到的错误是:
CreateProcess error = 2, system cannot find file specified.
该文件位于/WEB-INF/lib
下,如果需要更多信息,我会发布。
感谢。
答案 0 :(得分:1)
路径/WEB-INF/lib/jarName.jar
显然不起作用,这是通过ServletContext
获取jar的绝对路径的方法:
ServletContext context = ...
String jarpath = context.getRealPath("/WEB-INF/lib/jarName.jar");
ProcessBuilder pb = new ProcessBuilder(jarpath, "-jar", "parma01 parma02");