Search code examples
javajspjpaeclipselink

Class Exception Issues (is in unnamed module of loader)


I honestly have no idea what is causing this error all I know is when I try to instantiate a DAO object inside of my servlet and use a request dispatcher to send the object this happens.

SRVE0777E: Exception thrown by application class 'com.projecteden.mainmenu.SelectSurvivorData.doGet:27'
java.lang.ClassCastException: class com.projecteden.livingobjects.entities.SurvivorPO cannot be cast to class com.projecteden.livingobjects.entities.SurvivorPO (com.projecteden.livingobjects.entities.SurvivorPO is in unnamed module of loader com.ibm.ws.classloading.internal.AppClassLoader @3a649edc; com.projecteden.livingobjects.entities.SurvivorPO is in unnamed module of loader com.ibm.ws.classloading.internal.AppClassLoader @3c834c19)
at com.projecteden.mainmenu.SelectSurvivorData.doGet(SelectSurvivorData.java:27)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:686)
at [internal classes]

package com.projecteden.mainmenu;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.projecteden.livingobjects.DAO.SurvivorDAO;

@WebServlet("/GetSurvivorData")
public class SelectSurvivorData extends HttpServlet 
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    SurvivorDAO getList = new SurvivorDAO();
    response.setContentType("text/html;charset=UTF-8");
    request.setAttribute("test", getList.getAllSurvivors().get(0).getName());
    
    request.getRequestDispatcher("WebPages/MainMenu/SelectPlayer.jsp").forward(request, response);
}

}
package com.projecteden.livingobjects.DAO;

import java.util.List;

import com.projecteden.livingobjects.entities.SurvivorPO;




public class SurvivorDAO extends LivingObjectsEM
{

    
    public List<SurvivorPO> findWithId(String id) 
     {      
        return em.createQuery(
            "SELECT c FROM SurvivorPO c WHERE c.id LIKE :id")
            .setParameter("id", id)
            .setMaxResults(1)
            .getResultList();
     }
     
    
    public List<SurvivorPO> getAllSurvivors() 
     {      
        return em.createQuery(
            "SELECT c FROM SurvivorPO c")
            .setMaxResults(10)
            .getResultList();
     }
      
}
package com.projecteden.livingobjects.entities;

import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "survivor_stats")
public class SurvivorPO
{
    @Id
    @Column(name ="id")
    @GeneratedValue(strategy=GenerationType.AUTO, generator="SurvivorIdGenerator")
    @SequenceGenerator(name="SurvivorIdGenerator", sequenceName="SurvivorSequencesId")
    private int id;
    
    @Column(name ="name")
    private String name;
    
    @Embedded
    private StatsPO stats;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public StatsPO getStats() {
        return stats;
    }

    public void setStats(StatsPO stats) {
        this.stats = stats;
    }

    @Override
    public String toString() {
        return "HeroPO [id=" + id + ", name=" + name + ", stats=" + stats + "]";
    }
    
    

}

As far as other things I'm using: This is a Maeven Project with JPA2.2, JSP, Bootstrap5, MySQL, Dynamic Web Module 4.0, Java16, JS 1.0, and EclpipseLink

If you need any further clarification, feel free to ask.


Solution

  • For anyone who has run into a similar issue like this, I have managed to solve the issue. Based on my understanding (which isn't perfect) Liberty Web Sphere in some way or the other has its own class loader that seems to conflict with my DAOs and POs which may have been using a different classloader. The solution for my problem at least was to go into the WebSphere server settings found under Liberty Runtime/servers/WebSphere/server.xml

    Once this file is open there should be a GUI with a few different windows under where it says Server Configuration there should be an item in the list called "Web Application: 'project name'" after opened there should be a setting inside called Classloader which I changed to $(server.config.dir) with that change my project seems to be running normally again.

    Keep in mind I'm specifically using Eclipse, Liberty Web Sphere, Eclipse Link and working on a Maevan / Dynamic Web Application. I also don't know if this is best practice but for a simple project you don't plan to post anywhere, I would think this solution would be fine. This is also a very specific problem with a very specific solution so this may or may not work in a similar situation with similar errors.

    If you happen to have this error but it's referring to two sperate classes then it is more likely based on what I found that you are calling Obj1 obj = new Obj2(); and the error is actually related to Obj2 not being compatible with Obj1. But if you have Obj1 obj = new Obj1; with this error then it is likely a classloader error from what I've seen.

    Best of luck to whoever else has this error because god knows it caused me 6 hours of headaches simply trying to solve the issue.