Results 1 to 3 of 3
  1. #1
    soxfan714 is offline Member
    Join Date
    Nov 2008
    Posts
    17
    Rep Power
    0

    Default Need Help with Class Cast Exception

    I start the rmi registry, then I run the server and everything seems to be fine.
    When I run the client, I get this exception:

    Exception in thread "main" java.lang.ClassCastException: $Proxy1 cannot be cast
    to cscie160.hw5.ATM
    at $Proxy0.getATM(Unknown Source)
    at cscie160.hw5.Client.main(Client.java:13)

    This is the server code

    Java Code:
    package cscie160.hw5;
    
    import java.rmi.*;
    
    public class Server {
        public static void main(String[] args) {
    
            try {
                ATMFactoryImpl atmfactory = new ATMFactoryImpl();
                Naming.rebind("//localhost/atmfactory", atmfactory);
                System.out.println("ATMFactory is bound in regsitry");
            } catch (Exception e) {
                System.out.println("Server err: " + e.getMessage());
                e.printStackTrace();
            }
        }
    }
    This is the client code:

    Java Code:
    package cscie160.hw5;
      import java.net.MalformedURLException;
      import java.rmi.Naming;
      import java.rmi.NotBoundException;
      import java.rmi.RemoteException;
      import java.rmi.UnknownHostException;
      
      public class Client {
         public static void main(String[] args) {
            ATM atm = null;
            try {
               ATMFactory factory = (ATMFactory)Naming.lookup("//localhost/atmfactory");
               atm = factory.getATM();
            } catch (MalformedURLException mue) {
               mue.printStackTrace();
            } catch (NotBoundException nbe) {
               nbe.printStackTrace();
            } catch (UnknownHostException uhe) {
               uhe.printStackTrace();
            } catch (RemoteException re) {
               re.printStackTrace();
            }
            if (atm!=null) {
               try {
                  // get initial account balance
                  System.out.println("Initial Balances");
                  System.out.println("Balance(0000001): "+atm.getBalance(0000001));
                  System.out.println("Balance(0000002): "+atm.getBalance(0000002));
                  System.out.println("Balance(0000003): "+atm.getBalance(0000003));
                  System.out.println();
                  // make $1000 depoist in account 0000001 and get new balance
                  System.out.println("Depositting(0000001): 1000 ");
                  atm.deposit(0000001, 1000);
                  System.out.println("Balance(0000001): "+atm.getBalance(0000001));
                  // make $100 withdrawal from account 0000002 and get new balance
                  System.out.println("Withdrawing(0000002): 100 ");
                  atm.withdraw(0000002, 100);
                  System.out.println("Balance(0000002): "+atm.getBalance(0000002));
                  // make $500 deposit in account 0000003 and get new balance
                  System.out.println("Depositting(0000003): 500 ");
                  atm.deposit(0000003, 500);
                  System.out.println("Balance(0000003): "+atm.getBalance(0000003));
                  // get final account balance
                  System.out.println();
                  System.out.println("Final Balances");
                  System.out.println("Balance(0000001): "+atm.getBalance(0000001));
                  System.out.println("Balance(0000002): "+atm.getBalance(0000002));
                  System.out.println("Balance(0000003): "+atm.getBalance(0000003));
               } catch (RemoteException re) {
                  System.out.println("An exception occurred while communicating with the ATM");
                  re.printStackTrace();
               }
            }
         }
      }

    This is the ATMFactory Code:

    Java Code:
    package cscie160.hw5;
    
    import java.rmi.RemoteException;
    
    /**
     * Public Interface ATMFactory
     * getATM()
     */
    public interface ATMFactory extends java.rmi.Remote{
    	public ATM getATM() throws RemoteException;
    }

    Any help would be greatly appreciated.

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    From your tutorial:
    The java.rmi.Naming class provides methods lookup, bind, rebind, unbind and list (returns an array of object name registered on the host: port). The naming service is accessed on a particular host: port combination.
    Source: distance education section of Harvard Extension CSCIE160

    ( note the colon is parsing as a smiley so there is an embedded space that is to be removed )
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    smartino is offline Member
    Join Date
    Sep 2011
    Posts
    64
    Rep Power
    0

    Default Re: Need Help with Class Cast Exception

    Can you provide a little more detail about the answer provided, I am having the same issue...

Similar Threads

  1. Class not found Exception
    By surendra in forum Java Servlet
    Replies: 8
    Last Post: 06-09-2011, 12:52 PM
  2. class cast exception
    By venkatallu in forum New To Java
    Replies: 2
    Last Post: 09-02-2008, 09:50 PM
  3. How to create your own Exception class
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:40 PM
  4. Exception Failed to Generate Wrapper Class on WebLogic
    By christina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 02:15 AM
  5. Replies: 1
    Last Post: 08-02-2007, 04:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •