tags:

views:

43

answers:

2

Hi People!

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??

THANXS in advanced!! :D

+1  A: 

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.

Joachim Sauer
ah no i don't think so. I already wrote it for my thesis in Ruby. For performance reasons i am now rewriting it in java. And well Ruby is just a bit different than Java :P
A: 

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

That only works, when `Database` is in the default package (i.e. has no `package` directive at the beginning). Using the default package is discourage, because it can easily lead to naming clashes and classes in "real" packages can't use classes from the default package.
Joachim Sauer
well if that will happen (ever), And i don't think it will (because the servers that communicate with the esb are very very very small (just a few methods in the controller) I will move it to a "classes" directory or something and make a package of it. Thanxs for the time and effort Joachim!. it is very much Appreciated!
So this is a proof-of-concept rather than a full-fledged production system? That's different, of course. But still: once your code grows above 1-2 classes, you'll definitely want to start using packages.
Joachim Sauer
yes this is a proof of concept. And i agree when there are more server specific classes in the server1 or server2 etc. then you should create packages/directories for it. However at this time it's just a Main class (for setting up tcp server etc.) and a Controller class for server specific methods. Creating a directory for the Controller would be.. ehm .. unnecessary