exception":"java.lang.ClassCastException in liferay service builder - java

I have worked on implementation of the Entity remote service.
I have created one custom service method in EntityServiceImpl,Created custom service method providing service through InstitutionServiceUtil.
After deploy the portlet, while sending request to service method through from browser window,I am getting below Exception
exception":"java.lang.ClassCastException: com.institutions.model.impl.InstitutionImpl
cannot be cast to com.institutions.model.Institution
Note: If I send the request after restart the server, I didn't get above exception.
How to resolve the above Exception?

I assuming that InstitutionImpl implements the interface Institution. If so, then the root cause of the exception is classloading: Classloader A did load Institution but InstitutionImpl was loaded from a different classloader. Two classes in Java are only equivalent if the fully qualified name and the classloader are the same.
I don't know enough about liferay to tell you how it's class loading works. But to solve the problem, you need to find out if the Institution interface could already be around when you try to load your implementation (maybe from a previous deployment attempt).

While deploying the portlets that throws the class cast exception, do the following:
deploy the application in the liferay/deploy.
shutdown the liferay
move the service jar from the WEB-INF/lib from the portlet to the /lib/ext of the tomcat
remove the temp and work folder from the tomcat
restart the tomcat.
OR ...what worked for me was
change the package name while building the service.xml in the service.xml file
Or if you have already built the service, do these steps
Just delete the 5 packages that are created from the service builder,
i.e
model.impl
service.base
service.http
service.impl
service.persistence
delete the .xml generated in the META-INF folder except for the file ext-spring.xml
delete the XX-service.jar from the docroot/lib folder
delete the service folder in the docroot folder.
change the package name in the service.xml and build the path.

Related

Java EE WebApp ServiceLoader loading external jar as plugin

I have a very simple POC setup where I deploy a JEE7 webapp on a wildfly 9.
Via a jaxRs Resource endpoint I can trigger a "plugin loader".
The PluginLoader does use a directory and scans for jar files in the directory, which URLs then will be fed into a URLClassLoader.
Afterwards I use the ServiceLoader to load implmementations of a simple interface from those URLs.
When the ServiceLoader starts iterating over the found implementations, I get this error:
Caused by: java.util.ServiceConfigurationError: com.test.MyIface: Provider com.test.MyImpl not a subtype
The structure is also very simple:
MyIface.jar is the interface.
MyImpl.jar is a implementation of MyIface, while it contains a META-INF/services file with the correct naming and content for MyIface..
The webapp itself only knows MyIFace of course.
In JavaSE using a simple main entry point and invoking the loader from there, everything works.
In JavaEE the services file seems to be ignored though..at least that is what I get from the exception.
I put it in src/main/resources/META-INF/services
and in src/main/resource/WEB-INF/classes/META-INF/services (as I read that already in context with SPI and webapps)
In order for this to work, the following 2 steps must be followed:
Instantiate a ClassLoader (the stock URLClassLoader will do) that knows both the targeted jar AND the classloader of the web application.
It needs to know the targeted jar obviously to load the service implementation
It needs to have the classloader of the web app as parent so that all the classloaders share the interface class; otherwise, even if the custom classloader loads the interface, you will run to ClassCastExceptions like "MyIface is not an instance of MyIface"
Specify the classloader you created using the ServiceLoader.load(Class, ClassLoader) method

Spring Boot with devtools call SOAP API

I am deploy an project Spring Boot, using devtools(spring-boot-devtools) and call a Soap service.
I generate the Soap class into /src/main/resources/templates/generated
and add this folder as Source Code.
Because when call this Soap service, its have a problem:
java.lang.IllegalArgumentException: ...ClassV11PortType referenced from a method is not visible from class loader
So, I was add the spring-devtools.properties file to /src/main/resources/META-INF/spring-devtools.properties
and add this line to spring-devtools.properties file:
restart.exclude.mygeneratedclasses=/[packageOfGeneratedClass].class
Then now, I can call the SOAP service successful.
But now, my project cannot reload automatically when i modified some code.
I was try to edit some code anywhere and save but not luck, my project doesnot reload.
Instead of excluding generated files, you can try to include JAR responsible for loading these classes into restart classloader (used in spring-devtools).
For dependency com.sun.xml.ws:jaxws-rt:2.3.2-1, update /src/main/resources/META-INF/spring-devtools.properties like this:
restart.include.jax=/jaxws-rt.*\.jar
Github issue reference: Devtools cannot be use with jaxws-ri #19379

JDBC driver not found (servlet, DAO, mariaDB) [duplicate]

