我正在尝试创建一个客户端-服务器体系结构,我只想使用javac来编译我的代码。我想要这些目录树:

/Project/Server/src/Server.java
                   /ClientConnection.java
               /bin/Server.class
                   /ClientConnection.Class

        /Global/src/Constants.java
                   /Packet.java
               /bin/Constants.class
                   /Packet.Class

        /Client/src/(it doesn't matter at the moment)
               /bin/(it doesn't matter at the moment)


我想在服务器中而不是在客户端中使用Constant和Packet类,但使用javac却无法做到这一点。我真的不明白包和导入关键字是如何工作的。例如,我不明白为什么下面的代码是错误的:

小型服务器代码段:

import Global.src.Constants;
import Server.src.*

public class Server extends ThreadPoolExecutor{
   private Iterator<ClientConnection> iterator;
   private NodeServer[] map;
   private ServerSocket listener;
   private ArrayList<ClientConnection> clients;
   private HashSet<ClientConnection> clientsDisconnected;


编译命令和相对错误:

javac -sourcepath ~/Documenti/Project Server.java

Server.java:1: error: cannot access Constants
   import Global.src.Constants;
             ^
bad source file: /home/francesco/Documenti/Project/Global/src/Constants.java
file does not contain class Global.src.Constants
Please remove or make sure it appears in the correct subdirectory of the sourcepath.


谁能帮助我拥有前面描述的目录结构?

先感谢您!

贝壳我喜欢您的解决方案,但我不知道该如何应用。我创建了common.jar文件,但我不知道如何导入它。此外,我还不确定是否需要创建一个软件包。你能给我任何澄清吗?

最佳答案

尝试使用多个文件。

javac file1.java file2.java (space is needed b/w file names)


或当前目录中的所有文件

javac *

关于java - javac:如何在多个目录中编译多个类?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45356851/

10-11 22:16
查看更多