Eclipse adding a project with jars to a new project - java

So I want to have a 'utility project' with all the utility classes and the utility libraries in it (say apache commons libraries under a particular directory called libs in the utility project). So that I can add this 'utility project' to a new project and want to use its java classes and jars both. I know when you add a project you can use its java classes but can't use the jars. Anyway to do this?

You want to create a dependency relation between the two projects. This is better than just adding external JARs as it will keep track of the actual libraries in the utility project even if you change its name or location. To achieve that you need to do two things (although, I believe only step (a) is new to you. If I understand correctly you actually have already carried out (b)).
(a) Go to your utility project then do:
Project -> Properties -> Java Build Path -> Order and Export
Check all the JARs that you'd like to make available to the other project.
(b) Go to the other project (the one that uses the utility project):
Project -> Properties -> Java Build Path -> Projects -> Add
Check the "Utility Project"
You should now see your utility project in the list of projects:

In the properties of the project (from Package Explorer > Right Click > Properties) browse to 'Java Build Path' > Libraries tab.
You can then select the 'Add External JARs' button and add the required jars.

right click on the project where you want to add jars and then select properties. After that select Java Build Path and then click on Add External JARs. You should be able to add jars from here. See following pic, for more info.
Note: You can add reference to projects by clicking on projects tab in same window.

I would suggest you try creating a new "user library" in eclipse which you can use in your projects.
An EclipseSW User Library can be used to represent a set of jarW files. This user library can be added to a project's classpathW. Thus, a user library can be a convenient way to add a set of jar files (rather than individual jar files) to a project's build path. Here, I'll create a user library for a set of TomcatSW jar files.
To create a user library, you can go to Window → Preferences and go to JavaSW → Build Path → User Libraries. I'll click the New button to create a new user library.
More on user libraries at this link
http://www.avajava.com/tutorials/lessons/how-do-i-create-an-eclipse-user-library-for-the-tomcat-jar-files.html
Other ways of doing this at the link below
http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-(Java)

Related

Need to add the javax.mail package to my eclipse

I want to create a basic java application where I send and receive an email, but my Eclipse doesn't recognize the javax.mail package which I need for the code.
I've downloaded the package, but I can't figure out how to add it to my Eclipse.
I also would prefer that it gets added to my Eclipse workspace and not just to the one project that I am doing, as I will need the package later for other projects.
How do I install the package? I don't know how to properly use things like class paths and command prompts etc, so I need a step by step help if it requires that.
If that's really what you want to do, go to Preferences -> Java -> Build Path -> User Libraries.
Then press new... give it a name and add the jars, which should be ideally located in an appropriate place that makes sense to you and especially not in the JRE directory.
Then you can use those libraries in any new project. For that go to properties of the project, Java Build Path and add the libraries in the (surprise) Libraries tab.
But I think you would be better off using Maven, Gradle or any other build tool in your projects. However if a library is not publicly available in a repository, and you must download the jars, then this a reasonable option.
Here are some images illustrating the process:
1. How to add new libraries to Eclipse
2. Edit library settings (adding source, javadoc and so on)
3. Add your user library to a project

Java build path, differences between Library tab and Order And Export Tab

What's the difference between Library tab and Order And Export Tab?
I have imported some external jars by adding them in the Library tab. Then they appeared in the Order and Export tab. If I check the boxes on this last tab, I get a duplicate resource error when running my application.
So, what is the purpose of that boxes?
The user Library tab is used to:
Define personal librairies by adding, editing and removing Jars to this personal library, the purpose here is that you don't have to always import all the jars in every project build-path, you just need to put them in a user library(once) and then include this personal library to any project build-path.
And the Order Export/import tab:
Is generally used to set the order of how your librairies will be parsed by the Java Compiler.
Because in some cases the order of Librairies is important and may cause problems.
Take a look here for more information:
User Libraries Preferences
Java Build Path-Order and Export tab
Reading from eclipse user guide
A user library is a set of JAR files. A user library can be added to a projects build path through the build path properties page. The User Libraries preference page allows to define, edit, import, export, or remove user libraries.
Order and export tab is mainly used to export jars to other dependent project. For an example, suppose I have two projects . Project1 has sample.jar added in its libraries, hence it can directly reference and use its resources. There is another project2 dependent on project1 which also needs sample.jar, so either I check the jar in project1's Order and export tab , or add it in project2's libraries. Note that this feature is useful when there are many dependent projects in your workspace.
It is also used to specify the order (by moving libraries up and down) ,as the order of resolution for resources used during project build.

How to add Swing Data Binding to class path?

