Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the Java library path from a Java task?

Tags:

java

ant

Is it possible to specify a library path in a java task? Like the equivalent of:

java -Djava.library.path=somedir Whatever
like image 562
Geo Avatar asked Jul 21 '09 13:07

Geo


People also ask

How to set java path in Windows 10?

Go to Environment Variables: Press “New” button in “System variables” section: Type name “JAVA_HOME” and value – your path to Java, in my case, it’s C:\Program Files\Java\jre-9.0.4 and press “OK“. Done. How to set Java path. Find “Path” system variable and press “Edit“.

How do I set the PATH environment variable in Java?

If the PATH environment variable does not exist, click New. In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK. Reopen Command prompt window, and run your java code. From the desktop, right click the My Computer icon.

How do I set java path in Linux terminal?

How to set Java path. Find “Path” system variable and press “Edit“. Press “New” and type “%JAVA_HOME%bin” to add Java path. Done. Check if JAVA_HOME variable is defined. We set Java home and Java path variables, let’s check is it really working. Open command prompt. We can check Java home variable by typing “echo %JAVA_HOME%“.

How to set the path of JDK in Java?

If you are saving the Java source file inside the JDK/bin directory, the path is not required to be set because all the tools will be available in the current directory. However, if you have your Java file outside the JDK/bin folder, it is necessary to set the path of JDK. There are two ways to set the path in Java: Temporary. Permanent.


2 Answers

<propertyset> and <syspropertyset> should be what you are looking for

See also this thread for instance.


You can set them one by one within your java ant task:

<sysproperty key="test.classes.dir" 
             value="${build.classes.dir}"/> 

tedious... or you can pass them down as a block of Ant properties:

<syspropertyset> 
    <propertyref prefix="test."/> 
</syspropertyset> 

You can reference external system properties:

<propertyset id="proxy.settings"> 
    <propertyref prefix="http."/> 
    <propertyref prefix="https."/> 
    <propertyref prefix="socks."/> 
</propertyset> 

and then use them within your java ant task: This propertyset can be used on demand; when passed down to a new process, all current ant properties that match the given prefixes are passed down:

<java>
     <!--copy all proxy settings from the running JVM--> 
     <syspropertyset refid="proxy.settings"/> 
     ...
</java>

I completely missed the fact you were trying to pass java.library.path property!
As mentioned in this thread:

if you try to set its value outside of the java task, Ant ignores it. So I put all properties except for that one in my syspropertyset and it works as expected.

meaning:

<property name="java.library.path" location="${dist}"/>

<propertyset id="java.props">
    <propertyref name="java.library.path"/>
</propertyset>

<target name="debug">
    <java>
        <syspropertyset refid="java.props"/>
    </java>
</target>

will not work, but the following should:

<target name="debug">
    <java>
        <sysproperty key="java.library.path" path="${dist}"/>
    </java>
</target>

(although you might try that with the "fork" attribute set to true if it does not work)
(Note: you cannot modify its value though)

like image 80
VonC Avatar answered Sep 20 '22 20:09

VonC


For JUnit ant task set your java.library.path in section <junit>

<target name="test" depends="build-test">
  <junit printsummary="yes" fork="true">
    <sysproperty key="java.library.path" 
                 path="path/where/your/library/is/located"/>
    <!-- ... -->
  </junit>
</target>

See ant manual, page JUnit, section <sysproperty> for more details.


The rest of this answer are details for beginners.

1. Load your library in your junit fixture

public class MyFeatureTest {

  @Before
  public void load_library_xxxxx() {
    System.loadLibrary("library_name_without_extension");
  }

  @Test
  public void on_that_case_my_feature_does_this() {
    // ...
  }
}

2. Set the java.library.path in your ant script

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="build" name="xxxxxx">

  <!-- ... -->

  <property name="lib_dir" value="path/where/your/library/is/located"/>

  <!-- ... -->

  <target name="test" depends="build-test">
    <mkdir dir="${test_report_dir}" />
    <junit printsummary="yes" fork="true">
      <sysproperty key="java.library.path" path="${lib_dir}"/>
      <classpath>
        <pathelement location="${antlr}" />
        <!-- ... -->
      </classpath>

      <formatter type="xml" />
      <formatter type="plain" />
      <batchtest todir="${test_report_dir}">
        <fileset dir="${test_src_dir}">
          <include name="**/*Test.java" />
        </fileset>
      </batchtest>
    </junit>
  </target>
</project>

3. Use ant option -v to check java.library.path

Search a line like [junit] '-Djava.library.path= within your ant output to check the presence and value of java.library.path. The expression [...] represent text that has been removed for clarity.

> ant test -v
[...]
test:
    [mkdir] Skipping /home/user/my/dir/report because it already exists.
    [junit] Implicitly adding /usr/share/ant/lib/junit.jar:[...] to CLASSPATH
    [junit] Executing '/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java' with arguments:
    [junit] '-Djava.library.path=/home/user/my/project/path/where/your/library/is/located'
    [junit] '-classpath'
    [junit] '/home/user/my/project/external/antlr.jar:[...]'
    [junit] 'org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner'
    [junit] 'com.example.myproject.myfeature.MyFeatureTest'
    [junit] 'skipNonTests=false'
    [junit] 'filtertrace=true'
    [junit] 'haltOnError=false'
    [junit] 'haltOnFailure=false'
    [junit] 'formatter=org.apache.tools.ant.taskdefs.optional.junit.SummaryJUnitResultFormatter'
    [junit] 'showoutput=false'
    [junit] 'outputtoformatters=true'
    [junit] 'logfailedtests=true'
    [junit] 'threadid=0'
    [junit] 'logtestlistenerevents=false'
    [junit] 'formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,/home/user/my/dir/report/TEST-com.example.myproject.myfeature.MyFeatureTest.xml'
    [junit] 'formatter=org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter,/home/user/my/dir/report/TEST-com.example.myproject.myfeature.MyFeatureTest.txt'
    [junit] 'crashfile=/home/user/my/project/junitvmwatcher4952613017772370651.properties'
    [junit] 'propsfile=/home/user/my/project/junit3999929381398716397.properties'
    [junit] 
    [junit] The ' characters around the executable and arguments are
    [junit] not part of the command.
    [...]
like image 37
oHo Avatar answered Sep 20 '22 20:09

oHo