流程构建器问题
我正在开发一个java程序,我必须使用进程生成器调用另一个java程序,因为它使用单独的JRE。
这是调用该程序并正常工作的批处理程序:
cd C:\apps\Project_name\bin\
C:\Progra~1\Java\jre1.6.0_03\bin\java -Xms512m -Xmx1024m
-cp ../lib/spring-1.2.2.jar;../lib/log4j-1.2.8.jar;../lib/commons-logging-1.0.4.jar;../lib/wrapper.jar
com.project.main.MainProgramLauncher arg1 arg2
现在我声明了我的代码,如下所示:
ProcessBuilder builder = new ProcessBuilder(
"java",
"-Xms512m",
"-Xmx1024m",
"-cp ../lib/spring-1.2.2.jar;../lib/log4j-1.2.8.jar;"+
"../lib/commons-logging-1.0.4.jar;../lib/wrapper.jar",
"com.project.main.MainProgramLauncher arg1 arg2 ...argN"
);
Map<String, String> environment = builder.environment();
environment.put("path", ";"); // Clearing the path variable;
environment.put("path", java_bin_location+";");
Process javap = builder.start();
InputStreamReader tempReader = new InputStreamReader(
new BufferedInputStream(javap.getInputStream())
);
BufferedReader reader = new BufferedReader(tempReader);
while (true) {
String line = reader.readLine();
if (line == null)
break;
System.out.println(line);
}
但它没有按应有的方式执行。程序立刻就崩溃了。我应该做些什么不同的事情吗?请建议。
I am working on a java program where I have to call another java program using process builder because it uses a separate JRE.
Here is batch program which calls this program and works properly:
cd C:\apps\Project_name\bin\
C:\Progra~1\Java\jre1.6.0_03\bin\java -Xms512m -Xmx1024m
-cp ../lib/spring-1.2.2.jar;../lib/log4j-1.2.8.jar;../lib/commons-logging-1.0.4.jar;../lib/wrapper.jar
com.project.main.MainProgramLauncher arg1 arg2
Now I declared my code something like this:
ProcessBuilder builder = new ProcessBuilder(
"java",
"-Xms512m",
"-Xmx1024m",
"-cp ../lib/spring-1.2.2.jar;../lib/log4j-1.2.8.jar;"+
"../lib/commons-logging-1.0.4.jar;../lib/wrapper.jar",
"com.project.main.MainProgramLauncher arg1 arg2 ...argN"
);
Map<String, String> environment = builder.environment();
environment.put("path", ";"); // Clearing the path variable;
environment.put("path", java_bin_location+";");
Process javap = builder.start();
InputStreamReader tempReader = new InputStreamReader(
new BufferedInputStream(javap.getInputStream())
);
BufferedReader reader = new BufferedReader(tempReader);
while (true) {
String line = reader.readLine();
if (line == null)
break;
System.out.println(line);
}
But its not executing the way it should be. The program crashing right away. Is there anything different I should be doing? Please suggest.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜你应该让类名和参数使用不同的字符串。而不是:
它应该是
另一件值得注意的事情是您使用完整路径名来调用
java.exe
,但在使用ProcessBuilder
时则不然。您可能还想读取错误流(或调用
redirectErrorStream(true)
- 参数很重要(!))。I'm guessing that you should make your class name and parameters different strings. instead of:
It should be
The other noticeable thing that you used the full path name to call
java.exe
, but not when you usedProcessBuilder
.You might also want to read the error stream (or call
redirectErrorStream(true)
- the argument is important(!)).