searching through .jar files eclipse - java

I'm porting a few java gwt projects into eclipse and the projects depends on many external .jar files, some of the dependencies seem to be dynamically linked so eclipse isn't picking up the missing links while running the ide.
At run time, I often get exceptions that say for example 'import bar.foo.XML' is missing or some FooBar class definition is missing and it always takes me a while to figure out which .jar file these classes/libraries belong to so I can add them to the run path.
Is there a quick way to search which .jar files contain what classes and what other library they depend on?

An oldie, but still good, and integrated into eclipse:
JarClassFinder:
Update 2013: the project still exists in a different form as an independent app JarClassFinder.
In term of Eclipse plugin, it is no longer maintained, and you only have variant like "BundleclassFinder".
Other (manual) ways to find a class within jars: "Java: How do I know which jar file to use given a class name?"

You need a tool like Jar Analyzer

Can't you just mark all the jar-files in Eclipse and right-click->add to Build Path?

Related

How to Make Java .jar files be automatically found in any environment

I am trying to add an external library to my Java project. The library files are in .jar format, and every time someone tries to run the project in a different environment, they have to modify the build path so that their environment can find the .jar files.
Is there any way to make it so that the .jar files are automatically found by the build path? Maybe by specifying where in the project the .jar files are located?
Edit: Here is an example -
In my linux environment, the .jar files are located in:
/home/MyUsername\SomeDirectory\workspace\java_project_name\data
In my windows environment, the .jar files are located in:
C:\MyUsername\SomeDirectory\workspace\java_project_name\data
In the example above, the project root directory is "java_project_name", and the .jar files are always in the java_project_name\data folder. Even though this is the case, different environments can't seem to detect that it is always in the same path in relation to the project's root directory.
I have looked in many places on how to do this, and some people told me it isn't possible. This doesn't sound reasonable to me, which is why I am posting this question here.
Thanks in advance for your help!
you can add a ClassPath: entry in your manifest file of the main jar, and use relative paths. As long as the jars are found relative to the main jar it will work.
This issue was one of my main motivators to start using maven.
#YoK nicely quotes here the relevant reasons to this question https://stackoverflow.com/a/3589930/1493954
Here is one the relevant quotes from Benefits of using Maven
Henning
quick project setup, no complicated build.xml files, just a POM and go
all developers in a project use the same jar dependencies due to
centralized POM.
getting a number of reports and metrics for a project
"for free"
reduce the size of source distributions, because jars can
be pulled from a central location
This is a common problem for developement environments.
I myself use the following solution in netbeans [Hope so that it works in your IDE too]:
Create an Ant variable [for e.g. JAR_LOCATION].
Each and every JAR that you refer, must have it's location relative to JAR_LOCATION.
Now, in every development environment that you're gonna use, you can set up the same ANT variable and it'll automatically pick up the JAR.
You can see the example in the given snapshot of netbeans..

Adding external jar in eclipse

