Tool to remove unnecessary dependencies in a Java project - java

I have a Java project that currently has a lot of JARs in its libraries directory, which are all included in the resulting package when building. I know, however, that some of these libs are never referenced in the project.
Is there a tool that can search for libs that are not referenced within the project? I guess there must be something in that sense.
BTW, an Eclipse plugin would be awesome.
EDIT: I chose to go with ClassDep because it was the only suggestion that worked. However, I'm having some trouble with it: please check this question

Beware of the case that a class is loaded via Class.forName() and not specified as a dependency in the manifest file (there is a Depends-On: attribute that is for that, but many people don't specify it, which breaks tools like this, the bane of my existence when I worked on such a tool).

ClassDep (from Sun, in the Jini development kit) will do this for you.

ClassPathHelper can help you with that.
Espacially the "Not on Classpath View"
This view scans for jars that are not on the classpath (but are under the current project). It provides basic browsing of packages and classes that are available but not on the classpath. This can be helpful when trying to build up a classpath, as you can quickly browse for the missing classes to see which jars contain them.

Not an eclipse plugin, but I believe the "shrinking" functionality of ProGuard is exactly what you're looking for.

I wrote a small eclipse plugin that takes an existing java project from the workspace. For every classpath entry of the projects raw classpath it removes it from the projects raw classpath and builds the project. If no problem markers with severity error appear on the project, it permanently removes the classpath entry from projects raw classpath.
I'm not able to share that plugin, but that is not too much work to implement it yourself with the links to the api given above.

You also can't tell if JARs that you don't import are required dependencies of dependencies. For example, if you use Spring it comes with its own dependencies, even if you don't import or call those classes in your code. I'm ignorant of ProGuard - does it check for those cases?

Related

No source code is available for type com.google.gwt.ajaxloader.client.AjaxLoader;

I am migrating an project someone else built to newer packages and I have traced the root of my errors to this
No source code is available for type com.google.gwt.ajaxloader.client.AjaxLoader; did you forget to inherit a required module?
I am new to GWT and not really sure how it works 100% under the hood so I am having trouble applying solutions to similar posts to solve this error.
go to Maven Repository and get gwt-ajaxloader.jar - it contains both .java and .class files
save the jar to war/WEB-INF/lib folder in your project
right-click on the file in Project Explorer (I assume you use Eclipse), select Build Path -> Add to Build Path
enjoy ;)
GWT needs source code as opposed to .class files to do its work.
So any references you might have from your client code towards any jars, etc, need to also include source code.
In your case, you either need the source of com.google.gwt.ajaxloader.client.AjaxLoader, or remove any references towards the mentioned class, from your GWT code.

Working with and importing external libraries / frameworks in Java

Firstly this is a very n00b question. But being a junior dev I've never needed to import and work with other Java Frameworks. The standard library was always good enough for me to write the classes I needed to write.
But now getting exposed to more "advanced" concepts, I need to start working with external frameworks, e.g. JSON for Java, Apache's HttpClient for java and so on. And I'm looking for a basic understanding on how this works and how to go about importing these libraries so you can start working with the classes...
So my initial understanding is that each of these fraemworks will provide you with a .jar file that contains all the classes for the framework. Which you then import into your project and lo and behold you'll be able to use the classes/library in your project by just importing it e.g. 'import org.json.*;'
Is the understanding correct?
Correct.
You just add the libraries to your classpath and are now able to use classes from these libs.
How you add the libs to your classpath depends on your actual development environment. If you use Apache Maven for example, you just have to define the dependencies (libs) in your projects pom.xml and Maven downloads them automatically for you.
hth,
- martin
EDIT: The following only applies if you are not using automated build-tools like Maven or Ivy
Yes this is correct. To use a third party .jar file, download and place it in a convenient location (either system-wide or project-specific depending on your needs) and then include it in your classpath.
When executing from the command line do:
java -cp /path/to/library:. path.to.main
The :. is necessary so that the JVM will find your main method.
In an IDE you should be able to include the library in your classpath via the options menu.
Then you can just use the third party library like any other:
import name.of.library.class;
//Do something

proper way to configure a project in eclipse for eSWT?

can someone direct me on how to configure a project in eclipse so that i can utilise eSWT.
Just by manually adding eswt-converged.jar gives me build/pre verification errors.
I downloaded this and utilised the jar from the folder and copied it to my project folder and added it to the build path.But it shows me something like
Please help.
EDIT
i did what Neil said and i am getting this now:
I see that in the link you provided, there are a number of other jars that I don't see included in your project. If I had to take a guess, your jar is heavily dependent on these jars which aren't there, so all the classes that it uses which are defined in these jars simply aren't there.
Though, without further information on the nature of the problem, it's difficult to determine anything else I'm afraid.

How to manage Libraries/jar files in eclipse?

I might be missing something but how do you manage Java projects in eclipse that need a lot of Jar files. I know maven manages libraries well if there are new updates but maybe I'm missing something, is there a way that eclipse can update new jar files (it would be especially useful for projects using apache-commons, say).
I don't want to sound like asking for a feature request, but I'm looking at if there are ways to keep libraries jar files that a Java project uses to keep them updated automatically the way maven does. With more languages coming with this type of features, finding the right Jar files probably should be easier than this.
Eclipse doesn't manage your jar versions for you, and as far as I know it won't do any auto-updating of jars that have newer versions out there. There's simply not enough information or infrastructure for Eclipse to recognize that a given jar you've added to the classpath is eligible for updating and that you want it updated.
However, there is a Maven plugin for Eclipse called M2Eclipse, which will read a POM and construct a classpath out of jars it finds in the local repository and any remote repositories you've configured. It behaves largely like Maven does in terms of finding the latest version for a given jar (if you've specified a version range in your POM).
You can create user libraries and change their content when new versions are available. That way you do not at least need to change the build path of every project. Or you can load sources of the libraries from their svn and use their trunk version. Remember that you can select multiple projects and svn update them at once.

How to get Javadoc to include documentation for a sub-project?

I have two projects in Netbeans that are closely linked. They are separate projects because one of the projects is a util package that could be used in the future for another project. Is there a way to configure Netbeans so that the Javadoc generated for the main project will include links to the objects defined in the util project instead of listing the full package path?
Edit: I have found how to do it. In Project Properties > Build > Documenting you can set Additional Javadoc Options. The option to use is -link and then the URL of the documentation directory. However, I can only get it to work with an absolute link and am having trouble getting the relative link to work.
-link file:///A:\B\C\Util\dist\javadoc //this works
-link file: ..\..\..\Util\dist\javadoc //this doesn't works
Both project folders are in directory C, so I think that should be the correct relative path based on the documentation.
I realize that this suggestion lies outside of the implied constraints of your question, but it might be better to use an SCM tool (and not your IDE) to publish javadoc that covers dependent projects. For example, Maven has a plugin specifically designed to publish javadoc for modular projects.

Categories