Understanding External Jars in Eclipse - java

So I'm fairly new to Java and especially Eclipse, so please excuse my ignorance. I took a project from a server and copied it locally to my machine. When I opened the workspace, I had many errors due to it not being able to find the jars. This makes sense because I don't have the same dir structure as the server I copied from. So if I copy the same external jar's to my machine and get it to compile into a jar and copy it back to the server, will it work? Or will it fail because now the external jar's are in a different place than it is expecting?
Also, down the road should I put the external jars into regular jars to avoid this problem?

You should be OK. Java is using what is called classpath to locate dependencies. The classpath may be different on the development machines, but as long as all the dependencies are on the classpath in the production everything should work.
To avoid issues with the synchronisation of directory structures the most common way is to use Maven - it will manage all the dependencies for you (but you have to manage the pom.xml - the Maven's project descriptor). A little clumsier way is to have the dependencies in the project, however you may end up with many projects having to include same jars, and then there will be version conflicts and so on.
For small projects you can manage dependencies yourself, however larger projects will need a more thought through strategy (like Maven).
In regard to the executable jars, make sure the Class-Path entry in <jarfile>:\META-INF\MANIFEST.MF is correct, e.g. where it references other jars, those jars are going to be there in the production. For example, assume we have ourjar.jar and assume this is a snippet from its MANIFEST.MF:
Class-Path: lib/myteamjar.jar
It will then be expected that a following directory structure is in place:
lib/myteamjar.jar
ourjar.jar

No, the location of the external jars does not mater. What you want to do is put the external jars on your classpath. How you do it depends on how you are running your java code. If you are running it from the CLI using the java command, it takes the classpath as an argument. If you want your code to build/run in Eclipse, you need to right click on your project, select "Build Path" > "Configure Build Path..." Use the "Add JARs..." button to add jars that are part of a project you have open and "Add External JARs..." to add jars that reside outside of the project. See specific documentation for your tool for more details about classpaths.

I would not recommend Maven to somebody who is fairly new to Java and Eclipse. I would forget about Eclipse, too.
You have a packaging and CLASSPATH issue. Focus on that.
What kind of project are you talking about? The answer you get will depend on what type of app you're creating. Is it an executable JAR? Then the right way to do it is to package everything into a ZIP file that's laid out exactly as the CLASSPATH in the JAR manifest expects.
If it's a web app, the right thing is a WAR file, with all the JARs your app needs in the WEB-INF/lib directory.
If you package things properly, you should end up with a single package that has everything laid out the right way. You should be able to deploy it to the server and make it all work.

Related

Where to put Java libraries?

I'd like to know how I can add Java libraries to an Eclipse project on a development machine so that they can be added to an Eclipse project without causing errors when someone who has the library in a different location. For example, one developer might add an external JAR in C:\Java, but another might have the same JAR somewhere else. (Where's C:\Java on Mac OS?) I thought I might set the CLASSPATH environment variable, but I can't figure how to add an external JAR from the CLASSPATH environment variable. I'd like to do this so that it works with any workspace. Is this possible?
This is specifically for use with Anypoint Studio, but I think the same problem would exist with any Eclipse-based IDE.
In general, it's recommended to either embed JARs directly into the project, usually in a /lib folder of the project, as described here; or to use a tool like Gradle or Maven to manage dependencies, both of which have nice plugins to support their use in Eclipse.
Another alternative would be to use a Classpath Variable to refer to the JAR(s), which abstracts the physical location so that it can be set on a per-workspace basis.
I think the best way to add library to eclipse project is creating a directory - lib in your project directory. Then add the whole lib to you eclipse class path. You can follow these step to add a lib to class path -
Right click on project and select properties
Select Java Build Path
click Add Library and create User Library
Now add External Jars to this library create at step 3.
By this a .classpath file is crated in you project directory and the CLASSPATH problem will be resolved
I guess the best way to do that would be using Maven, or a similar build system that can construct your Classpath base on dependencies.
You can add the dependencies to your pom and having the jars in your local maven repository in the machine.
http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
Because in any other approach you will need to maintain everything manually, and when having different OS the path will change.
Eclipse is just the IDE that will help to write code and assemble the project (JAR, WAR, ... ). You can add your external jars from wherever you want, and when you want to export your project (with eclipse) you may choose to package the required libraries into the jar.
However, I recommend always to use maven (or something like ) to avoid this kind of problems.
Part1:(import .jar file as library in Eclipse)
You make a new project to Eclipse(name:Project1)
When you open it you see JRE System Library[java version something]
1.right click on JRE System Library
2.Go->Build Path->Configure Build Path
3.You can see (Up right Corner the button[add jars or add external jars]
*Here i advise you to choose the first(add jars) but..
*First copy(or move) the (name).jar inside the project((example):Project 1)
*Now you can add it with the button(add jars).
*In this way when you finish your project the (name).jar will be
imported inside the project(If you export it as a .jar from Eclipse)
..Now you can call any method of (name).jar just(import it into the class
you want to use and call it)

Building the same project in Eclipse in different jars depending on a profile

I keep server and client code in the same project of Eclipse. Libraries for both of them are included. Images for the client are also added to the build path.
Now I want to generate jars for server and client, that they include only required libraries, and server jar does not have images included.
Is there a way to do this without maven, etc?
Right-click on the project and go to Export. Select jar from the selection tree and in the next tab, remove the code/resources that you don't want included in the jar. Better still if you keep them in separate projects (even if there is no client java code, you can create a resource project).
However, what you're describing sounds a lot like a WAR file. Contrary to a jar, a WAR file is a deployable jar meant to be added to a web hosting application like Tomcat. I don't know if that's suitable for your needs, but more often than not, you want to have both server and client code together. If your project is dependent upon another project, that project will automatically create a jar that will be included in the WAR.
You can divide your sources into three source (and output) folders within the same project:
src-shared
src-server
src-client
Then create a build.xml (Ant buildfile) which creates a jar from bin-shared + bin-server and a jar from bin-shared + bin-client.
Note: One danger of keeping it in the same project is that you can accidentally use client classes from server classes, or vice versa, which will fail at runtime. To fix this, make three projects instead of three source folders.

How to setup automatic classpath update in eclipse?

I am looking for a way to setup automatic resolution in my eclipse project. Lets say all of my jar files are situated in %PROJECT_HOME%/lib folder. So, every time a new jar is added, this should be added to the project classpath also. I am thinking for some way so whenever a jar file is copied in the target location, this new jar will be also available in project classpath.
A similar feature is as in web projects. When a jar file is added in WEB-INF/libfolder, this is autometicaly added to web app library.
What are the options, I do have?
Take your time and learn to use Maven, or a similar build tool. It will take a lot of work, but it will do even more work for you automatically in the long run.

How to add source files of library jars?

I am using Eclipse IDE and its derivative like Spring IDE for Java development.
In a web application project, I add external jars like Spring MVC jars, Apache commons jars etc to the Web App library folder, hence they are automatically added to the build path. There are many jars in the Web App library folder.
I want to create folder in the project and add all the source files (zip/jar) of the libraries included in Web App library folder, so that I can navigate through the source of libraries from the Java editor window. Whenever I add a source zip/jar file to this folder, Eclipse should detect it and use it whenever I want to navigate to the source of a library.
Is the above possible in eclipse?
Note: I know how to add source files
for each individual jar by navigating
to the build path window and
specifying the source location. But
this is very crude way, and I need to
do for every library individually.
Also the drawback is that source path
is absolute, which means if I import
the project into another computer then
I need to create the source path or
even worse I might have to add the
source files individually again.
One way to automagically get the sources for the jars would be some kind of dependency management system. Most people would scream Maven (2/3) by now, but others exist and work well. Maven does have nice Eclipse integration, so that should be a plus.
The downside is that setting up a Maven project just for it's dependency management can seem overkill. Another point is that all the jars you depend on should be "Mavenized" as well.
As far as I know Eclipse wont automatically detect/scan source archive files and link them up to libraries in your workspace in the way you described it.
I agree with #Gressie on using Maven and the Eclipse Maven plugins -- as in that case it's just a matter of ticking a few boxes and Maven will do that for you.
If however your project is not Maven-ized, you can still do this in Eclipse but it's more tedious:
for each one of the jars in your project (which appear under the dependecies section) right click on it and select properties
in the dialog that pops up you have (at least) 2 locations you can configure: java source attachment -- simply browse to your jar with the sources -- and also javadoc location (point it to the jar with javadoc if you want the javadoc to appear as a tooltip when you hover the mouse over one of the classes/methods/etc in that library).

eclipse library bundle - fails to export packages from some libraries

I've created a simple plugin project in eclipse 3.5 that just stores third-party libraries for the use by other bundles in an eclipse RCP application. Worked as expected: I edited the manifest, exported the required packages and added the libraries to the build path (project build path as well as manifest build path).
Some days later I added another jar to that project, did the same steps (exporting a package, adding the library to the build path(s)) but this time I can't import classes from that exported package in other bundles. The package was clearly selectable on the manifest editor but import statements in classes just taunt me with curly red lines. Importing classes from other packages exported by the bundles still works, only classes from the newly added lib remain invisible.
Has anyone has an idea what I might have missed? I'm pretty stuck and have no idea how to convince google to show me the solution to that ugly issue...
Check the following:
Make sure they are in the Build
Configuration -> Order and Export
area, and they are check for export.
Make sure you have the packages
exported in the manifest (PDE Tools
-> Open Manifest -> Runtime tab)
In the same place as above, make
sure the JAR files are in the
manifest classpath.
(as Al says
below), make sure the build
properties has your jars marked for
exclusion (though this is not likely
your issue as you are getting errors
compiling).
If all of this is done (and it's still not working), do a clean build an restart eclipse. Sometimes the Eclipse gets a little confused about this and a restart helps.
Check the build.properties to see if the bin.includes includes your newly added Jar. Without it, it won't be exported by the build process, and thus won't be able to use it in dependent bundles.

Categories