开发者

Process Builder Issue

开发者 https://www.devze.com 2022-12-17 05:23 出处:网络
I am working on a java program where I have to call another java program using process builder because it uses a separate JRE.

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.MainProgramLaunche开发者_如何学Pythonr 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.


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(!)).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号