2

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?

0

2 Answers 2

2

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.

1
  • 7
    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 Mar 19, 2010 at 9:33
1

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

4
  • 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. Mar 19, 2010 at 9:51
  • 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! Mar 19, 2010 at 10:01
  • 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. Mar 19, 2010 at 10:16
  • 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 Mar 19, 2010 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.