I'm new to Eclipse and Swing. I wanted to use some Data Binding for my program, and saw here [http://help.eclipse.org/juno/index.jsptopic=%2Forg.eclipse.wb.swing.doc.user%2Fhtml%2Ffeatures%2Fswing%2Fdata_binding%2Findex.html][1]
In order to use Swing Data Binding (JSR 295), you must first add the Swing Data Binding jar (e.g., beansbinding-1.2.1.jar) to your classpath
How do I do that? I couldn't google it out...
One thing you can do is download the jar file itself from http://mvnrepository.com/artifact/net.java.dev.beansbinding/beansbinding/1.2.1
Then, right-click on your project in the Package Explorer and select Build Path -> Configure Build Path... Select the Libraries tab and then click Add External JARs... Navigate to the location you downloaded the jar file to and select it; now it's in your build path.
Note: If you already had a class open in the WindowBuilder when you did this, you'll have to close it and then reopen it in order to see the Bindings tab.
Hope that helps!

The question on configuring build path which involves a lot of libraries

For a newly created project B, I need to build the path the exactly same way as an existing project A. After opening the “java build path” window by clicking “build path--> configure path”, I found that There are a lot of libraries involved in Project A. How to handle this kind of scenario? Add those jar files one-by-one?
If you can locate all those jars on your file system/network, the best thing for you to do is to create an eclipse User Library.
Creating a Library
Window >> Preferences >> Java >> Build Path >> User Libraries
Select New
Give your library a name
Ok
Select Add Jars
Add all the jars you need for your library
With this library created, you can share the same jars between projects easily by importing the library as opposed to each individual jar file.
Importing a Library
For each project that needs a particular library all you have to do is import the library.
Open the project's properties
Select Java Build Path
Select Libraries tab
Select Add library
Select user Library
Select your Library you created
Finish
Note
User Libraries do not belong to projects, they belong to the User but can be referenced by multiple projects.
Using libraries allows you to update your jars only once for all projects that share the same jars.
I hope that helps :D
If you are using Eclipse and you want your new project to have the exact same libraries/dependencies as a previous project, you can copy the appropriate contents of the .classpath file from the old project to the new one.
This would include any classpath elements with a kind="lib" attribute. You may have to update the path attribute if the location is relative.
This assumes you aren't using something like Maven for dependency management...
There are 2 options:
Create a User Library.
Window > Preferences > Java > Build Path > User Libraries
The good thing about this is, you can re-use it provided you need exact set of libraries for other projects.
Bad thing is, if you want to add new JARs to this, you have to modify it. And modifications is .....not difficult, but takes time. Also, if you need slightly different set of JArs for different projects, then either you have to add some not-needed JARs in either/both projects or you have to add this User Library + some external JARs.
EDIT 1
Go into the Eclipse preferences > Java > User Libraries. Click on "New.."
Choose a name for your user library. Leave the "System Library" checkbox unchecked. Push "OK".
Select your library and click "Add JARs...". Navigate to the location where you put the library.
For each JAR file, there are 4 additional parameters you can attach to the JAR: Source location, Javadoc location, Native library location, and access rules. Except the rest 3, Native library location is not an optional field, and must be included if the JAR requires one
OR
Simply add as External JARs.
Good thing: Simple, straightforward.
Bad thing: If you need too many jars, you have to keep track of each separately.

How do you "install" Simple in eclipse?

How do I use simple in eclipse?
Do I add it to the JDK?
http://simple.sourceforge.net
The web site seem to have allot of info on using the methods but little on setting up the library.
I have used C and C++ to set up external libraries before but the Java set up confuses me.
Thank you!
In java world, in order to be able to use third party libraries, what you need to do is to add them to your project/application's CLASSPATH. In case of Simple, its library file is simple-xml-2.4.1.jar that is located inside a folder "jar" within the simple-xml-2.4.1.zip that you would download from Sourceforge.
Download and unzip the
simple-xml-2.4.1 Archive. (ZIP file).
Note that it has a Jar folder that
contains simple-xml-2.4.1.jar - that
is the library eclipse and other IDES
would use to set up the CLASSPATH
Goto your project in project
explorer.
Right-click on the project name and
select "Properties". Alternately, you
can click on the project name and
type "Alt+Enter". A window will
pop-up.
In the window, select "Java Build
Path". You will see several tabs,
including "Source", "Projects" and
"Libraries".
To add simple-xml-2.4.1.jar to your
project, click on the "Libraries"
tab. From there, you can add either
internal jars or external jars. The
"Add External Jar" would be to add a
jar that's on your computer somewhere
. So choose that button, browse to
where simple-xml-2.4.1.jar is located
and make sure to add it to your build
path

Categories

Resources