13

How do I set the ant bootclasspath in conjunction with -source 1.5 -target 1.5?

How can this not be a hardcoded path to the 1.5 JDK? Can I set an environment variable to bootclasspath similar to how JAVA_HOME can be used from ant?

Ideally I would like to do something like set an environment variable or pass an argument to ant.

initialZero
  • 6,197
  • 9
  • 29
  • 38

2 Answers2

17

Here's an illustration of how you might fetch the Java 5 boot classes location from an environment variable, then use it.

First, set the environment variable - say JAVA5_BOOTCLASSES. The property task gives you access to the environment, then the bootclasspath argument of the javac task passes the setting to the compiler.

<property environment="env" />
<property name="java5.boot.classpath" value="${env.JAVA5_BOOTCLASSES}" />

<javac source="1.5" target="1.5"
       bootclasspath="${java5.boot.classpath}"
       ...
/>

Note that if the environment variable is not set, Ant will ignore it and proceed without - so the compiler will fall back to the default boot classpath.

Another option, if appropriate, is to switch off the warnings, and not bother with the bootclasspath. Something like

<javac srcdir= ... >
    <compilerarg arg="-Xlint:-options" />
</javac>

But that's likely to expose you to some subtle bugs.

martin clayton
  • 76,436
  • 32
  • 213
  • 198
  • You might would like to add executable property in javac task to point to JDK1.7 executable for making it more clearer – Manish Singh Sep 11 '11 at 10:21
  • 1
    This is a really helpful answer but I'm not sure what to set `JAVA5_BOOTCLASSES` to if I'm building a jar for distribution. Wouldn't that require me to know paths on the machines of anyone who ever runs the jar? – nickform Jul 25 '22 at 08:33
  • 1
    @nickform - hopefully not. The source/target/bootclasspath options are there for building backwards-compatible classes. Once deployed the JAR should work anywhere, as long as the minimum version criterion is met. Beyond that the bootclasspath at run time should not matter at build time. – martin clayton Jul 25 '22 at 10:32
4

It's worth noticing that variable JAVA5_BOOTCLASSES should contain all needed libraries not just rt.jar. In my case it was also jce.jar So it's good to set this variable using this simple snippet when in *nix environment:

export JAVA5_BOOTCLASSES=""
for i in /usr/lib/jvm/java/jre/lib/*.jar; do 
    export JAVA5_BOOTCLASSES=$JAVA5_BOOTCLASSES:$i
done
soltysh
  • 1,464
  • 12
  • 23