简体   繁体   中英

Java Compiling RMI Server/Client Application in Multiple Packages

I'm having a bit of trouble with compiling a Java RMI assignment in packages with the command line interface on Windows 10 (can't use Eclipse or any other IDE). I need to compile and run two .java files - RemoteBankServer.java, and RemoteBankClient.java. Both of these files use other .java classes (Bank.java, RemoteBankImpl.java, RemoteBank.java, Account.java and subclasses of Account.java). Previously, with all of my files in one folder, I could easily compile and run my server and client with two command line windows, with:

 javac RemoteBankServer.java
 java RemoteBankServer

and the same for RemoteBankClient. Now that I've tested that my code works fine, I need to organize them into java packages, following this structure:

- package name: edu.btp400.w2017.client
  class name: RemoteBankClient

- package name: edu.bt400.w2017.server
  class names: RemoteBankImpl, RemoteBankServer

- package name: edu.btp400.w2017.common
  class name: RemoteBank (and all other classes, like Bank, Account, etc.)

I've created those folders (edu/btp400/w2017/) and have placed the appropriate classes in each folder, but I can't get it to compile. For now, I have these two lines at the top of each .java file:

package edu.btp400.w2017.server;   //or .common or .client
import edu.btp400.w2017.*;

But I'm still getting compilation errors when trying to compile RemoteBankServer.java (in the server folder), such as this (which is weird, because RemoteBankImpl is in the same folder and RemoteBankServer:

RemoteBankServer.java:40: error: cannot find symbol
                    RemoteBankImpl b1 = new RemoteBankImpl();
                                            ^
 symbol:   class RemoteBankImpl
 location: class RemoteBankServer

as well as similar errors saying that Account and other classes cannot be found. I've looked at various posts on how to compile with Java packages using the command line interface, but I haven't gotten it working. Any help would be greatly appreciated. Sorry for the long post.

Edit: this is the command I used to compile above (after storing the files in various packages): I tried this in the folder containing edu, and got 27 compilation errors:

javac -cp . edu/btp400/w2017/server/RemoteBankServer.java

and this inside the server folder, and got 15 compilation errors:

javac RemoteBankServer.java

Edit: Thanks to user EJP, I found out that I needed to include the subfolders in my import statements: instead of just import edu.btp400.w2017.*; I needed to do:

import edu.btp400.w2017.common.*;
import edu.btp400.w2017.server.*;
import edu.btp400.w2017.client.*;
import edu.btp400.w2017.*;

That doesn't import all the classes in the subpackages indicated by * . The asterisk only refers to classes.

You need to import all the packages individually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM