Java import from other directory

2019-07-18 18:01发布

问题:

I am building a Enterprise Service Bus (ESB) with Java. I won't get into details But I have to build multiple servers who make use of the same classes.

I have the following directory structure:

/server1
   -Main.java
/server2
    -Main.java
/com
    -Database.java

I want to import from the Main.java class for example the Database.class. But of course the following statements won't work:

import com.Database;

I am working with the javac compiler in the command line (so not eclipse stuff or whatever. just TextMate and the command line). And I found a (pretty stupid) solution by creating a symbolic link in the servers to the com directory. But that is not really an ideal solution.

Does anybody have a better one?

回答1:

Set up your classpath to include the directory that contains the com directory.

And I hate to be negative, but if you need help with that, then writing an ESB is probably above your current skill level.



回答2:

Thanx Joachim. Well if you don't know where to look you won't find the answer on google.. so not able to find the answer by yourself doesn't mean you are a bad programmer.

I found the answer (thanxs to Joachim).

for people reading this:

javac Main.java -cp ../com -cp ./

will do the trick (the last -cp ./ is not necessary but i also have a Controller class that IS located in the same directory and i need to import that one too).



标签: java import