Gradle include compiled Java classes as a module dependency - java

I have been given a directory of compiled .class files in Java, which I must include as a dependency when building my JAR. I am building this JAR using Gradle. How do I include this directory as a dependency in Gradle?
Note: This is a directory that looks like 'com.example.projectname' with files like 'file1.class', 'file2.class', etc. I want these .class files to be in my JAR.

You can simply add any files to the JAR file of your project by configuring the respective jar task (created by the java plugin) :
jar {
from files('path/to/file.class', 'path/to/otherfile.class'
// or
from fileTree('path/to/dir') {
include '**/*.class'
}
}
Please note, that those classes are part of the classpath when using the JAR, but they won't be available when using an IDE, as Gradle does not know these files in its own understanding of either source sets (for compilation) or configurations (for dependencies).

Related

Where are the custom class loader generated by Eclipse when using a jar inside a jar?

With Eclpise (and other tools), it is possible to include a dependency jar inside another jar (see this answer). When Eclipse do that, it generates a custom class loader, because "classic" class loaders are not able to find a class in a jar that is inside another jar.
To create my jar (package.final.jar), I :
- Imported the jar to include (dep.jar) inside a libs/ folder in my project ;
- Added dep.jar in MANIFEST.MF -> Runtime -> Classpath (so it added the line Bundle-ClassPath: libs/dep.jar,. to my manifest) ;
- Exported my project as a deployable plug-ins and fragments.
And package.final.jar contains only this structure :
-META-INF/MANIFEST.MF
-package/-class1.class
-class2.class
.
.
.
-libs/dep.jar
So I am wondering, where is the custom class loader created by Eclipse ?
For Eclipse plug-ins the Bundle-ClassPath entry in the plug-in's MANIFEST.MF tells the Eclipse / OSGi system which classes and jars in the main plug-in jar are part of the class path.
There is no extra code added to the plug-in jar, the Eclipse class loaders deal with the included jars.
So you can build the jar however you like as long as the MANIFEST.MF is correct. Using maven + Eclipse tycho is common these days.
You should have the following folder inside your jar:
org/eclipse/jdt/internal/jarinjarloader
Containing the Classloader etc.:
JIJConstants.class
JarRsrcLoader$ManifestInfo.class
JarRsrcLoader.class
RsrcURLConnection.class
RsrcURLStreamHandler.class
RsrcURLStreamHandlerFactory.class
You can also check it if you look into the META-INF/MANIFEST.MF-File to see what Main-Class is set

How to include a jar file that has dependent jars with it as a dependency itself in an another project?

I just want to know how to include a jar file that has all dependent jars with it, as a dependency itself of an another project. I have tried export as runnable jar option and though it does work when I run the project as standalone, however I get noclassdeffound errors when I include the jar itself as a dependency for another project. To summarize suppose I have project A which depends upon some external jars a.dep1 and a.dep2 I include them in the jar by exporting the project A as a runnable jar file. Now I wish to use project A itself as a dependency in project B and for that purpose I include the jar of project A in my project B. But when trying to run I get the noclassdeffound errors. I don't want to use maven plugins. Is this possible?
for such cases you should be using maven
then you need to create a fat jar.
a fat jar will include all the dependencies it needs inside .
such a jar can be created using the assembly plugin you can see an example here:
assembly plugin
in general if you are using maven you do not have to do this as maven will bring all the dependencies your jar needs based on the pom file of your first jar.
A jar is just a collection of files; you have free rein on what you want to include in it via the command line. https://docs.oracle.com/javase/tutorial/deployment/jar/basicsindex.html should teach you what you need to know

Create JAR file using Net Beans including all dependent libs and folders

I have created a JavaFX application using NetBeans IDE and below is my folder structure.
I want to a build a single jar file including all dependencies for this jar to work properly.
This jar requires testplanner and batch folder from project root directory and files inside dist folder to work properly.
How can I package all this to a single jar file?
Theoretically JAR files cannot contain dependencies within, as java does not support it out of the box. WAR and EAR archives can. What You want to do is not standard, but is named fat jar. Fat jars are used i.e. by spring-boot maven plugin, but you could try this:
https://dzone.com/articles/how-build-fat-jar-using
And some more explanation:
NetBeans - deploying all in one jar
Use tecreations Deploy. Put all your sources into a path declared as Tecreations.getProjectPath(), run BuildAll to create your corresponding classes, put your jars in projectpath/jars and select the appropriate settings, whether to include jars, sources or classes. Select your main class and click Deploy. Unsigned and signed output jars are produced in user/Documents.
Download: https://tecreations.ca/java/downloads/release.

Creating jar issue

This question may be theoretical but I could not find any proper solution.
Suppose I am making a module which uses 3 jar file(hibernate,log4j,jackson).
Now I want to compile my module and create a jar such that my module can be used by any other module and that module should not require the three jars(hibernate,log4j,jackson) to again.
i.e my module jar file should not have any dependencies.
I am using eclipse.I am able to create a jar(project->export->jar) but it does not include the jars in it
Guide me how can I do that.
Is apache ant of any use here?
Eclipse's Runnable Jar Wizard
Eclipse's Runnable Jar Wizard (File → Export… → Java → Runnable Jar File) allows developers to create executable jars from an existing run configuration:
The wizard includes 3 options for handling dependencies:
Extract required libraries into generated jar: unarchives library
dependencies and repackages them into your executable jar. This
option has the advantage of simplicity and does not require a custom
class loader. However repackaging library jars can cause other
problems and does not preserve the signatures of signed jars. This
option may also violate the license terms of the libraries you are
using.
Package required libraries into generated jar: creates a "fat jar"
with a custom class loader. The resultant jar contains
the application's classes and resources
library jars required to launch the application
a small custom class loader that knows how to find jar libraries inside another jar archive
Copy required libraries…: creates the application archive and copies
any required library dependencies to the destination folder.
I think the second option suits your present purpose.
You will need to include all classes from your dependencies into your jar file.
Since a jar file is merely a zip file, you can use any archive manager such as Winzip to explore them, then copy the contents of the jars you depend on into your own jar, taking care to keep the directory structure intact.
That way you have everything in one jar.

JSPF is not loading lib of jar

I am using Java Simple Plugin Framework. I export a jar that has my plugin implementation. The implementation depends on a library, which I have as a jar. That jar gets exported within the lib directory of my jar, and added to the classpath of my jar.
But when I load my jar with JSPF, it fails with "NoClassDefFound" because it can't find the jar in the lib director of my jar.
My apologies if my approach off base; I just need to know how this is supposed to be done. How should I bundle my plugin implementation as a jar if it depends on another jar?
I used JSPF and achieved this requirement the following way:
place the library jar file in a folder called lib outside the plugin jar file. (So that
the lib folder and the plugin jar file is in the same folder). Then I added lib/"name_of_libjar" to the classpath entry in the manifest.mf file of the plugin jar file (Which should be inside the plugin jar files META-INF folder), and it worked fine for me.

Categories

Resources