4

It's been about 6 years since I had to do any Java programming, and even longer since I had to do any notable amount of Java programming. While I remember the language, I was always weak on all the other things, such as all the tools for building programs and such. In fact, I've forgotten more than I remember - and I was self taught in the first place.

In the past I based my organization of code on what I had seen in some open source projects, so I had directories set up with something like com/mybiz/util and com/mybiz/network and so on. I'd put the source code for the classes in the appropriate directory and make sure it was in the package that matched that path. Then if I had to change the code (like for a bug fix or to add a new routine in an existing class), it was easy for me - change it and recompile the class. As I recall, imports for the classes in the root directory for my project (it was all tied together) to use these classes were no problem with that setup.

Then someone told me about Eclipse, but the biggest thing I remember doing was refactoring in it. Until then, my IDE was a console window and a text editor.

So I still have a lot of classes in that hierarchy - com/mybiz/util (and so on). But now I'm using this code for personal libraries, so it's in com/tango/util and com/tango/network and such. I've having to make changes here and there to code to make it more universal and to remove stuff that was specific to the business for one reason or another.

I want to use these classes as libraries for my projects in Eclipse now. I'd rather not just compile and put them all into a jar, since many of the classes are still being fine tuned and need recompiling. I'd rather just be able to tell Eclipse, "Use this bundle of source code in the "com/tango..." directory tree and then just use something like "import com.tango.util.FileUtils" in my source code.

Even more, I'd like to be able to specify this as a library or some kind of available source code or resource in Eclipse so it's easily added (or added by default) to each project I create.

Can I do this? Or should I be looking into something else or another way to handle it instead? Again, I'd rather just have the source code included, since it's still being changed around and being recompiled.

3
  • Suggestion: for easier compiling (on Windows at least): just make a .bat file with something like jar Classes.jar *.class in it. Commented Jan 1, 2013 at 21:21
  • Another suggestion is to not use Eclipse at all. I've found that the IntelliJ editor is way more user friendly and much less time-consuming. So if you're looking for a way back into Java I really recommend using another editor. Commented Jan 1, 2013 at 21:30
  • @JensEgholm: I had not even thought about that - it's well worth looking into! Thanks! Commented Jan 1, 2013 at 21:38

2 Answers 2

5

For the refactoring "magic" you want to use Eclipse needs to know all source files to execute, so you have to have all your source code added into an Eclipse Java project.

However, if you want to have a set of classes that are available for multiple projects, nobody stops you from creating multiple projects, and setting up dependencies between them. The easiest way to achieve this is to add a dependency in the New Java Project wizard (be careful not to press the finish button after setting up the project name but use the Next button where you can add existing Java projects into the build path).

If all your source code is available in either a single, or some interdependent Eclipse Java projects, then Eclipse will take care of compiling all the classes. Usually, Eclipse is intelligent enough to only recompile what needs to be changed, so this process is really swift (at least most of the time).

I hope this answer is helpful enough - if not, feel free to ask for further information.

Edit: Adding information about Java libraries support.

If your "library" project does not change, but you have a jar for it (typically a case of an externally downloaded library), Eclipse allows you to define User Libraries - libraries that can be added to build path of a Java project. To create such a User Library, open Preferences, go to the page Java/*Build Path*/User Libraries, where you can define libraries that consist of one or more jar files.

However, if you are developing your own libraries, and your project does not go into a gigantic size (e.g. several million lines of code), I recommend adding the library project as source into the Eclipse workspace, as in my experience that is easier to maintain in the long run.

Sign up to request clarification or add additional context in comments.

3 Comments

So with my directory tree of com/tango/stuff..., do I create that as an actual project? Or do I create it as a library or something else?
You create a Java project, and you put that directory tree into that Java projects source folder (by default called src inside the Eclipse project). Then you can create a new project, that will refer to the already defined project, and can reuse all code defined in it.
This did it. I created a project just for my libraries, then moved the com/tango... directory tree into it, refreshed the project and it was there. So now it's in and easily found. Thank you!
0

First, I would suggest using IntelliJ (in my opinion it's much better than eclipse) but it is very possible to do this and simple as well. So to save time lets pretend all the classes you need in the future library are Network.class, FileUtils.class, and Helper.class. First make a new folder on your desktop called My Libraries. Right click on it and hit Send To, then Compressed Zip Folder.:

Example

Example Number Two

Once that's done drag your class files into the folder.

Open up Eclipse and choose a workspace. Once you've done that, you should show up with the default Eclipse screen. Now hit the File tab and hover over New, then go to Java Project.

Another Example

You will show up with another screen. Enter the name for your project and click Next. Hit the Libraries tab and then click Add External Jars.

Example Three

Now navigate to your Compressed Zip and click Open.

Example Number Five

You now have your library added.

Here is a little ASCII Chart so you can remember:

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Folder -> Class Files -> Compressed Zip -> Eclipse -> New Project -> Next -> Libraries -> Add External Jars -> Compressed Zip (Library)

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

5 Comments

Why does it have to be compressed? Also, I'm not on Windows (used to use Linux, now on OS X) - if it's compressed, doesn't that cause trouble when I modify the classes in the compressed files?
No. It has to be compressed so it can be added as a library together.
Doesn't cause any trouble with the classes.
So I can still edit the classes and save them easily? And will Eclipse automatically recompile them when I edit the .java files?
Instead of external jars you could also define User libraries in the same dialog. In that case, you don't have to compress everything, but sadly Eclipse will never recompile these libraries.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.