-2

I'm trying to automate a process, one of which requires doing an ant build. I've set up all the environment variables. Inputting commands directly into cmd works:

my build.xml has some properties like these:

<property name="java.home" value="${env.JAVA_HOME}"/>
<property name="java.compiler.path" value="${JAVA_HOME}\bin\javac"/>
<echo> ${java.home} </echo>
<echo> ${java.compiler.path} </echo>

and calling "ant" on the cmd will echo the directory of the JDK. However, calling ant through ProcessBuilder

 CommandUtils.execute("\"c:\\Program Files (x86)\\Ant\\bin\\ant\"");
 //inside execute() { pb = new ProcessBuilder("cmd", "/c", command);....}

will echo ${java.home} and ${java.compiler.path} literally. Is there any change I can make to point to the java path so that ProcessBuilder will recognize the java path?

user3758745
  • 777
  • 1
  • 6
  • 19
  • Where does it say that `ProcessBuilder` substitutes environment variables? – user207421 Nov 20 '15 at 21:44
  • What about the main question? I don't think that's the cause of the problem to the build.xml when I do the ant. ${JAVA_HOME} echo the right directory but ${env.JAVA_HOME} echo ${env.JAVA_HOME}. So I can't run ant build at all through ProcessBuilder? – user3758745 Nov 20 '15 at 21:57
  • Thats because your java home points to JRE not JDK so no compiler (in tools.jar) is found on the classpath – Jan Nov 20 '15 at 21:59
  • I edited the question. ProcessBuilder points to a completely different lib directory. Running ant from terminal still points to JRE of jdk1.6.0_21 but it build successfully – user3758745 Nov 20 '15 at 22:32
  • The main question is precisely why you are expecting undocumented behaviour. There is no other problem here to solve. The class you mention is working as documented. – user207421 Nov 20 '15 at 22:45
  • And **don't edit out your code**. Without code, this is not a programming question at all and is off topic. – user207421 Nov 20 '15 at 22:52
  • There is no code, I just want to call Ant through ProcessBuilder, but calling Ant through it behaves differently than calling than through CMD. The only piece of code is "CommandUtils.execute("\"c:\\Program Files (x86)\\Ant\\bin\\ant\"");" which is "pb = new ProcessBuilder("cmd", "/c", command);" which is already listed. Which are of course useless to people who're knowledgable enough to answer the question about ProcessBuilder in the first place. I edited the first problem because the example about %path% is bad and has nothing to do with the real question I want to ask, which is about Ant. – user3758745 Nov 20 '15 at 23:10
  • **Don't edit my edit**. It's edited to keep people like you away. This is not a coding question but it **IS** a programming question. No where did I say ProcessBuilder is not working as documented. – user3758745 Nov 20 '15 at 23:24

2 Answers2

1

Substitution of environment variables with % will not work. Use values from System.getenv () instead.

For the ant error: you need a compiler in your classpath. Tools.jar is not included in JRE only in JDK

Jan
  • 13,738
  • 3
  • 30
  • 55
  • What about the main question? I don't think that's the cause of the problem to the build.xml when I do the ant. ${JAVA_HOME} echo the right directory but ${env.JAVA_HOME} echo ${env.JAVA_HOME}. So I can't run ant build at all through ProcessBuilder? – user3758745 Nov 20 '15 at 21:57
1

Use ProcessBuilder to set the JAVA_HOME environment variable and execute the command.

CommandUtils.execute:

ProcessBuilder pb = new ProcessBuilder();
// copy the java.home variable from the current Java process (or set your own)
pb.environment().put("JAVA_HOME", System.getProperty("java.home"));
pb.command("cmd", "/c", command);

Process process = pb.start();
R. Oosterholt
  • 7,720
  • 2
  • 53
  • 77