を返す:通過/私はこのエラーを取得しています実行している場合は、私たち自身のタイプ
JavaのMyRemoteClient
java.lang.ClassCastException: $Proxy2 cannot be cast to Account
`enter code here`at MyRemoteImpl_Stub.addAccount(Unknown Source)
at MyRemoteClient.main(MyRemoteClient.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.dynamicjava.symbol.JavaClass$JavaMethod.evaluate(JavaClass.java:362)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.handleMethodCall(ExpressionEvaluator.java:92)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.visit(ExpressionEvaluator.java:84)
at koala.dynamicjava.tree.StaticMethodCall.acceptVisitor(StaticMethodCall.java:121)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.value(ExpressionEvaluator.java:38)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.value(ExpressionEvaluator.java:37)
at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.visit(StatementEvaluator.java:106)
at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.visit(StatementEvaluator.java:29)
at koala.dynamicjava.tree.ExpressionStatement.acceptVisitor(ExpressionStatement.java:101)
at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.evaluateSequence(StatementEvaluator.java:66)
at edu.rice.cs.dynamicjava.interpreter.Interpreter.evaluate(Interpreter.java:77)
at edu.rice.cs.dynamicjava.interpreter.Interpreter.interpret(Interpreter.java:47)
at edu.rice.cs.drjava.model.repl.newjvm.InterpreterJVM.interpret(InterpreterJVM.java:246)
at edu.rice.cs.drjava.model.repl.newjvm.InterpreterJVM.interpret(InterpreterJVM.java:220)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
>
私は、PARAMATERSとしてプリミティブと文字列を使用して同様のプログラムをやったかのタイプを返す、それが正常に動作します例えば、
public String sayHello() throws RemoteException;
しかし
public Greet sayHello(Job j) throws RemoteException;
のようなもののために10
が実行されず、上記のようなエラーが生成されます。
すべてのクラスで[b] Serializable [/ b]を実装しようとしましたが、まだこのエラーが発生しています。ここ
すべてのコードである:
AccountInterface.java
import java.rmi.*;
public interface AccountInterface extends Remote {
public float deposit(float amt) throws RemoteException;
public float withdrawal(float amt) throws RemoteException;
public String checkBalance() throws RemoteException;
public String accDetails() throws RemoteException;
}
[/コード]
Account.java
import java.rmi.*;
import java.rmi.server.*;
import java.io.Serializable;
public class Account extends UnicastRemoteObject implements AccountInterface, Serializable{
private static int nextId = 0;
public int accNumber;
private String name;
private String address;
private float balance;
public Account(String n, String a, float b) throws RemoteException{
accNumber = ++nextId;
name = n;
address= a;
balance= b;
}
public float deposit(float amt) throws RemoteException {
balance += amt;
return balance;
}
public float withdrawal(float amt) throws RemoteException {
balance -= amt;
return balance;
}
public String checkBalance() throws RemoteException{
return (String.format("Balance=Rs.%,.2f\n", balance));
}
public String accDetails() throws RemoteException{
String s = String.format("Name=%s, Address=%s\n",
name, address);
return s;
}
}
MyRemote.java
import java.rmi.*;
public interface MyRemote extends Remote {
public Account addAccount(String n, String a, float b) throws RemoteException;
public int getTotalAccount() throws RemoteException;
public Account getObject(int id) throws RemoteException;
public String deleteAccount(int id) throws RemoteException;
}
MyRemoteImpl.java
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
import java.io.Serializable;
public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote, Serializable {
private ArrayList<Account> acct = new ArrayList<Account>();
public Account addAccount(String n, String a, float b) throws RemoteException{
Account acc = null;
try {
acc = new Account(n,a,b);
acct.add(acc);
} catch(Exception ex) {ex.printStackTrace();}
return acc;
}
public int getTotalAccount() throws RemoteException{
return acct.size();
}
public Account getObject(int id) {
try {
for (Account a : acct) {
if (a.accNumber == id) return a;
}
} catch(Exception ex) {ex.printStackTrace();}
return null;
}
public String deleteAccount(int id) throws RemoteException{
int index = -1;
try {
for (Account a : acct) {
index++;
if (a.accNumber == id) break;
}
} catch(Exception ex) {ex.printStackTrace();}
acct.remove(index);
return "Account deleted.";
}
public MyRemoteImpl() throws RemoteException { }
public static void main (String[] args) {
try {
MyRemote service = new MyRemoteImpl();
Naming.rebind("AccountDirectory", service);
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
MyRemoteClient.java
import java.rmi.*;
import java.util.*;
import java.io.Serializable;
public class MyRemoteClient implements Serializable {
public static void main (String[] args) {
try {
MyRemote service = (MyRemote) Naming.lookup("rmi://127.0.0.1/AccountDirectory");
Account a = (Account) service.addAccount("John", "Texas", 20000); //Dnt know if this is good, tryinh to cast it to Account
Account b = (Account) service.addAccount("Paul", "California", 35000);
Account temp = service.getObject(1);
temp.deposit(1000);
temp.withdrawal(1000);
System.out.println(temp.accDetails());
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
任意のソリューション?おかげ
これを試す:AccountInterface a = service.addAccount –
@Usman:正しい。あなたはコメントではなく答えとして投稿するべきです! –
まだ同じエラー –