Runtime.getRuntime().exec(

Hi,

Based on XML-files, my webservice generates java sourcecode.

If I compile this code manually, it compiles without errors.

However if the service tries to compile it, the spawned process returns with exitvalue() 1

String os_name = System.getProperty("os.name");

String shell = null;

String shellParam = "";

String classPad = java.lang.System.getProperty("java.class.path");

if (os_name.startsWith("Windows")) {

if ( (System.getProperty("os.name").endsWith("NT"))

|| (System.getProperty("os.name").endsWith("2000"))

||(System.getProperty("os.name").endsWith("XP")) ) {

shell = "cmd.exe";

} else {

shell = "command.com";

}

shellParam = "/C";

}

String [] commando = {shell,

shellParam,

"javac",

"-Djava.ext.dirs=" +startPad +File.separator+ "WEB-INF" +File.separator+ "lib",

"-classpath \"" +classPad+"\"",

"-verbose",

File.separator +"minlnv" + File.separator +"pdf" + File.separator + srcStub};

String [] commandEnv = {"JAVA_HOME=\"C:" +File.separator+ "Program Files" +File.separator+ "Java" +File.separator+ "jdk1.5.0_06\""};

Process p = Runtime.getRuntime().exec(commando, commandEnv, new File(startPad));

try {

p.waitFor();

System.out.println("outPut exec.ret " + p.exitValue());

} catch (InterruptedException e) {

System.out.println(e.getMessage());

int ret = p.exitValue();

e.printStackTrace();

}

[1519 byte] By [BenEngbersa] at [2007-10-2 12:32:20]
# 1
Hi,It is only now that I see that I have forgotten to write down my question.Why does the spawned process return with exitcode 1 and how can I intercept stderr (I want to know what is going wrong)Ben
BenEngbersa at 2007-7-13 9:31:16 > top of Java-index,Developer Tools,Java Compiler...
# 2
You might want to read this http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
aniseeda at 2007-7-13 9:31:16 > top of Java-index,Developer Tools,Java Compiler...
# 3
Thanks,I'll take a good look at it and let yoy know if it helped me.Ben
BenEngbersa at 2007-7-13 9:31:16 > top of Java-index,Developer Tools,Java Compiler...
# 4

Yep,

After implementing the code from MediocreExecJavac.java, it proved that javac was not found in the path. I am now trying to solve that problem but since I have installed more jdk's in Eclipse, that leads me to the next problem.

At least now I know why it doesn't work (yet)

Thanks

BenEngbersa at 2007-7-13 9:31:16 > top of Java-index,Developer Tools,Java Compiler...
# 5

Hi,

It took some time before I figured out how I should add -classpath and/or -extdirs to the commando [] but the following code is working:

Ben

String os_name = System.getProperty("os.name");

String shell = null;

String shellParam = "";

String classPad = java.lang.System.getProperty("java.class.path");

String javaHome = java.lang.System.getProperty("java.home");

String javacHome = javaHome.replaceAll("Program Files","PROGRA~1").replaceAll("jre","bin");

String libPath = javaHome.replaceAll("jre","lib");

System.out.println("javacHome = " + javacHome);

if (os_name.startsWith("Windows")) {

if ( (System.getProperty("os.name").endsWith("NT"))

|| (System.getProperty("os.name").endsWith("2000"))

||(System.getProperty("os.name").endsWith("XP")) ) {

shell = "cmd.exe";

} else {

shell = "command.com";

}

shellParam = "/C";

}

String [] commando = {

shell,

shellParam,

javacHome + File.separator + "javac",

"-verbose",

"-classpath",

classPad,

"-extdirs",

startPad + File.separator + "WEB-INF" + File.separator + "lib",

startPad + File.separator + "minlnv" + File.separator +"pdf" + File.separator + srcStub

};

for (int tel=0; tel < commando.length; tel++) {

System.out.println(commando[tel]);

}

System.out.println();

try {

Process p = Runtime.getRuntime().exec(commando, null, new File(startPad));

..........

BenEngbersa at 2007-7-13 9:31:16 > top of Java-index,Developer Tools,Java Compiler...