Apache CXF client loads fine in Eclipse but standalone jar throws NullpointerException in WSDLServiceFactory
Asked Answered
S

4

20

My goal is to create a Web Service client that runs in a standalone jar with all the dependencies using mvn assembly:single

I generated the client using CXF codegen wsdl2java, creating a @WebServiceClient called NetBanxAutostatementService

For the dependencies I have

<cxf.version>2.5.2</cxf.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>${cxf.version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>${cxf.version}</version>
        <scope>runtime</scope>
    </dependency>

desperately I even tried to add more "stuff"

    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-core</artifactId>
        <version>2.5.2</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf</artifactId>
        <version>2.5.2</version>
        <type>pom</type>
        <scope>runtime</scope>
    </dependency>

The problem: everytime I try to run "java -jar target/Netbanx-0.0.1-SNAPSHOT-jar-with-dependencies.jar"

INFO [main] (Netbanx.java:97) - autostatement_wsdlLocation:https://www.test.netbanx.com/cgi-bin/autostatement_wsdl
Exception in thread "main" java.lang.NullPointerException
at org.apache.cxf.wsdl11.WSDLServiceFactory.<init>(WSDLServiceFactory.java:92)
at org.apache.cxf.jaxws.ServiceImpl.initializePorts(ServiceImpl.java:204)
at org.apache.cxf.jaxws.ServiceImpl.<init>(ServiceImpl.java:148)
at org.apache.cxf.jaxws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:91)
at javax.xml.ws.Service.<init>(Service.java:56)
at com.netbanx.autostatement.NetBanxAutostatementService.<init>  (NetBanxAutostatementService.java:39)
at my.project.netbanx.Netbanx.<init>(Netbanx.java:98)
at my.project.netbanx.Netbanx.main(Netbanx.java:130)

This happens in the line that invokes the WebServiceClient autostatementService = new NetBanxAutostatementService(autostatement_wsdlLocation); I know by the log line that I am not passing autostatement_wsdlLocation as null

Java code:

URL autostatement_wsdlLocation = null;
URL payment_wsdlLocation = null;
try {
    autostatement_wsdlLocation = new URL(properties.getProperty("autostatement_wsdlLocation"));
    payment_wsdlLocation = new URL(properties.getProperty("payment_wsdlLocation"));
} catch (MalformedURLException e) {
    logger.error("MalformedURLException",e);
}

    /**
     * Load the Netbanx's webservices AutostatementService and PaymentService
     */
try {           
    logger.info("autostatement_wsdlLocation:"+autostatement_wsdlLocation.toString());
    autostatementService = new NetBanxAutostatementService(autostatement_wsdlLocation); //it is here I get the NullPointerException error
    logger.info("payment_wsdlLocation:"+payment_wsdlLocation.toString());
    paymentService = new NetBanxPaymentService(payment_wsdlLocation);
webServiceStarted = true;
    } catch(javax.xml.ws.WebServiceException wsException ){
        String error = "Cannot create NetBanx web service please make sure this host can reach:" + autostatement_wsdlLocation +" and " + payment_wsdlLocation;
        logger.error(error);
        logger.error("WebServiceException",wsException);

}

Stoop answered 30/1, 2012 at 18:52 Comment(5)
Could we the java code as well?Caprice
NetBanxAutostatementService is a class generated by CFX I can add it as well. This code runs fine in Eclipse so I guess I am missing some dependency to add into the JAR file. I already tried to add cxf.xml Spring configuration but I got the same error.Infirmary
Hmm, I really do not think it is a class loading issue, because that would give you a ClassNotFound exception, not a null pointer. I see that WSDLServiceFactory has more than one argument in its constructors, but it does not say that they would throw a nullpointer exception based on the documentation here: cxf.apache.org/apidocs/org/apache/cxf/wsdl11/… . Try downloading the source code and looking at like 92 and seeing what causes it. Im out of suggestions sorry.Caprice
I had exactly the same problem, and using the shade-plugin fixed it for me, so happy days! But am I right in saying this is a fault in the maven-assembly-plugin - it shouldn't overwrite depenencies should it? - I'd rather it just said there are conflicts. Wasted a lot of time on diagnosing this one.Touslesmois
I am having this exact problem after I changed my webstart to use a single fat jar with the contents of dependency jars bundled within from using individual jars before and assembling the classpath in the launch pageDuley
H
31

