What is a dependent JAR and what is a source JAR ? Why are they needed ?
I saw it in the sentence:
Dependent jars can be found in the libs folder and the corresonding source jars can be found in the libs-sources folder.
It would depend on exact context. Most likely:
Dependent JAR - is a java library file the application depends on, common example would be third party libraries. It contains .class files.
Corresponding source jars - java source code of the dependent jar packaged with jar utility. It contains .java files.
Related
Inside some aar archives their is a libs subfolder with some .jar in it. What the purpose of those jars? why they are not merged in the classes.jar? If I add the aar as a dependency of my android project, how the jar inside the libs subfolder will be included in the final apk?
The JARs in the libs directory of an AAR are dynamically linked during runtime. Exactly like the JAR files you would provide in the libs directory of your project.
As you may note, the content of an AAR file is similar to the structure of an Android application project -- at minimum a manifest file, and further (optional) files and directories. You may find resources (res and/or assets), libraries (native ones in lib, JARs in libs) and so on. Not every AAR files does even contain a classes.jar, as some of theme are only intended to deliver additional res content. That can often be found in compat-AARs.
Basically, during the APK building process one simple thing is made with an AAR file which has been included into the project. It will be unzipped and its files and directories are merged -- at least logically, not necessarily physically -- with their corresponding counterparts in the project. So in your case, the JAR files in the AAR's libs directory are added to the libs directory of your project, where you keep the Java libraries you want to link dynamically during application runtime.
Apart from that the APK building process runs as usual.
In short -- An AAR is a ZIP whose content is merged into the project it was added to.
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.
I have been looking around for some time now, but didn't a find way how to export a JAR (not runnable jar) that contains in it's build path the referenced libraries.
Using Eclipse, I have included the lib folder which contains the jars of the referenced libraries in the export process.
Importing that JAR to another project and calling some method results in a ClassNotFoundException.
Looking at the MANIFEST, I didn't see any reference to those jars in the classpath, though the jars are indeed included in the jar.
So my questions are:
1. Is there any way to accomplish the packaging of the non-executable JAR so it will include libraries?
2. Is there any best practice for building and deploying a jar that include other jars libraries?
I tried it too but it doesn't work for me. I added the final .jar file but it doesn't work.
So, I did a workaround.
Extract the .jar file that you want as a dependency.
Copy that content and put it all inside your .jar file.
Add your .jar file as dependency inside an eclipse project.
Run it and see if everything is ok.
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.
I would like to know the difference between extracting and packaging libraries into a jar file from eclipse with the runnable jar file creation.
If my program (runnable jar) uses other classes which require these external libraries(jars), what should I pick?
If you want to put jars into your generated jar file, you can use packaging method.
For example if you are using an Apache library or some other 3rd party jars, you may want to keep these jars preserved in your generated jar. In this case, use packaging.
"Packaging required libraries into a jar file" option puts classes of org.eclipse.jdt.internal.jarinjarloader package into your generated file and this package is just under the root directory of the generated jar file. This option also creates a larger jar file in terms of size due to jar loader classes of Eclipse.
Extracting required libraries will result in putting classes of 3rd party libraries into your jar file by following the package naming convention, e.g. if you open your jar content you can see some classes under org.apache.. packages.
Main class entries are different between the MANIFEST.MF files of these jar files:
Main class entry when you package required libraries:
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
Main class entry when you extract required libraries:
Main-Class: YourMainClass
For my use, the principal difference is that packaged JAR files are included intact as a distinct item, hence retaining their copyright information and signature data.
If you choose extract, the class files are pulled out of their original context and stored as if you had originated them, hence possibly violating some licence conditions, although the size of the final JAR will be smaller in this case. Eclipse does warn you about licensing in this case, too.
So, if using third-party JAR libraries, it's professional to always package.
Try it both ways, and open the resulting jar files with a Zip program. Very instructive.
These are mainly two ways to export as a Runnable jar in eclipse.
1). Package required libraries into jar
This will add actual jar files of the libraries into your jar.
This is the cleanest since it separates application class files with
library JARs.
The downside is this makes runnable jar perform very slow.
2). Extract required libraries into generated jar
This way will extract only the actual class files from the libraries
that your application uses and includes in the runnable jar file.
As a result your Jar file will include your application class files
as well as class files of all the libraries used by your application.
This method makes runnable jar perform just like it is run in your
eclipse IDE.