Using javah maven-antrun-plugin with jdk 1.7, clas

2019-07-30 12:34发布

问题:

In jdk 1.7 for the Mac, the location of com.sun.tools.javah.Main moved from classes.jar to tools.jar. Consequently, Maven's maven-antrun-plugin cannot find the run the javah task and a ClassNotFound exception is thrown:

Caused by: java.lang.ClassNotFoundException: com.sun.tools.javah.Main
at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java :50)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:244)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:230)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at org.apache.tools.ant.taskdefs.optional.javah.SunJavah.compile(SunJavah.java:57)
... 47 more

1.7 Location: /Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home/lib/tools.jar

1.6 Location: /Library/Java/JavaVirtualMachines/1.6.0_32-b05-420.jdk/Contents/Classes/classes.jar

This seems like a bug or failure of maven version 3.0.4. I considered making tools.jar a plugin dependency, but that just doesn't seem right. I tried these solutions, but I couldn't get them to work:

JDK tools.jar as maven dependency

maven: How to load tools.jar/classes.jar in an OS independent way?

Any work around until the maven folks address the maven-antrun-plugin with jdk-1.7 on mac?

回答1:

The answer was to add the tools.jar as a dependency of the plugin. Include the part below from <dependencies> ... </dependencies>

 <build>   
 <plugins>
   <plugin>
     <artifactId>maven-antrun-plugin</artifactId>
     <version>1.7</version>
     <executions>
       <execution>
         <phase>compile</phase>
         <configuration>
           <target>
             <property name="runtime_classpath" refid="maven.runtime.classpath"/>
             <property name="test_classpath" refid="maven.test.classpath"/>
             <property name="plugin_classpath" refid="maven.plugin.classpath"/>

             <echo message="runtime classpath: ${runtime_classpath}"/>
             <echo message="test classpath:    ${test_classpath}"/>
             <echo message="plugin classpath:  ${plugin_classpath}"/>

           </target>
         </configuration>
         <goals>
           <goal>run</goal>
         </goals>
       </execution>
     </executions>
     <dependencies>
       <dependency>
         <groupId>com.sun</groupId>
         <artifactId>tools</artifactId>
         <version>1.7</version>
         <scope>system</scope>
         <systemPath>${java.home}/../lib/tools.jar</systemPath>
       </dependency>
     </dependencies>
   </plugin>
 </plugins>
 </build>