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.
Related
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
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)
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.
We have an application developed in NetBeans, based on the NetBeans platform. There's a 3'rd party program that we have a runtime dependency on, more specifically a jar in the other progams lib folder.
How should we include the other progam's jar in our classpath?
The recommendation from the other progam's manufacturer is to set environment variable CLASSPATH to include
C:\Progam Files\Other Program\lib\theJAR.jar
And if that's not possible, we should copy theJAR.jar to JRE-HOME\lib\ext
We must not copy theJAR.jar anywhere else, that might cause it to stop working...
Now NetBeans takes no notice of what's on environment variable CLASSPATH. Their recommended way seems to be to make a wrapper, but that would lead to copying the jar, unless there's some way to make a wrapper module that points to CLASSPATH?
At the moment we are copying the jar into JRE-HOME\lib\ext. But then there's unnecessary hassle when we install some java update.
Do you have any solution to this problem? It seems like something that might be simple, but I haven't found the right place to look yet...
Edit: The application is ant-based.
From the documentation for the Module System API's overview of the runtime infrastructure (bottom of the page under the section "Common Problems and Solutions"):
Q: Can my module add a library JAR to the classpath from outside the
IDE [read: platform] installation?...
A: Not easily. You have a few options:
Add an entry to ide.cfg [your app's .config file]. For example:
-cp:a c:\eak\lib\eak.jar This startup file provides the ability to add classpath entries to the IDE's Java invocation.
...
It goes on to list two more options. The second option is the same solution you've come up with. The third is to "partition your module and use a new classloader" which I can't recommend either way since I have no experience doing this (but it's worth a read).
Assuming that this first option is what you are looking for, you will need to add a custom .conf file to your project and point to it in your project.properties file, like so: app.conf=nbproject/my.conf. This will add your custom .conf file to your app's install directory instead of the default config file that is normally added. Of course, you'll need to add the -cp:a c:\eak\lib\eak.jar entry to this custom config file in order to load the .jar.
During development you'll need to add the following entry to the project.properties file: run.args.extra=-cp:a c:\eak\lib\eak.jar. This will pass the command line option to your debug instance.
You can add that .jar file by following the steps below:
In the left side panel named "Projects" open your project and right click on the "Libraries", then select "Add JAR/Folder...".
Then select your .jar file from the location where you have stored it in the next dialog box that opens and then press "Open".
Vola Finished!!! (with the same process you can add other libraries also like JavaCV, JMF,etc)
And Now You Can Use That .Jar File From Your Project Library.
Hope It Helps.
I saw that in Eclipse I can define User Libraries - to make setting the classpath easier (and probably for other reasons as well).
The problem is, that these libraries are only available in the workspace, and if I want other people using the same project to use them - I need to export my user library and they need to import it.
Is there any functionality like this on the project level? I basically need to have a 'classpath group' - can it be done?
If not, is there an automatic way to auto import the user library to the workspace when importing the project?
I'm using Eclipse 3.6.
JDT has the 2 concepts, user libraries and classpath variables. In the classpath variable, you can add jars to your project. Other team members have to fill in the variables in their workspace so their classpath is complete. This is useful when external jars might be in different locations on each team members local file system.
The USER_LIBRARY is a container for adding a logical group of local jars all at once. For example, the JRE_LIB container represents a number of local jars. But as you've seen, it points to a local set of jars meant to be used in multiple projects (as the JRE is added to multiple projects).
Aside from export/import (which you're already doing), I don't believe you can check CLASS_LIBRARIES into a project's SCM. If there was, the preference page would have a "Configure Project specific settings" link at the top.
Your best bet is to simply add the jars to the project, so they'll be included in the SCM. If they can be in different locations depending on the rest of your team, then use a classpath variable so it can be set in each workspace. That's the least amount of hassle as far as team members checking out the project and being ready to go.
The best way IMO is to use m2eclipse - Maven plugin for eclipse. In Maven all the dependencies are defined in pom.xml and downloaded automatically as needed. This means that the only thing you share with your team is pom.xml - your project definition.
There is a lot more advantages when using m2eclipse vs standard eclipse approch. More information is at http://www.sonatype.com/books/m2eclipse-book/reference/
The way I have used user libraries is for something like Ant. Define a user library "ant" for all the jars in ANT_HOME/lib. If including this in your Eclipse .classpath and then sharing with other users, they will get a build problem report until they create that "ant" user library themselves. It's useful, but you need to share knowledge on how to create the library. If you're using it for simple cases like above, then instructions for adding the right jars to the library are straightforward.
Another approach I've used is to build classpaths pointing into a folder (or folders) defined as a variable in Eclipse. See File -> New Folder -> Advanced -> Link to folder in the file system -> Variables. This lets you setup (again at workspace level) variable references to one or more folders. You can then build your Eclipse classpath/s with reference to the folder/s.
So say in your development environment, everyone needs to have a directory called "thirdparty" containing all the external jars dependencies (probably in hierarchy within that dir: thirdparty/apache; thirdparty/sun; ...). You define "thirdparty" as a variable pointing to wherever that dir is on your current system, you create a folder in your project/s using the variable. You can then setup (and share) classpath using paths into that folder.
It's similar to User Library and with similar limitations. The limitation is that the other users you share your project with must create variable folder/s as you have. But it's more flexible in that they don't have to add the jars explicitly as they do with a library; rather, your classpath/s in Eclipse point into the folder, as required for each project.
Note that although the folder variable is defined at workspace level, it can be reused in multiple projects, each of which builds their classpaths (.classpath files) with different references into the folder).
This is maybe something easier to show than to describe with words, but I hope it makes sense.