Search code examples
springantjavah

javah cannot find dependent classes from spring-context-3.0.3.jar file


Am usning ANT tasks to compile java classes and then generate .h files using

javah [ version "1.7.0_55" ] on windows 7

following is the code snippet from my build.xml

...
<property name="build.dir"        value="./main/test_target"/>
<property name="classes.dir"      value="${build.dir}/classes"/>
<property name="external.lib.dir" value="./externalJars"/>  <!-- contains spring-context-3.0.3.jar file --> 
<property name="jni.output.dir"   value="${build.dir}/jniHeaders"/>
..
...
<target name="compile-header" depends="build">

    <exec executable="javah">
        <arg line="-classpath ${classes.dir};${external.lib.dir} -o ${jni.output.dir}/MyNativeImpl.h -verbose -force examples.MyNativeImpl"/>
    </exec>
</target>

...
..

But I am not able to generate the .h files and get the following ERROR

Error: Class org.springframework.context.MessageSource could not be found.

even though I have added "spring-context-3.0.3.jar" to externalJars dir => ${external.lib.dir}

My java file MyNativeImpl.java uses package "import org.springframework.context.MessageSource; " Java file compilation goes fine using ${external.lib.dir} and MyNativeImpl.class also generated under ${classes.dir}

Not sure what am I doing wrong!!

Almost spend 1 day searching on SS but could find a specific solution. I have many such dependent jars from spring framework to compile my app fully under ${external.lib.dir} hence wanted to know how could this be resolved so than JAVAH could find classes within .jar files !!

Surprisingly I have a work-around which is:

1. unzip the contents of pring-context-3.0.3.jar to CLASSES DIR  ${classes.dir}
2. now my ${classes.dir} has following contents
   a. examples/MyNativeImpl.class
   b. org/springframework/context/MessageSource.class  ( and other classes )

3. Now JAVAH doesn't complain and generate my MyNativeImpl.h

But I am now sure why it doesn't find the same MessageSource.class when I use spring-context-3.0.3.jar ??

Is there something I am missing or this is the only way :-( any help is highly appreciated.

Thanks in Advance

Regards, Vivek


Solution

  • When specifying jars on a classpath, you must list each jar. The directory containing the jar is not enough, that only works for classes, explaining why your work-around works.

    I suggest something like:

    <path id="javah.path">
      <pathelement location="${classes.dir}"/>
      <fileset dir="${external.lib.dir}" includes="*.jar"/>
    </path>
    
    <pathconvert property="javah.classpath" refid="javah.path"/>
    
    <exec executable="javah">
      <arg line="-classpath ${javah.classpath} -o ... />
    </exec>
    

    PS