java - Java Web 服务的类路径、classNotFoundException

标签 java web-services classpath

这里出现严重问题,我不知道该怎么办,一直在这个时间。出现此错误:

java.lang.ClassNotFoundException: User

这是我的代码:

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();
    }
}
}

所有文件中的包都是bs,我将其作为java bs.BookingServerPublisher执行,

java bs.BookingClient 来自包含 bs 文件夹的文件夹,即bs' 父目录,BookingServerPublisher 控制台会抛出错误。

任何人都可以帮助我,我真的很感激。

最佳答案

我假设是反序列化失败:

Object obj = read.readObject();

失败是因为对象序列化时的完全限定类名是 User(因此出现异常消息“java.lang.ClassNotFoundException: User”)。但是您的代码库只有一个名为 bc.User 的类(因为它位于“bc”包中)。从 Java 的角度来看,这些是完全不同且不相关的类。

您需要在序列化的两端使用相同的类。

关于java - Java Web 服务的类路径、classNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12033733/

相关文章:

java - Intellij-Idea中Openshift应用程序的远程调试

java - struts.xml 标签结果类型的基于注释的配置替代方案

web-services - 处理 REST Web 服务中的操作

java - SLF4J 的 “dynamic binding” 功能什么时候适合使用?

eclipse - 如何设置eclipse忽略 "Classpath Dependency Validator Message"警告

java - 如何使用 java @Configuration Spring 配置以过滤模式运行 jersey

java - 访问 CXF Rest 服务中的 post 方法时出现问题

java - 在此服务器上找不到请求的 URL/REST/WebService/MyMethod

java - 类路径和 java.library.path 之间的区别?在linux和windows下如何设置?

java - 需要重新设计动态输入的现有逻辑