流程构建器问题

发布于 2024-08-18 18:48:43 字数 1219 浏览 3 评论 0原文

我正在开发一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城空念 2024-08-25 18:48:43

我猜你应该让类名和参数使用不同的字符串。而不是:

"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"

它应该是

"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"

另一件值得注意的事情是您使用完整路径名来调用 java.exe,但在使用 ProcessBuilder 时则不然。

您可能还想读取错误流(或调用redirectErrorStream(true) - 参数很重要(!))。

I'm guessing that you should make your class name and parameters different strings. instead of:

"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"

It should be

"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"

The other noticeable thing that you used the full path name to call java.exe, but not when you used ProcessBuilder.

You might also want to read the error stream (or call redirectErrorStream(true) - the argument is important(!)).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文