Why Cant Java Load This Class:

- 1 answer

currently I am working on a school Project. The goal of this project is to receive data from a Server and writing data to the Server. For this purpose I am using Sockets and ServerSockets. To encrypt the messages I am using Apache Commons Codec 1.9.

(Server is a java .jar file executed on an Ubuntu Server and the client is an android app)


To encrypt the messages I am exchanging public keys.

private void Schluesselaustausch() {
    try {

        GenerateKeys gk = new GenerateKeys(4069);
        ServerPrivateKey = gk.getPrivateKey();

        ObjectOutputStream oos = new ObjectOutputStream(clientSocket.getOutputStream());
        ObjectInputStream ois = new ObjectInputStream(clientSocket.getInputStream());
        Object obj = ois.readObject();
        ClientPublicKey = (PublicKey) obj; //Line 108

    } catch (Exception e) {

The error occurs when an object is casted to an PublicKey (Line 108).

The Client has the same function, but he is receiving and then sending an object.

The Error

    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(
    at java.base/java.lang.ClassLoader.loadClass(
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(
    at java.base/
    at java.base/
    at java.base/
    at java.base/
    at java.base/
    at java.base/
    at Engine.ClientVerbindung.Schluesselaustausch(

There is more

Now to the strange part: Everything works if I am using a client running on PC. So I figured out that my server was coded on Java 10 but on my Ubuntu Server there was Java 11 installed. I downgraded Java and tested it again. Nothing changed.

My Questions

Is there an error in Android? Do I have to upgrade java to 11? What is the problem?

Thank you for your help. RT



You are not sending a key (some string or number value) but the whole Java object back and forth through the Object*Streams. When you use a PC you are basically using the same Java implementation as the server, so the server can deserialize this Java object. If you System.out.println the .getClass() of the received object on the server, you will find that it does not talk about

Yet, when you connect from your mobile device, the Android Java version is used and it obviously contains Android specific objects of class The server does not have these class definitions anywhere in its library, so it cannot deserialize them.

Rather then de-/serializing the Java key objects, you should send the plain keys around.