I have a java main project and a java library project which is added as a library in the main.
The library has some .jars with essential content for both (the main and the library). I thought the main project could access these jars, but apparently not. Am I correct?
So... I thought I would have to dupplicate the .jars from the library and put them also in the main project. But this is quite awful. So I found the "Adding external .jar" from eclipse which let me add this .jar from the library, but my question is: "What will happen when I build an unique .jar? Will the .jar be added to the library and also to the main dupplicating itself?"
Also, can I import as a library an open project wihout having to compile it into a jar and be able to edit the project in real time?
Thanks
When you build a unique jar for your own code, it will not include the classes from the other jar. If you want other people to run your program, they will either need that jar themselves, or you will have to distribute the additional jar with your own jar (assuming that's allowed).
You could unpack the jar on which you are dependent and put it in your own jar, but this is unusual and not recommended. If the people who wrote that code corrected bugs and distributed a new jar, people who got their new jar would get the fixes and improvements, but not people who were using your packed version unless you distributed a new one.
I have no idea what your last question means...
You can certainly have one project depend on another, which sounds like what you mean by "can I import as a library an open project wihout having to compile it into a jar and be able to edit the project in real time?".
To share the jars from the library project, go to the "Order and Export" tab in the Build Path dialog, and tick the jars you want to make available.

Eclipse (java) web services, what files to version and how to properly pull the project to a new computer?

Alright, so I have a web service that was created using an eclipse dynamic web project. It is currently shared on a CVS repository, but the versioning system used is irrelevant. At the moment, I have literally NEVER been able to pull this project out as is and get it working. It leads to countless errors that cannot be fixed. Every time I need to work on this webservice in a new machine I have to create an entirely new dynamic project, copy over the source files, add all the necessary libraries and make the deployment assembly work correctly again. After finally making it run I share the project as the same one, stop after a second, and then synchronize again (in a way tricking eclipse into thinking this was the shared project all along).
I feel like others must have run into this problem and found a way around it. So if you have a web service or any dynamic web project, what files do you share, and how do you successfully pull it from the repository and get it to run on another machine besides what I currently do now?
Your help is much appreciated,
-Asaf
Edit: After reading some of the responses I feel that this question is actually more specific to those who use WTP to create/test their web services. Just wanted to add the clarification.
Edit2: Let me also clarify that the other 20 or so projects not using WTP are shared just fine. I am able to pull and run them with no problem. Only web service projects are an issue.
In general, you want to check in everything that's not "derived" (generated or compiled - that's usually the contents of the bin directory or other place where your code is compiled/built into). For Eclipse Java projects, you want to include the .project, .classpath, .settings, and any other similar files that Web Tools might create for Dynamic Web projects. The Eclipse CVS client will ignore files marked as Derived so you shouldn't have to worry to much about it.
Without more detail about what kind of problems you've run into, it's not possible to guess what was causing them. My only guess is that perhaps you had different versions of Eclipse and/or the WTP (Web Tools Platform) plugins installed on the different machine. That's just a wild guess, but could explain some incompatibility when you check out the project from CVS.
Bottom line, checking in those .* files is the long recommended approach from Eclipse gurus. Maven can kind of change things, but you didn't mention it so I'm assuming you aren't using it.
I am primarily sharing my experience, may be you can find some help.
Conceptually speaking, the files which the IDE can generate itself while creating new project should not be pushed. I.e the IDE specific files should not be pushed. And everything which the IDE cannot generate on its own must be pushed.
Forexample in case of eclipse, following files should not be pushed:
.settings
build
.classpath
.project
For setting the project on new machine, first pull the files from server, and then create a project from IDE using pulled files.
EDIT: If your project has external jars/libraries, then you will have to add to the classpath manually. You could also push .classpath but that might give errors while creating a new project.
I think it's easiest to use a build system and let the IDE generate the project from your build system.
Eclipse, Netbeans, and Intellij are all pretty good at building projects from maven or ant build files. With this solution you have a simple build that is easy to setup in CI (Hudson, Bamboo, whatever) and you don't have any IDE specific files checked in. If my workspace is totally different than yours, with different versions, plugins, whatever, I'm not stuck with your project file and you're not stuck with mine. My IDE creates the project appropriate for my environment and your IDE does the same for yours.
Since you mentioned having to manually add libraries, I assume you are not using any build manager (like, maven or ant) besides ecplise.
For ecplise to handle the project properly you need the source files (*.java) in their respective directories, any resources bundled with the web service (e.g. services.xml), the ".project", ".classpath", ".settings", etc. files for eclipse. This should be enough for eclipse to generate anything else necessary to build the project.
Any files/directories that are generated by eclipse during the build process (e.g. target & bin directory, *.class, *.war) should not be checked in -- they will be generated when needed during the build.
I am thinking that, since you are adding the necessary 3rd-party jars manually, these libraries might reside in a different path between computers (e.g. if the path contains the username, it will not be transferable to another computer for a different user). To fix that you can set up the classpath using an eclipse classpath variable. In Preferences->Java->Build Path->Classpath Variables set up a varable linked to the "root" folder where the 3rd party jars a stored. Then add the libraries to the project using this new variable, not their full path. To make it work on someone else's computer, you would only need to set this classpath variable to have the build path point to the correct libraries.
It might be beneficial if you migrated your project from eclipse only to a build manager (e.g. maven) that takes care of many of these issues for you. Eclipse can build a project from the configuration of the build manager, making it easier to manage the project.

How do I import an Android library and use it in both production code and tests?

I've extracted a bunch of functionality from my app into a library. The problem is I'd like to make use of the library classes in both production code and tests. The issue is that my app, my library, and the test code are separate modules, so both the app code and the test code need to depend on the library. When I try to compile the test module, I get the following error:
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: (some class)
It's cryptic, but it's trying to say that I've tried to add the same class to the .dex file more than once. It's not too surprising since the test code depends on the library code and on the app at compile time, which also depends on the library at compile time. How do I set up my dependencies (or change my code) to avoid this?
NOTE: I'm using IntelliJ IDEA 10.5 CE, so I use their terminology, but I think the problem is at least conceptually IDE-agnostic.
It's a bug in IDEA, we've submitted an issue for it, please watch/vote.
Hi I feel the same jar is added multiple times using different ways.
There is two ways of putting jar in your project
Right on Project->Properties->Java Build Path->Add Jars->
Right on Project->Properties->Java Build Path->Add External jars->
first remove all jars. inside your application folder create a folder there put all the required jars.
Right on Project->Properties->Java Build Path->Add Jars-> select your project folder and select the jar file
Thanks
Deepak

Load whole project from Eclipse(about source code)

I am working on buid a plugin of Eclipse. In one step, i load the project through scan classpath,but the feedback from requirement is:
Loading projects encompasses two things:
Loading multiple files from the same
directory/package
Including files from other packages through for instance Eclipse's .classpath files
Since the prototype will be built into an Eclipse plugin anyways, is it fine to use Eclipse stuff, like the .classpath files? you are now loading and reading these files yourself. It might be more convenient to use Eclipse code for this, since it has to be already there.
I have no idea how to deal with it from Eclipse, can anybody help?
I think you're looking for PDE build. You could have a look at this tutorial.

Categories

Resources