3

I have a interesting problem.

When I started glassfish server, everythings work fine. But, I changed some code and published the server, and I run my client (SistemGirisClientKullaniciDogrula). The application throws this exception:

java.lang.ClassCastException: tr.com.app.Kullanici cannot be cast to tr.com.app.Kullanici.

Interesting part is, after the Glassfish server restart, application works fine.

I am using restlet-spring-hibernate. And I am also using JAXB (org.restlet.ext.jaxb.jar) for converting XML to Java objects. My application server is Glassfish v3.0

detail for congiguration

  • restlet 2.0.5
  • spring 3.0.5
  • hibernate 3.3.2
  • glassfish v3.0

client class(Just for test)

import java.io.IOException;

import org.restlet.Client;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.MediaType;
import org.restlet.data.Method;
import org.restlet.data.Protocol;
import org.restlet.ext.jaxb.JaxbRepresentation;

public class SistemGirisClientKullaniciDogrula {

    public static void main(String[] Args) throws IOException {

        String url = "http://localhost:8080/Project/sistemgirisws";

        Client client = new Client(Protocol.HTTP);

        Kullanici kullanici = new Kullanici();
        kullanici.setKodu("1");

        JaxbRepresentation<Kullanici> jaxbRepresentationSendingKullanici= new JaxbRepresentation<Kullanici>(MediaType.APPLICATION_XML, kullanici);

        Request request = new Request(Method.GET, url, jaxbRepresentationSendingKullanici);
        Response response = client.handle(request);

        JaxbRepresentation<Kullanici> kullaniciResponse = new JaxbRepresentation<Kullanici>(response.getEntity(), Kullanici.class);
        kullanici = kullaniciResponse.getObject();

        System.out.println("kullanici id : " + kullanici.getId());
    }
}

Web Service

public class ProjectWebService {

/**
 * 
 * @param representation
 * @return
 */
@Get
public Representation getKullanici(Representation representation) {

    JaxbRepresentation<Kullanici> jaxbRepresentation = new JaxbRepresentation<Kullanici>(representation, Kullanici.class);

    Kullanici kullanici = new Kullanici();

    try {

        kullanici = jaxbRepresentation.getObject(); //THIS LINE THROW java.lang.classCastException tr.com.app.Kullanici cannot be cast to tr.com.app.Kullanici.

    } catch (IOException e) {
        e.printStackTrace();
    }

    try {

        kullanici = sistemGirisBusinessManager.kullaniciDogrula(kullanici);

        getResponse().setStatus(Status.SUCCESS_OK);
        return new JaxbRepresentation<Kullanici>(MediaType.APPLICATION_XML, kullanici);

    } catch (Exception exception) {

        exception.printStackTrace();
        getResponse().setStatus(Status.CLIENT_ERROR_EXPECTATION_FAILED);
        return new JaxbRepresentation<MesajList>(MediaType.APPLICATION_XML, sistemGirisBusinessManager.getMesajList());

    }
}
}

Does Anyone know what the problem is?

2 Answers 2

3

This could be a class loading issue. In Java, If two classloaders have loaded the same class, then it is treated as two different classes. In that case, your casting will fail because it seems to the JVM that you are casting one type to another which is not in an inheritance tree.

What must be happening is that when you modify your class, it gets loaded into a different classloader, where as the web service uses the original one.

4
  • Hi Yohan, Thanks for reply. But i have just one war.Is it possible that one war could use two different class loader. May 31, 2011 at 7:51
  • Hi @Musa, Yes, it is possible to use multiple classloaders with in a container even when you have only one WAR file. That means, the container can use a separate classloader for your WAR file only. My guess is that when you deploy the application it uses one classloader, and for your subsequent modifications (debug mode), the code replace uses another classloader. May 31, 2011 at 7:55
  • 2
    @Musa YUVACI - ClassLoader leaks of this form are often due to references being held in server-scope ClassLoader (either bugs in the app server or developers adding libraries outside WARs/EARs.)
    – McDowell
    May 31, 2011 at 9:08
  • @McDowel-Thanks for reply, it works now. Problem was occured by adding libraries outside WARs. So after removing libraries from the glassfish server, it works. May 31, 2011 at 13:35
2

Problem indeed in different class loaders. i've had it too and adding implements Serializable and serialVersionUID for those object solved the problem.

1
  • Hi Vidmantas, what were the values of serialVesionUIDs that You've added? I have all my entity classes implementing Serializable and with serialVersionUIDs inside (all have value = 1L). I still have that problem with casting Exception, though it occurs very unpredictibly.
    – greenskin
    Apr 23, 2013 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.