I developer a web application using Java. When I deploy it to my application server (Jetty, Tomcat, JBoss, GlassFish, etc.) throws an error. I can see this error message in the stacktrace:
java.lang.ClassNotFoundException
Or
java.lang.NoClassDefFoundError
What does this mean and how can I fix it?
What does this mean?
First, let's see the meaning of java.lang.ClassNotFoundException:
Thrown when an application tries to load in a class through its string name using:
The forName method in class Class.
The findSystemClass method in class ClassLoader.
The loadClass method in class ClassLoader.
but no definition for the class with the specified name could be found.
Usually, this happens when trying to open a connection manually in this form:
String jdbcDriver = "...'; //name of your driver
Class.forName(jdbcDriver);
Or when you refer to a class that belongs to an external library and strangely this class cannot be loaded when the application server tries to deploy the application.
Let's see the meaning of java.lang.NoClassDefFoundError (emphasis mine):
Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.
The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.
The last part says it all: the class existed at compile time i.e. when I compiled the application through my IDE, but it is not available at runtime i.e. when the application is deployed.
how can I fix it?
In Java web applications, all third party libraries used by your application must go in WEB-INF/lib folder. Make sure that all the necessary libraries (jars) are placed there. You can check this easily:
- <webapp folder>
- WEB-INF
- lib
+ jar1
+ jar2
+ ...
- META-INF
- <rest of your folders>
This problem usually arises for JDBC connectivity jars (MySQL, Derby, MSSQL, Oracle, etc.) or web MVC frameworks libraries like JSF or Spring MVC.
Take into account that some third party libraries rely on other third party libraries, so you have to add all of them in WEB-INF/lib in order to make the application work. A good example of this is RichFaces 4 libraries, where you have to download and add the external libraries manually.
Note for Maven users: you should not experience these problems unless you have set the libraries as provided, test or system. If set to provided, you're responsible to add the libraries somewhere in the classpath. You can find more info about the dependency scopes here: Introduction to the Dependency Mechanism
In case the library must be shared among several applications that will be deployed on your application server e.g. MySQL connector for two applications, there's another alternative. Instead of deploying two war files each with their own MySQL connector library, place this library in the common library folder of the server application, this will enable the library to be in the classpath of all the deployed applications.
This folder vary from application server.
Tomcat 7/8: <tomcat_home>/lib
JBoss 7/Wildfly: <jboss_home>/standalone/lib
The class must exist under WEB-INF/classes or be inside a .jar file under WEB-INF/lib. Make sure it does.
Same problem happen with me.
Might be possible one of your libraries are using some classes internal which is not available
in your lib or maven dependency pom.xml.
Thats means you have analyze your error logs and identify these classes and then import all dependencies in maven or lib folder.
I have fixed this error by the same way.
because some of my libraries are using activation.jar and json.jar internally.

Call a method after deploying a war in a Java EE server

I am deploying two war say A,B in Java EE Server say Jboss.
Deployment order will be
WAR A
WAR B
I need to call a method once war B is deployed. That method will create an instance of a class which will be loaded only from the class path mentioned in the manifest file of the war.
I used a ServletContextListener, but this failed.
Note : I tested ServletContextListener - by specifying a sample content in the static block of the class that has to be instantiated and tried to create an instance from the ServletContextListener. But an error is thrown stating "RunTime Exception unable to load the class", and the sample content is also not displayed. More over the class is loaded in the server which is confirmed via -verbose:class option in the JAVA_OPTS.
But it works if I load the class from war A. It even works fine. It's a big process to explain why there is a need for creating such instances. I don't want to change the way in which the instance of the class is created.
Is there any other way to call the method after the war and all its dependencies are completely loaded?

Ejb lookup failing on WAS7.0 with NamingException

I have an application developed on RAD using WAS 6.0. I migrated the code to WID 7.0. After making some changes in the EJB modules(Had to remove the bnd.xmi file from each ejb module to deploy the application on Application Server)the application is running fine, but the EJB modules give the following error:
NamingException has Occured While Getting Local Home
javax.naming.NameNotFoundException:nullName ejb/com/igcc not found in context "local:".
I am not able to figure out what changes do it need to make to run the application on WID.
Any help is appreciated.
Thanks,
Ayush
Well, the "bnd.xmi" files you deleted are the WebSphere-specific deployment descriptors, containing binding information. One of the things that are mentioned there is the name under which to bind each individual EJB home.
You cannot possibly run an EJB module without this binding information existing somewhere.
If you delete these files (which are generated by RAD), you have to assign new binding information from within the administration console, or via your wsadmin-based deployment scripts.
In short... lets start by recovering those files that you erased. :-)

Categories