简体   繁体   English

javac:如何在多个目录中编译多个类?

[英]javac: how to compile multiple classes in multiple directories?

I'm trying to create a client-server architecture and I want use only javac to compile my code. 我正在尝试创建客户端-服务器体系结构,我只想使用javac来编译我的代码。 I'd like to have these directories tree: 我想要这些目录树:

/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)

I want to use the Constant and the Packet classes either in the server than in the client but with javac I'm not able to do it. 我想在服务器中而不是在客户端中使用Constant和Packet类,但是使用javac却无法做到这一点。 I really can't understand how packages and import keyword work. 我真的不明白包和导入关键字是如何工作的。 For example I can't understand why the code below is wrong: 例如,我不明白为什么下面的代码是错误的:

Small Server code piece: 小型服务器代码段:

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;

Commands to compile and relative errors: 编译命令和相对错误:

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.

Can anyone help me to have the directories structure previously described? 谁能帮助我拥有前面描述的目录结构?

Thank you in advance! 先感谢您!

Seashell I like your solution but I don't understand how to applay it. 贝壳我喜欢您的解决方案,但我不知道该如何应用。 I crated the common.jar file but I don't know how to import it. 我创建了common.jar文件,但我不知道如何导入它。 Futhermore I don't got whether I need to create a package yet. 此外,我还不确定是否需要创建一个软件包。 Could you give me any clarifications please? 你能给我任何澄清吗?

try this for multiple files. 尝试使用多个文件。

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

or for all files in the current directory 或当前目录中的所有文件

javac *

Do it following way. 请按照以下方式进行操作。 In your case, your codes have dependencies (server depends on global, client will probably depend on global and server). 在您的情况下,您的代码具有依赖性(服务器取决于全局,客户端可能取决于全局和服务器)。 You need to compile things in order. 您需要按顺序编译内容。

javac -d Project/Global/bin Project/Global/src/*.java
javac -d Project/Server/bin -cp Project/Global/bin Project/Server/src/*

This way, Global codes will be placed inside bin and will be used during Server compilation via class path. 这样,全局代码将放置在bin中,并将在服务器编译期间通过类路径使用。

In case you want to use javac and you have nested directories inside your sources, you can use find 如果您想使用javac并且您的源中有嵌套目录,则可以使用find

find Project/Global/src "*.java" -exec javac -d Project/Global/bin {} \;

As for the error: 至于错误:

Server.java:1: error: cannot access Constants
   import Global.src.Constants;

I guess you want to have slightly different source structure. 我想您想要略有不同的源代码结构。 Eg 例如

Project
  +-Global
    +-src
      +-some_package

and then, inside Constants.java you can put 然后,在Constants.java中,您可以将

/* Constants.java */

package some_package;

public class Constants { ... }

Then, inside your code you want to import 然后,在您要导入的代码中

import some_package.Constants;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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