Cannot compile all classes separately in the same

2019-07-09 02:25发布

问题:

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.

回答1:

1) Compiling class by class is not required.
In fact you don't execute the command in the correct working directory.
You should not execute javac from the demo package but rather in the parent dir that contains it.
And it should be fine :

D:\eclipse-workspace\Shildt\folder\javac demo\*.java should be fine

2) As you compile with javac (or even as you execute the java command), you have to be aware of the classpath value that by default is the current directory where the command is executed.

So this D:\eclipse-workspace\Shildt\folder\demo>javac AddServerIntf.java compiles but that D:\eclipse-workspace\Shildt\folder\demo>javac AddServerImpl.java doesn't compile for the same reason : you execute javac from the demo package, so it means that only the content of the demo directory will be added in the classpath by default. The default classpath you need is a classpath that contains the demo directory.
So identical advise : compile your classes from :

D:\eclipse-workspace\Shildt\folder

and not from :

D:\eclipse-workspace\Shildt\folder\demo