java.lang.ClassNotFoundException Java Netbeans

0

Hello friends can help me with an error in execution time, please if someone gives me an idea to solve it.

  

java.lang.ClassNotFoundException

I've been trying to solve a lot of time trying some solutions on google but it's still the same.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    try {  
        Socket misocket= new Socket("127.0.0.1",999);


       paqueteuno datos = new paqueteuno();
       datos.setNick(Nick.getText());
       datos.setIp(Ip.getText());
       datos.setMensaje(Mensaje.getText());

        ObjectOutputStream paquete_datos = new ObjectOutputStream(misocket.getOutputStream());
         paquete_datos.writeObject(datos);
        misocket.close();
        /*
        DataOutputStream salida = new DataOutputStream(a.getOutputStream());
        salida.writeUTF(Mensaje.getText());
        salida.close();
        */




    } catch (IOException ex) {
        System.out.println(ex);
    }


} 
package cliente;

import java.io.Serializable;

public class paqueteuno implements Serializable
{
 private String Nick;

 private String Ip;

 private String Mensaje;


        public String getNick() {
            return Nick;
        }

        public void setNick(String Nick) {
            this.Nick = Nick;
        }

        public String getIp() {
            return Ip;
        }

        public void setIp(String Ip) {
            this.Ip = Ip;
        }

        public String getMensaje() {
            return Mensaje;
        }

        public void setMensaje(String Mensaje) {
            this.Mensaje = Mensaje;
        }   
}

    
asked by Javier 11.02.2018 в 22:47
source

2 answers

0

The statement that you indicate java.lang.ClassNotFoundException is not thrown in any method of the class java.io.ObjectOutputStream at the moment of the serialization of the written object in paquete_datos.writeObject(...); , of stream . However, SI is launched at the moment of deserialization in class java.io.ObjectInputStream , you can see it here:

Documentation of the readObject () method of the ObjectInputStream class

And this error occurs when the class you are trying to send (in this case you try to send a class called paqueteuno ) is not found at the end of the data flow ( stream ), and this is because at the moment to make a readObject() the class sent is deserialized and if it is not found it will throw the error.

Because of this it is necessary to have a class with the same name, the same variables and with the same serialVersionUID in both packages (I suppose that just as you have a Cliente you also have a Servidor , so the class paqueteuno must be in both packages).

The default deserialization tries to reconstruct the class sent, creating a new instance of that class paqueteuno , so if this class is not found, the error will appear.

    
answered by 12.02.2018 / 17:46
source
0

Solution:

The packaged class that told me that it was not found (error), that class converted it to a jar and then added it to my client and server project, with that it was solved.

Thank you all for your answers.

    
answered by 12.02.2018 в 18:06