I have the "Client-server" programm which has 3 classes and 1 interface. (same code but another issue)
AddServerIntf.java
import java.rmi.Remote;
import java.rmi.RemoteException;
interface AddServerIntf extends Remote {
double add(double d1, double d2) throws RemoteException;
}
AddServerImpl.java
import java.rmi.*;
import java.rmi.server.*;
public class AddServerImpl extends UnicastRemoteObject implements AddServerIntf {
public AddServerImpl() throws RemoteException {
}
public double add(double d1, double d2) throws RemoteException {
return d1 + d2;
}
}
AddServer.java
import java.rmi.*;
public class AddServer {
public static void main(String args[]) {
try {
AddServerImpl addServerImpl = new AddServerImpl();
Naming.rebind("AddServer", addServerImpl);
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
AddClient.java
import java.rmi.*;
public class AddClient {
public static void main(String args[]) {
try {
String addServerURL = "rmi://" + args[0] + "/AddServer";
AddServerIntf addServerIntf = (AddServerIntf) Naming.lookup(addServerURL);
System.out.println("The first number is: " + args[1]);
double d1 = Double.valueOf(args[1]).doubleValue();
System.out.println("The second number is: " + args[2]);
double d2 = Double.valueOf(args[2]).doubleValue();
System.out.println("The sum is: " + addServerIntf.add(d1, d2));
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
In Eclipse I have the structure like:
If I'm trying to compile each .java
file using javac
then getting the errors:
D:\eclipse-workspace\Shildt\folder\demo>javac AddServerIntf.java
D:\eclipse-workspace\Shildt\folder\demo>javac AddServerImpl.java
AddServerImpl.java:6: error: cannot find symbol
public class AddServerImpl extends UnicastRemoteObject implements AddServerIntf {
^
symbol: class AddServerIntf
1 error
D:\eclipse-workspace\Shildt\folder\demo>javac AddServer.java
AddServer.java:9: error: cannot find symbol
AddServerImpl addServerImpl = new AddServerImpl();
^
symbol: class AddServerImpl
location: class AddServer
AddServer.java:9: error: cannot find symbol
AddServerImpl addServerImpl = new AddServerImpl();
^
symbol: class AddServerImpl
location: class AddServer
2 errors
D:\eclipse-workspace\Shildt\folder\demo>javac AddClient.java
AddClient.java:9: error: cannot find symbol
AddServerIntf addServerIntf = (AddServerIntf) Naming.lookup(Url);
^
symbol: class AddServerIntf
location: class AddClient
AddClient.java:9: error: cannot find symbol
AddServerIntf addServerIntf = (AddServerIntf) Naming.lookup(Url);
^
symbol: class AddServerIntf
location: class AddClient
2 errors
The result is:
Seems like only interface compiled, but why? I can compile all these classes as one. (if putting the code in one file .java and then compile).
Or I can compile all the files at the same time:
D:\eclipse-workspace\Shildt\folder\demo>cd..
D:\eclipse-workspace\Shildt\folder>javac demo\*.java
D:\eclipse-workspace\Shildt\folder>
And in this case is everything fine.
But I can't properly understand, why compiler didn't find classes separately. Explain me, please. I appreciate any help.