tags:

views:

4832

answers:

4

The java.library.path property appears to be read-only. For example when you run ant on the following buildfile

<project name="MyProject" default="showprops" basedir=".">
    <property name="java.library.path" value="test"/>
    <property name="some.other.property" value="test1"/>
    <target name="showprops">
        <echo>java.library.path=${java.library.path}</echo>
        <echo>some.other.property=${some.other.property}</echo>
    </target>
</project>

you get

> ant -version
Apache Ant version 1.6.5 compiled on June 2 2005

> ant -Djava.library.path=commandlinedefinedpath
Buildfile: build.xml
showprops:
    [echo] java.library.path=commandlinedefinedpath
    [echo] some.other.property=test1
BUILD SUCCESSFUL
Total time: 0 seconds

The output indicates that the java.library.path hasn't been changed, but some.other.property was set correctly.

I would like to know how to modify the java.library.path within a buildfile. Specifying the java.library.path on the ant command line is not really an easy option, because the library path location is not know at that time.

Note: I would like this to work so that I can specify the location of native libraries used in a unit test.

A: 

I think this is not possible, mainly because the JVM has already started by the time this value is modified.

You can however try to start a new process with the correct env variables ( see exec or ant tasks )

I think what you want is to compute the value of the library at runtime and then use it to run the test. By creating a new process you can have that new process to use the right path.

OscarRyz
+1  A: 

I think you can modify it if you use fork=true in your "java" task. You can supply java.library.path as a nested sysproperty tag.

Yoni Roit
+3  A: 

Ant properties do not work the way you expect: they are immutable, i.e. you cannot change a property's value after you have set it once. If you run

ant -Dsome.other.property=commandlinedefinedpath

the output will no longer show

[echo] some.other.property=test1

Peter Hilton
A: 

If you really want to change a property, you can do this in a Java task or in a scripting language.

Here is an example using Groovy:

<?xml version="1.0"?>
 <project name="example" default="run">
 <taskdef name="groovy"
          classname="org.codehaus.groovy.ant.Groovy"
          classpath="lib/groovy-all-1.1-rc-1.jar"/>


 <target name="run">
   <echo>java.library.path = ${java.library.path}</echo>
   <groovy>
     properties["java.library.path"] = "changed"
    </groovy>
    <echo>java.library.path = ${java.library.path}</echo>
  </target>
</project>

Caution, this violates Ant's "immutable property" property. Use at your own risk.

flicken