Most likely it's how you are creating your single jar. A normal usage of the assembly plugin would not allow that as various parts of CXFs META-INF/* stuff would need to be merged together. That would include all the /META-INF/spring* and much of the stuff in /META-INF/cxf/* I would suggest using the shade plugin for that. See the pom.xml for CXF's bundle jar for an example.

http://svn.apache.org/repos/asf/cxf/trunk/osgi/bundle/all/

Heirloom answered 30/1, 2012 at 19:17 Comment(3)
How does shade plugin know where to get this kind of resources? <transformer implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer"><resource>META-INF/wsdl.plugin.xml</resource></transformer>Infirmary
Changing to the shade plugin and adding all the AppendingTransformer and XmlAppendingTransformer(s) from the pom.xml in the link solved my problem. I guess the culprit was <transformer implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer"> <resource>META-INF/wsdl.plugin.xml</resource> </transformer>Infirmary
Thanks, this really saved my arse! Just note, exactly as from the above "AppendingTransformer" and "XmlAppendingTransformer" is the stuff you want, not the "PluginTransformer" or the "CXFAllTransformer" - I made that mistake at first, your project will probably not build with those included. Peace!Evalyn
W
28

Expanding on @DanielKulp's answer, which worked a treat for me with CXF 2.7.7 (just in case the link dies). Configure your shade plugin with the following additional transformers:

<configuration>
  <transformers>
    <!--  transformers for CXF (see https://mcmap.net/q/610227/-apache-cxf-client-loads-fine-in-eclipse-but-standalone-jar-throws-nullpointerexception-in-wsdlservicefactory) -->
    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
      <resource>META-INF/spring.handlers</resource>
    </transformer>
    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
      <resource>META-INF/services/com.sun.tools.xjc.Plugin</resource>
    </transformer>
    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
      <resource>META-INF/spring.schemas</resource>
    </transformer>
    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
      <resource>META-INF/cxf/cxf.extension</resource>
    </transformer>
    <transformer implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
      <resource>META-INF/extensions.xml</resource>
    </transformer>
    <transformer implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
      <resource>META-INF/cxf/extensions.xml</resource>
    </transformer>
    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
      <resource>META-INF/cxf/bus-extensions.txt</resource>
    </transformer>
    <transformer implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
      <resource>META-INF/cxf/bus-extensions.xml</resource>
    </transformer>
    <transformer implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
      <resource>META-INF/wsdl.plugin.xml</resource>
    </transformer>
    <transformer implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
      <resource>META-INF/tools.service.validator.xml</resource>
    </transformer>
    <transformer implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
      <resource>META-INF/cxf/java2wsbeans.xml</resource>
    </transformer>
  </transformers>
</configuration>
Wrand answered 28/10, 2013 at 9:48 Comment(3)
I searched for like half a day. Thank you so very much!Anent
thank you so much! I had to do this for gradle, which uses the shadow pluginLewert
thanks you SO much, i've been banging my head against the wall.Camacho
O
4

In complement to @Matt R's answer, I replaced the POM part with the maven-assembly-plugin with this code:

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.xxx.App</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <!--  transformers for CXF (see https://mcmap.net/q/610227/-apache-cxf-client-loads-fine-in-eclipse-but-standalone-jar-throws-nullpointerexception-in-wsdlservicefactory) -->
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.handlers</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/services/com.sun.tools.xjc.Plugin</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.schemas</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/cxf/cxf.extension</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
                                <resource>META-INF/extensions.xml</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
                                <resource>META-INF/cxf/extensions.xml</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/cxf/bus-extensions.txt</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
                                <resource>META-INF/cxf/bus-extensions.xml</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
                                <resource>META-INF/wsdl.plugin.xml</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
                                <resource>META-INF/tools.service.validator.xml</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
                                <resource>META-INF/cxf/java2wsbeans.xml</resource>
                            </transformer>
                        </transformers>
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                    </configuration>
                </execution>
            </executions>
        </plugin>

The exclude part is to prevent from having Java Security issues.

Ot answered 16/6, 2017 at 12:46 Comment(0)
A
3

You should try with the one jar plugin, it let's you create super jars that are clean and totaly self-contained

http://code.google.com/p/onejar-maven-plugin/

Ambo answered 31/1, 2012 at 10:58 Comment(1)
This approach works, but I wanted to create a jar with my class being the Main-Class not the com.simontuffs.onejar.BootInfirmary

© 2022 - 2024 — McMap. All rights reserved.