Java RMI,无法找到存根类,java.rmi.ServerException:服务器线程中发生了RemoteException;

时间:2012-12-25 21:24:27

标签: java client-server rmi stub

我正在尝试运行我的rmi程序。但它似乎无法找到我的存根类。我执行命令'start rmiregistry',它成功启动。 然后做完:

java -classpath . -Djava.security.policy=AllSecurity.policy RMIAuctionManagerImpl 

我收到以下错误:

Failed to register object java.rmi.ServerException: RemoteException occurred in
server thread; nested exception is:
    java.rmi.UnmarshalException: error unmarshalling arguments; nested excep
tion is:
    java.lang.ClassNotFoundException: RMIAuctionManagerImpl_Stub
java.rmi.ServerException: RemoteException occurred in server thread; nested exce
ption is:
    java.rmi.UnmarshalException: error unmarshalling arguments; nested excep
tion is:
    java.lang.ClassNotFoundException: RMIAuctionManagerImpl_Stub
    at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:419
)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:267)
    at sun.rmi.transport.Transport$1.run(Transport.java:177)
    at sun.rmi.transport.Transport$1.run(Transport.java:174)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:5
53)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTranspor
t.java:808)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport
.java:667)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.
java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
.java:603)
    at java.lang.Thread.run(Thread.java:722)
    at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Stream
 RemoteCall.java:273)
    at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:
 251)
    at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:377)
    at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
    at java.rmi.Naming.rebind(Naming.java:177)
    at RMIAuctionManagerImpl.main(RMIAuctionManagerImpl.java:133)
Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested ex
ception is:
    java.lang.ClassNotFoundException: RMIAuctionManagerImpl_Stub
    at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
    at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:409
)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:267)
    at sun.rmi.transport.Transport$1.run(Transport.java:177)
    at sun.rmi.transport.Transport$1.run(Transport.java:174)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:5
53)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTranspor
t.java:808)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport
.java:667)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.
java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
.java:603)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.ClassNotFoundException: RMIAuctionManagerImpl_Stub
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at sun.rmi.server.LoaderHandler.loadClass(LoaderHandler.java:453)
    at sun.rmi.server.LoaderHandler.loadClass(LoaderHandler.java:184)
    at java.rmi.server.RMIClassLoader$2.loadClass(RMIClassLoader.java:637)
    at java.rmi.server.RMIClassLoader.loadClass(RMIClassLoader.java:264)
    at sun.rmi.server.MarshalInputStream.resolveClass(MarshalInputStream.jav
a:216)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:159
3)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1514)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1
750)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
    ... 13 more

我已经厌倦了在不同的计算机上,我没有遇到这个问题,它似乎发生在我的笔记本电脑上,我的环境变量有问题吗?我查了一下,我的环境变量和其他PC一样,我厌倦了这个。

以下是我班级的代码:

import java.io.Serializable;
import java.util.ArrayList;
import java.rmi.*;
import java.rmi.server.*;
import java.sql.*;


public class RMIAuctionManagerImpl extends UnicastRemoteObject implements          RMIAuctionManager, Serializable {

protected ArrayList<RMIAuctionItem> itemList ;
protected ArrayList<RMIBidder> bidderList;

public RMIAuctionManagerImpl() throws RemoteException
{
    itemList= new ArrayList<RMIAuctionItem>();
    bidderList=new ArrayList<RMIBidder>();
}

public ArrayList<RMIBidder> getBidders() throws RemoteException
{
    return bidderList;
}

public ArrayList<RMIAuctionItem> getItems() throws RemoteException
{
    return itemList;
}

public RMIAuctionItem findItem(String itemName) throws RemoteException
{
    for(int i =0; i<itemList.size();i++)
    {
        if(itemList.get(i).getName()==itemName)
        {
            return itemList.get(i);
        }
    }
    return null;    
}
public void add(RMIBidder aBidder) throws RemoteException
{
    bidderList.add(aBidder);
}
public void sendMessageToServer(RMIAuctionItem aItem, RMIBidder aBidder, double theMessage) throws RemoteException
{
    Connection conn =null;
    PreparedStatement prestmt = null;

    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    } catch (ClassNotFoundException e) {
        System.out.println("Driver Not Found");
            e.printStackTrace();
    }



     try {

         conn = DriverManager.getConnection("jdbc:odbc:RMIAuctionHouseDB");

    } catch (SQLException e) {
        System.out.println("Connection Not Found");
        e.printStackTrace();
    }

     try {
            String insertSQL = "Insert into RMIAuctionHouse (ItemName, ClientName, BidAmount) VALUES (?,?,?)";
            prestmt = conn.prepareStatement(insertSQL);
            prestmt.setString(1, aItem.getName());
            prestmt.setString(2, aBidder.getName());
            prestmt.setDouble(3, theMessage);

        } catch (SQLException e1) {
            System.out.println("Statement Not Found");
            e1.printStackTrace();
        }


         try {
             prestmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("Sql Query Not Found");
            e.printStackTrace();
        }

        try {
            prestmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        aItem.makeBid(aBidder, theMessage);
}

public void joinBidding(RMIBidder aBidder,RMIAuctionItem aItem) throws RemoteException
{
    System.out.println("Bound Client: "+ aBidder.getName()+ " to "+ aItem.getName());
    aItem.joinBidding(aBidder);
}

public void setItems() throws RemoteException
{
    RMIAuctionItem aItem=new RMIAuctionItemImpl();
    aItem.setName("Ps3");
    aItem.setPrice(200);
    RMIAuctionItem aItem2= new RMIAuctionItemImpl();
    aItem2.setName("Macbook");
    aItem2.setPrice(500);
    RMIAuctionItem aItem3=new RMIAuctionItemImpl();
    aItem3.setName("HTC Desire HD");
    aItem3.setPrice(100);
    itemList.add(aItem);
    itemList.add(aItem2);
    itemList.add(aItem3);
}

public void deleteItems() throws RemoteException
{
    for(int i=0; i<itemList.size();i++)
    {
        itemList.remove(itemList.get(i));
    }
}


public static void main (String[] args)
{
    try{
        RMIAuctionManager myRMIAuctionManager=new RMIAuctionManagerImpl();

        Naming.rebind("RMIAuctionServer", myRMIAuctionManager);

        //myRMIAuctionManager.deleteItems();
        //myRMIAuctionManager.setItems();

        System.out.println("Remote object bound to registry");
    }
    catch( Exception e){

        System.out.println("Failed to register object " + e);
        e.printStackTrace();
        System.exit(1);

 }
}
   }

感谢您的帮助。

3 个答案:

答案 0 :(得分:4)

确保从有权访问您的类的目录(bin / root文件夹,而不是包文件夹)中启动rmiregistry

答案 1 :(得分:1)

RMI注册表无法找到该类。因此,笔记本电脑的注册表设置存在一些差异。

答案 2 :(得分:0)

这听起来像是一个类路径问题。我要做的是在代码启动时打印出java类路径。然后比较不同机器上使用的类路径。其他计算机可能有一个类路径,其中包含您的程序在您的电脑上寻找的缺少的类或jar文件。

Java Tips - How to print classpath