开发者

Error with serializable class

开发者 https://www.devze.com 2023-03-07 03:34 出处:网络
I have a java server that sends some data through a socket to a client developed in android. The data that sent is serialized...and at the client side when trying to read from the ObjectInputStream I

I have a java server that sends some data through a socket to a client developed in android. The data that sent is serialized...and at the client side when trying to read from the ObjectInputStream I get the following error:

 java.lang.ClassNotFoundException: servers.Coordinate    
 at java.lang.Class.classForName(Native Method)
 at java.lang.Class.forName(Class.java:237)
 at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:2604) 
 at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1860)
 java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:840)
 at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:2080)
 java.io.Objec开发者_如何学运维tInputStream.readNonPrimitiveContent(ObjectInputStream.java:943)
 java.io.ObjectInputStream.readObject(ObjectInputStream.java:2299)
 at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2254)
 at com.Server_1.ClientThread_special.run(ClientThread_special.java:30)
 at java.lang.Thread.run(Thread.java:1096)
 Caused by: java.lang.NoClassDefFoundError: servers.Coordinate
 Caused by: java.lang.ClassNotFoundException: servers.Coordinate in loader dalvik.system.PathClassLoader@43d02e18
 at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)

Now here is my code on the server side:

clientSocket = serverSocket.accept();
        System.out.println("S-a conectat clientul de monitorizare!");


        os=new ObjectOutputStream(clientSocket.getOutputStream());
        try{
        while(true){
            coord=(Coordinate)queue.take();
            System.out.println(coord.getLat());
        os.writeObject(coord);

        os.flush();

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

        }

On the client side I have this:

public void run() {

    try {
        InetAddress serverAddr = InetAddress.getByName(serverIpAddress);

        Socket socket = new Socket(serverIpAddress, serverPort);

        is=new ObjectInputStream(socket.getInputStream());



            try{
                while(!stop){

            coord=(Coordinate)is.readObject();

            System.out.println("Date de la clientul de monitorizare:"+coord.getLat());

            }

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

        is.close();

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

And here is my serializable class I have it both on the client side and in the server side:

 public class Coordinate implements Serializable{

  private final double lon;

  private final double lat;

  private final int workerId;



  public Coordinate(double lat, double lon, int workerId) {

    this.lat = lat;
    this.lon = lon;

    this.workerId=workerId;
  }

  public double getLon() {
    return lon;
  }

  public double getLat() {
    return lat;
  }
  public int getwId(){
      return workerId;
  }


}

An instance of this class is what I'm sending through the socket.


EDIT:

on server I have the following :

package servers;

import java.io.Serializable;

public class Coordinate implements Serializable{

  private final double lon;

  private final double lat;

  private final int workerId;



  public Coordinate(double lat, double lon, int workerId) {

    this.lat = lat;
    this.lon = lon;

    this.workerId=workerId;
  }

  public double getLon() {
    return lon;
  }

  public double getLat() {
    return lat;
  }
  public int getwId(){
      return workerId;
  }


}

on the client side I have the following:

package com.Server_1;

import java.io.Serializable;

public class Coordinate implements Serializable{

  private final double lon;

  private final double lat;

  private final int workerId;

  public Coordinate(double lat, double lon, int workerId) {

    this.lat = lat;
    this.lon = lon;

    this.workerId=workerId;
  }

  public double getLon() {
    return lon;
  }

  public double getLat() {
    return lat;
  }
  public int getwId(){
      return workerId;
  }


}

I don't understand how could I change the package to be the same on both of them!!!!!!


The package name of the class must be the same on the server and the client. I would also add serialVersionUID to the class. Implementing Serializable

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号