views:

158

answers:

2

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.

+1  A: 

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

Tom Hawtin - tackline
A: 

Thanks, for you quick response. Now I have changed the code and its working.

Sameer