1

Serious problems here, I don't know what to do, been at this hours. Getting this error:

java.lang.ClassNotFoundException: User

Here's my code:

public class BookingServerPublisher{

public static void main(String[] args){

    System.out.println("Starting web Service,Press Ctrl + C to stop\n");
    // 1st argument is the publication URL
    // 2nd argument is an SIB instance
    Endpoint.publish("http://127.0.0.1:9876/bs", new BookingServerImpl());
}
}

@WebService(endpointInterface = "bs.BookingServer")

public class BookingServerImpl implements BookingServer{

User users = new User();

public void initData(){
    users.loadUsers();
}
}

@WebService
@SOAPBinding(style = Style.RPC)

public interface BookingServer{

@WebMethod void initData();

}

class BookingClient{

public static void main(String args[]) throws Exception{
    URL url = new URL("http://localhost:9876/bs?wsdl");
    // Qualified name of the service:
    // 1st arg is the service URI
    // 2nd is the service name published in the WSDL
    QName qname = new QName("http://bs/", "BookingServerImplService");
    // Create, in effect, a factory for the service.
    Service service = Service.create(url, qname);
    // Extract the endpoint interface, the service "port".
    BookingServer eif = service.getPort(BookingServer.class);

    eif.initData();
}
}

package bs;

import java.io.*;
import java.util.*;

public class User{

private static final long serialVersionUID = 1;

static ArrayList<User> userList = new ArrayList<User>();    //Dynamic List of all the User objects

String name;
String password;
String level;
boolean isAdmin = false;

User(){
    super();
}

//Method for loading User objects from data file
static void loadUsers(){
    try {
        FileInputStream stream = new FileInputStream("UsersData");
        ObjectInputStream read = new ObjectInputStream(stream);

        while(stream.available() != 0){
            Object obj = read.readObject();
             if(obj instanceof User){
                User users = (User) obj;
                userList.add(users);
            }
        }
        stream.close();
        read.close();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

//Method for saving User objects to data file
static void saveUsers(){

    try {
        FileOutputStream stream = new FileOutputStream("UsersData");
        ObjectOutputStream write = new ObjectOutputStream(stream);

        for(int i = 0; i < userList.size();i++){
            if(userList.get(i) instanceof User){
                write.writeObject(userList.get(i));
            }
        }
        stream.close();
        write.close();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}
}

The package in all files is bs and I'm executing it as java bs.BookingServerPublisher,

java bs.BookingClient from the folder containing the bs folder,i.e. bs' parent directory, the BookingServerPublisher console throws the error.

Anyone can help me with this I really appreciate it.

4
  • ClassNotFoundException is exactly what it sounds. Either the Java-file is not compiled into a class file, or you got the path wrong (or some other problem). Use your IDE to figure out what's wrong with the class for which you receive the error. If it's in different folder, could it be permissions/owner problem ? Commented Aug 20, 2012 at 7:40
  • Thanks for you answer.. the file is definately compiled into a class file and sits in the same directory as the other files in the package Commented Aug 20, 2012 at 7:43
  • did you try running java -cp . bs.BookingServerPublisher? Commented Aug 20, 2012 at 17:09
  • Which line is the exception coming from? If I'm remembering right, ClassNotFoundException implies a dynamic link is failing, not a static link. In other words, it is reserved for when you try to load a class via Reflection, not when your class statically references another class that's not available. In the latter case, you'd get a NoClassDefFoundError. So I assume it's this line? Object obj = read.readObject(); Commented Aug 20, 2012 at 17:20

1 Answer 1

1

I'm assuming it's the deserialization that's failing:

Object obj = read.readObject();

It's failing because the object's fully-qualified class name when it was serialized was User (hence the exception message "java.lang.ClassNotFoundException: User"). But your code base only has a class named bc.User (since it's in the "bc" package). From Java's perspective, these are entirely different and unrelated classes.

You need the same class to be used on both ends of the serialization.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.