Setting the Order of Precedence of Jars in WAR - java

I'm getting the following error:
com.sun.xml.messaging.saaj.soap.MessageFactoryImpl cannot be cast to javax.xml.soap.MessageFactory
Is there a way to force use of the MessageFactory from saaj-api over the one from javaee-api? At least that's what I think may need to be done.
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.messaging.saaj</groupId>
<artifactId>saaj-impl</artifactId>
<version>1.3.15</version>
</dependency>
<dependency>
<groupId>javax.xml.soap</groupId>
<artifactId>saaj-api</artifactId>
<version>1.3.5</version>
</dependency>
<prefer-application-packages>
<package-name>javax.xml.soap.*</package-name>
<package-name>javax.xml.ws.*</package-name>
<package-name>com.sun.xml.messsaging.saaj.*</package-name>
</prefer-application-packages>

Generally speaking you don't want to package API's in your war/ear files as they should be provided by the application server. I would set the scope to provided on those two api dependencies and work from there.

Related

Use Provided jax-rs 2.0 and Jersey 2.1x in Weblogic 12c (12.2.1.3)

I need to implement a rest service call in my WEB application. According to Oracle, Weblogic is supported and does not need to register (deploy) jax-rs, so I would like to use these Server libraries. I made a simple class by calling a service (get). I configured the dependencies in the project and deployed it on Weblogic. However, when deploying, the following error appears: java.lang.ClassCastException: Cannot cast org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider to org.glassfish.jersey.server.spi.ComponentProvider
Note: It worked using this link below (deploying the jar on the server) But I want to use the native libraries on Weblogic. Could someone help me please?
https://docs.oracle.com/middleware/1213/wls/RESTF/use-jersey20-ri.htm#RESTF297
Code example (Java)
String host = "https://swapi.dev/api/people/2/";
Client client = ClientBuilder.newBuilder().build();
WebTarget webTarget = client.target(host);
Builder builder = webTarget.request(MediaType.APPLICATION_JSON);
String result = builder.get(String.class);
pom.xml
<properties>
<primefaces.version>3.5.RC1</primefaces.version>
<jersey.version>2.21.1</jersey.version>
<jaxrs.version>2.0</jaxrs.version>
</properties>
<!-- JAX-RS -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>${jaxrs.version}</version>
<scope>provided</scope>
</dependency>
<!-- Jersey 2.21.1 -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>${jersey.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.4.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
weblogic.xml
<wls:weblogic-version>12.2.1.3</wls:weblogic-version>
<wls:context-root>RecebimentoMercadoriaWEB</wls:context-root>
<wls:library-ref>
<wls:library-name>jsf</wls:library-name>
</wls:library-ref>
<wls:container-descriptor>
<wls:prefer-web-inf-classes>true</wls:prefer-web-inf-classes>
</wls:container-descriptor>
<wls:session-descriptor>
<wls:cookie-name>CookieRecebimentoMercadoria</wls:cookie-name>
</wls:session-descriptor>
<wls:container-descriptor>
<wls:prefer-application-packages>
<wls:package-name>javax.faces.*</wls:package-name>
<wls:package-name>com.sun.faces.*</wls:package-name>
<wls:package-name>com.sun.facelets.*</wls:package-name>
<wls:package-name>com.bea.faces.*</wls:package-name>
</wls:prefer-application-packages>
<wls:prefer-application-resources>
<wls:resource-name>javax.faces.*</wls:resource-name>
<wls:resource-name>com.sun.faces.*</wls:resource-name>
<wls:resource-name>com.sun.facelets.*</wls:resource-name>
<wls:resource-name>com.bea.faces.*</wls:resource-name>
<wls:resource-name>META-INF/services/javax.servlet.ServletContainerInitializer</wls:resource-name>
<wls:resource-name>META-INF/services/com.sun.faces.spi.FacesConfigResourceProvider</wls:resource-name>
</wls:prefer-application-resources>
</wls:container-descriptor>
On Weblogic 12.2.1.3 yo do not need to execute the procedure described by the link you have pointed, I mean the link below .
https://docs.oracle.com/middleware/1213/wls/RESTF/use-jersey20-ri.htm#RESTF297
This is because that link belongs to Oracle Weblogic 12.1.3 and there are several differences between Weblogic 12.1.3.0 and Weblogic 12.2.1.3.
Furthermore, this document for Oracle Weblogic 12.2.1.3 states.
Note:
Jersey 2.x (JAX-RS 2.0 RI) support is provided by default in this
release of WebLogic Server. Registration as a shared library is no
longer required.
This means, when it comes to Weblogic 12.2.1.3 Jersey libraries are in place and ready to be used. Thus, your application should be able to use them.
However, I think server libraries are getting troubles with the libraries you are using within your pom.xml file.
Furthermore Oracle Weblogic 12.2.1.3 provides jersey 2.22.4
I have also used wls-cat in one of my servers to know, which library is loading the class org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider and I have found the library in $ORACLE_HOME/oracle_common/modules/org.glassfish.jersey.ext.cdi.jersey-cdi1x.jar, which means it is loaded by Weblogic as is stated on above documentation.
Furthermore, after running wls-cat I can see this:
org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider implements org.glassfish.jersey.server.spi.ComponentProvider
Thus, cast should not be an issue, which means there is a class loading problem that most probably is caused by libraries included in your application.
You can see the results of wls-cat executed on my server on below picture
You can use wls-cat to see which file (a JAR library) is loading the conflicting class. In below post you will find information about how to use wls-cat to analyse class loading problems.
https://blog.sysco.no/class/loader/AnalysingClassLoadingConflicts/
The libraries were really conflicting. I removed these dependencies from pom.xml and it worked. Thanks for the tip.
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>${jersey.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.4.1</version>
<scope>provided</scope>
</dependency>

J2EE preview error with javax.faces.webapp.FacesServlet class

Trying to start an Eclise Maven Web-Project in J2EE preview server (in Glassfish4, it works fine) i get an error:
java.lang.ClassNotFoundException: javax.faces.webapp.FacesServlet
Howewer, trying to make the fix like adviced on some another topik in this forum:
set in properties->project->fasets-> user library the jsf-api-2.1,jar
(as external jar, what is not nice),
i get another error :
Started ServerConnector#1727e0ec{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/faces/webapp/FacesServlet
Searching for solution i found the info, that this is caused by classes in
javaee-web-api what are just blueprints and server must have implementaion for them.
That is why for example you write maven dependency for that as provided, like:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
So i guess it is in this external jar too, it just not suppose to be included. What jar must i add then to fix this problem, or there is another better solution for this?
you can add below dependency into project pom.xml
and try to update the project.'
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.1.7</version>
</dependency>
or you can also add
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>

what I need to do to use the ManagedBean library?

Ive been using anotation for my beans (I havent used them before) and I found out I have to include some dependencies and so on, like this:
<!--JSF API-->
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.0.2</version>
</dependency>
<!-- PrimeFaces -->
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>3.3</version>
</dependency>
<!-- JSF -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<!-- Javax Servlet. This needs to be included for runtime only! -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
and well.. the problem is that the I keep getting the error of
The import javax.faces cannot be resolved
Every time I need to use the managed bean... I am using Maven and I am supposed to have downloaded all things I need, I also tried other POM and in that project I can use anotations, so, I guess is a misconfiguratino of the POM? I didnt do anything special to ecplise, except enable the maven clean, maven eclipse, and maven eclipse skip test
I really checked other answers, like these:
The import javax.servlet can't be resolved
How do I import the javax.servlet API in my Eclipse project?
but nothing, any idea what could be wrong??
You should not import the servlet API but implementation of the API, for example the servlet container implementation you want on which you want to run your project : Tomcat, JBoss, Websphere, Glassfish....
E.g. for tomcat :
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>7.0.30</version>
<scope>provided</scope>
</dependency>
Thanks for the answer, I checked that using Maven its a little bit different and in the built path you have to specify the path for your maven repository, so, it will incluide all libraries directly or you can copy and past them into your project

When deploying a web application I get the exception NoClassDefFoundError:LocalizableImpl

I have a standard J2EE web application that includes web services. I'm using the webservices-rt library to host the services. [See the maven dependency below]. However, I get the following exception at run time:
SEVERE: Exception sending context initialized event to listener instance of class com.sun.xml.ws.transport.http.servlet.WSServletContextListener
java.lang.NoClassDefFoundError: com/sun/xml/ws/util/localization/LocalizableImpl
at com.sun.xml.ws.util.exception.JAXWSExceptionBase.<init>(JAXWSExceptionBase.java:63)
at com.sun.xml.ws.transport.http.servlet.WSServletException.<init>(WSServletException.java:47)
at com.sun.xml.ws.transport.http.servlet.WSServletContextListener.contextInitialized(WSServletContextListener.java:118)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [...]
at java.lang.Thread.run(Thread.java:724)
Caused by: java.lang.ClassNotFoundException: com.sun.xml.ws.util.localization.LocalizableImpl
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
... 33 more
Maven WS Dependency
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>webservices-rt</artifactId>
<version>1.4</version>
<scope>compile</scope>
</dependency>
Am I missing a library? I've tried adding jaxws-rt. However, that requires an additional repo [jboss]. I'm a bit leery of that, as that it introduces a lot of new libraries into the project.
If you are using Maven, add below to your project should solve your problem:
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.3.2</version>
</dependency>
You don't have to add others jar because it will automatically pull the rest of dependencies. I prefer to add the jar to my war instead of Tomcat lib. I think it is more portable.
try
The JAX-WS dependency library “jaxws-rt.jar” is missing.
Go here http://jax-ws.java.net/.
Download JAX-WS RI distribution.
Unzip it and copy “jaxws-rt.jar” to Tomcat library folder “{$TOMCAT}/lib“.
Restart Tomcat.
For a maven, tomcat application try these dependencies in your pom.xml
<dependencies>
<!-- jax-ws maven dependency -->
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.2.8</version>
</dependency>
<!-- servlet provided by tomcat -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-core -->
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.xml.stream.buffer/streambuffer -->
<dependency>
<groupId>com.sun.xml.stream.buffer</groupId>
<artifactId>streambuffer</artifactId>
<version>1.5.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl -->
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.xml.ws/policy -->
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>policy</artifactId>
<version>2.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.gmbal/gmbal-api-only -->
<dependency>
<groupId>org.glassfish.gmbal</groupId>
<artifactId>gmbal-api-only</artifactId>
<version>3.2.0-b003</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.ha/ha-api -->
<dependency>
<groupId>org.glassfish.ha</groupId>
<artifactId>ha-api</artifactId>
<version>3.1.9</version>
</dependency>
</dependencies>
I hope this works for new people who will face this issue
Regards
Declaring the JBoss repo doesn't automatically import the repo libraries. It simply makes the libraries available for importing.
Bottom line is that if you want to use a class that's in a library, then you have to pull the library into your project. If the library is in the JBoss repo, then you have to declare the JBoss repo.

What dependencies do I need for embedded ActiveMQ broker?

I'm trying to use embedded ActiveMQ broker for unit testing, as explained here: http://activemq.apache.org/how-to-unit-test-jms-code.html
What Maven dependencies I need to include? At the moment I have just these:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.5.0</version>
</dependency>
This is what I'm getting:
java.lang.ClassFormatError: Absent Code attribute in method that
is not native or abstract in class file javax/jms/JMSException
When trying to instantiate a broker:
final BrokerService broker = new BrokerService();
What else should I add to the list of Maven dependencies? (I'm not using Spring)
The solution is simple, just need to remove the javax:javaee-api dependency.
can you try activemq-all:
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.5.0</version>
</dependency>
moved the javaee dependency to the end of the class path. This solved my problem.

Categories