Using library in source code in Eclipse?

About 6 years have passed since I was supposed to be engaged in programming in Java, and even longer, since I had to do any noticeable amount of programming in Java. As long as I remember the language, I was always weak in all other things, such as all tools for creating programs, etc. In fact, I forgot more than I remember - and first of all I was taught on my own.

In the past, I based my code organization on what I saw in some open source projects, so I had directories created using something like com / mybiz / util and com / mybiz / network, etc. I would put the source code for the classes in the appropriate directory and make sure that it was in the package that corresponds to this path. Then, if I had to change the code (for example, to fix errors or add a new procedure to an existing class), it was easy for me to change it and recompile the class. As far as I remember, the import for classes in the root directory for my project (all this was connected) for using these classes did not cause problems with this installation.

Then someone told me about Eclipse, but the biggest thing I remember is refactoring. Until that moment, my IDE was a console window and a text editor.

So, I still have many classes in this hierarchy - com / mybiz / util (and so on). But now I use this code for personal libraries, so it is in com / tango / util and com / tango / network, etc. I need to make changes here and there to make the code more universal and remove things that were specific to the business for one reason or another.

I want to use these classes as libraries for my projects in Eclipse. I would rather not just compile and put them in a jar, as many of the classes are still well tuned and need to be recompiled. I would just like to say to Eclipse: "Use this source code kit in the" com / tango ... "directory tree and then just use something like" import com.tango.util.FileUtils "in my source code.

Moreover, I would like to indicate this as a library or some available source code or resource in Eclipse, so that it easily adds (or adds by default) to every project that I create.

Can I do it? Or should I look at something else or another way to handle this? Again, I would rather just include the source code as it is still changing and recompiling.

+4
source share
2 answers

For magic refactoring, you want to use Eclipse to know all the source files to run, so you must add all your source code to the Java Eclipse project.

However, if you want to have a set of classes available for several projects, no one 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 Java Java Creation Wizard (be careful not to click the exit button after setting the project name, but use the Next button, where you can add existing Java projects to the build path).

If all of the source code is available in either one or some of the interdependent Java Eclipse projects, Eclipse will take care of compiling all the classes. Usually Eclipse is smart enough to recompile just what needs to be changed, so this process is really fast (at least in most cases).

I hope this answer will be useful enough - if not, feel free to ask for more information.

Edit : Add Java library support information.

If your "library" project does not change, but you have a jar for it (usually this is the case of a library loaded from the outside), Eclipse allows you to define user libraries - libraries that can be added to build the path of the Java project. To create such a user library, open "Settings", go to the "Java / * Build Path * / User Libraries" page, 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 enter a gigantic size (for example, several million lines of code), I recommend adding the library project as a source in Eclipse as in my experience, which is easier to maintain in the long run.

+4
source

Firstly, I would suggest using IntelliJ (in my opinion, this is much better than Eclipse), but it is quite possible and simple. Thus, to save time, let's imagine that all the classes that you need in the future library are Network.class, FileUtils.class, and Helper.class. First create a new folder on your desktop called "My Libraries". Right-click and select Submit, and then Zip Compressed Folder.

Example

Example Number Two

After that, drag the class files into the folder.

Open Eclipse and select a workspace. Once you do this, you should appear with the default Eclipse screen. Now go to the File tab and hover over New, and then go to your Java project.

Another Example

You will appear with a different screen. Enter a name for your project and click Next. Go to the "Libraries" tab and click "Add External Banks".

Example Three

Now go to the compressed Zip and click "Open."

Example Number Five

Now your library is added.

Here is a small ASCII chart so you can remember:

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

All Articles