从java servlet运行可执行jar文件

时间:2016-04-13 08:08:37

标签: java servlets jar

我想从我的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下,如果需要更多信息,我会发布。

感谢。

1 个答案:

答案 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");