问题
I'm finding troubles when trying to use SOAP in tests in a maven project that has Java EE 6 API as a provided-scoped dependency.
Important facts
- I'm aware that the API is unusable for testing
- I know I should import an implementation of any specification if I want to use it
- SOAP is part of Java SE, not EE
- SOAP (mock) classes are included in the Maven artifact and I believe they should not
- This happens in Java EE 6, but those classes have been removed in version 7
- If I remove the dependency, the tests work fine
- When using 7.0 version of the API everything works
Exception
java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/xml/messaging/URLEndpoint
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:455)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:102)
at somecompany.someproject.service.SomeBean.someMethod(SomeBean.java:##)
at somecompany.someproject.service.SomeBeanTest.testSomeMethod(SomeBeanTest.java:##)
POM configuration
<dependencies>
...
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
...
</dependencies>
<build>
...
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
...
</plugin>
...
</plugins>
...
</build>
Failed solutions
I have tried several solutions (not going to post each one for clarity, but will post them by request):
- I have tried every single
<scope/>
in Maven and none disables the library in tests - Also, tried excluding the library as a dependency of the plugin
- Including rj.jar as a system dependency with a higher priority than javaee-api one didn't work either
Unwanted workaround
I got the tests working using 7.0 dependency but this is unacceptable according to policies, so I need to fix this.
回答1:
I got to make it work using the property classpathDependencyExcludes
of the maven-surefire-plugin
:
<build>
...
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
...
<configuration>
<classpathDependencyExcludes>
<classpathDependencyExcludes>javax:javaee-api</classpathDependencyExcludes>
</classpathDependencyExcludes>
...
</configuration>
...
</plugin>
...
</plugins>
...
</build>
I'm going to leave this posted as I believe I'll not be the only one experiencing this at some point.
回答2:
I was running in the same problem as below while running java class. Exception: java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/xml/messaging/URLEndpoint
With Java8 as runtime, I finally included correct version (7.0) and the program started working fine earlier it was taking version 6.
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
来源:https://stackoverflow.com/questions/26055085/maven-tests-troubles-with-soap-having-java-ee-6-dependency