Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Thursday, November 3, 2016

Is it correct or incorrect for a Java JAR to contain its own dependencies?

Is it correct or incorrect for a Java JAR to contain its own dependencies?


I guess this is a two-part question. I am trying to write my own Ant task (MyFirstTask) that can be used in other project's build.xml buildfiles. To do this, I need to compile and package my Ant task inside its own JAR. Because this Ant task that I have written is fairly complicated, it has about 20 dependencies (other JAR files), such as using XStream for OX-mapping, Guice for DI, etc.

I am currently writing the package task in the build.xml file inside the MyFirstTask project (the buildfile that will package myfirsttask.jar, which is the reusable Ant task).

I am suddenly realizing that I don't fully understand the intention of a Java JAR. Is it that a JAR should not contain dependencies, and leave it to the runtime configuration (the app container, the runtime environment, etc.) to supply it with the dependencies it needs? I would assume if this is the case, an executable JAR is an exception to the rule, yes?

Or, is it the intention for Java JARs to also include their dependencies?

Either way, I don't want to be forcing my users to be copying-n-pasting 25+ JARs into their Ant libs; that's just cruel. I like the way WAR files are set up, where the classpath for dependencies is defined under the classes/ directory.

I guess, ultimately, I'd like my JAR structure to look like:

myfirsttask.jar/      com/  --> the root package of my compiled binaries      config/  --> config files, XML, XSD, etc.      classes/  --> all dependencies, guice-3.0.jar, xstream-1.4.3.jar, etc.      META-INF/          MANIFEST.MF  

I assume that in order to accomplish this (and get the runtime classpath to also look into the classes/ directory), I'll need to modify the MANIFEST.MF somehow (I know there's a manifest attribute called ClassPath, I believe?). I'm just having a tough time putting everything together, and have a looming/lingering question about the very intent of JARs to begin with.

Can someone please confirm whether Oracle intends for JARs to contain their dependencies or not? And, either way, what I would have to do in the manifest (or anywhere else) to make sure that, at runtime, the classpath can find the dependencies stored under the classes/ directory? Thanks in advance!

Answer by Daniel Pryden for Is it correct or incorrect for a Java JAR to contain its own dependencies?


The intent (AFAIU) is for JAR files to behave like native code shared object files (.so on Unix, .dll on Windows). Generally, an application will install several shared object files as siblings, plus an executable with which to launch them.

An executable JAR is more like a standalone executable, and so it is more common to include all dependencies (similar to the way a statically-linked native code executable contains all its dependent objects directly).

Unfortunately, the default ClassLoader is not able to load classes from nested JARs. It is possible to write a ClassLoader that does. Or you can use one someone else has written. From the description of your problem, it sounds like Jar Jar Links is exactly what you're looking for.

Answer by Mentallurg for Is it correct or incorrect for a Java JAR to contain its own dependencies?


"Jar Jar Links" is only good for a standalone applications. But not for Ant. If your project has the same dependencies and they are upgraded to newer versions later on, like xstream-*.jar, then there will be a conflict, and the wrong version may be picked up. In the worst case there will be MethodNotFoundException. That's why it is a bad practice to include dependencies in a single jar.

What's a problem with "I don't want to be forcing my users to be copying-n-pasting 25+ JARs"?

That's the easiest solution. And the best, because you will avoid problems in the future.

Now, when you see the inconveniences of Ant, you might want to compare it to Gradle. With Gradle you get tasks a bit similar to Ant and you don't need to provide any dependency jars. All dependencies for you will resolve Gradle. And like in Ant you still can create your tasks.

Answer by Tom Anderson for Is it correct or incorrect for a Java JAR to contain its own dependencies?


The term 'JAR file' can mean at least two things, or rather, has at least two facets to its meaning. Most basically, it means a container format: basically, a ZIP file with a META-INF directory. More refinedly, it means this container used as a way to package class files.

In the sense of being a container, there is no intent with respect to contents; the file could contain class files, other JARs (in either sense!), etc. But in the sense of being a packaging of code, i believe the intent for JAR files proper is for them not to contain any dependencies.

If you have a read of the JAR File Specification, you'll find there are several allusions to the storage of class files, but nothing about storing other JAR files. Correspondingly, if you look at the implementation of the JAR file classloader in the JRE, it can't do anything useful with nested JARs.

Furthermore, the JAR specification does detail a mechanism for dealing with non-nested dependencies: the Class-Path attribute. This lets a JAR file make relative references to other JAR files in the filesystem.

Now, in-the-sense-of-a-packaging JAR files are not the only use of in-the-sense-of-a-container JAR files. WAR, EAR, and RAR files (and more besides) are all JAR files used for particular purposes. Each of those is capable of containing other JARs: WARs can contain in-the-sense-of-a-packaging JAR files, and EARs can contain those and also WARs. However, those are quite different beasts than in-the-sense-of-a-packaging JAR files. It's worth noting that special classloaders, that are not in the Java standard library, are needed to make use of them.

The way that WARs etc can collect many JAR files together is indeed very useful, and it's a real shame there's no generic mechanism for doing this in Java outside of Java EE. It would be great to have an 'application archive' or 'meta-archive' format that simply bundled some JARs.

So, you're left with this problem of users needing 25 JARs in order to use your plugin. You have roughly two options.

First, you accept the pain, and distribute your plugin as a zip full of JARs, which users will have to unpack.

Secondly, you join the 21st century, and use a build tool and distribution mechanism which handles dependencies automatically: in practice, that means using Gradle, or Maven, or some other tool (such as Ant) in concert with Ivy, to obtain dependencies from Maven Central, and then then releasing your code along with a POM file which lists those dependencies. Users can then download your JAR and your POM, and have their own build tool obtain the dependencies.

If you do go the second route, it might be prudent to also release a zip of the dependencies, for the benefit of users who are not using automatic dependency management.

Answer by Jarekczek for Is it correct or incorrect for a Java JAR to contain its own dependencies?


Some java application vendors use the following scenario to distribute their application which depend on other jars, it reminds of static linking. At the stage of building the jar all dependencies (being also jars) are unpacked. When building the final jar, they include both their freshly compiled classes and the classes extracted from dependencies.

Possible issues:

  1. Applications cannot reuse the libraries as they are contained in the app. Usual static linking issue.
  2. The licenses of the repacked libraries must be respected. Usually it will be ok to repack them, but sometimes additional care must be paid to their license files, which may happen to be inside their jars.

AFAIK it's not possible to have jars inside the jar or it will be impossible to specify a classpath for them. Hence the repacking procedure.

Answer by Christopher Peisert for Is it correct or incorrect for a Java JAR to contain its own dependencies?


Is it correct or incorrect for a Java JAR to contain its own dependencies?

There are use cases where it is correct for a JAR file to contain its own dependencies. If you would like to support users who do not use modern dependency management, you may want to provide a JAR file containing your Ant task code as well as all dependencies. The more powerful, flexible, and modular approach is to publish versioned JAR files to the Maven repository that only contain your project code.

1) JAR file containing your project code and all dependencies

Pros

  • Easy to download and the only setup for end users is including a in their Ant build files
  • No setup required to publish Maven artificats

Example Ant target to build JAR

                                          

Cons

  • If end users of your Ant task already have some or all of the dependencies included in their projects, then they will end up with redundant copies of the dependencies
  • The JAR file could be very large

2) JAR file containing only your project code published to Maven Repository

Pros

  • Users may fetch any version of your Ant task that you have published to the Maven repository, which provides more flexibility in releasing new versions of your task while allowing existing users to continue using previous versions to avoid possible regressions
  • Avoids duplicate copies of common dependencies (except where different versions of a dependency cause errors)
  • JAR file will be small

Cons

Need to learn about the following:


For reference, the Java? Tutorials provide a good summary of JAR files.

Lesson: Packaging Programs in JAR Files

The Java? Archive (JAR) file format enables you to bundle multiple files into a single archive file. Typically a JAR file contains the class files and auxiliary resources associated with... applications.

The JAR file format provides many benefits:

  • Security: You can digitally sign the contents of a JAR file...
  • Decreased download time: If your applet is bundled in a JAR...
  • Compression: The JAR format allows you to compress your files for efficient storage.
  • Packaging for extensions: The extensions framework provides a means by which you can add functionality to the Java core platform, and the JAR file format defines the packaging for extensions...
  • Package Sealing: Packages stored in JAR files can be optionally sealed so that the package can enforce version consistency. Sealing a package within a JAR file means that all classes defined in that package must be found in the same JAR file.
  • Package Versioning: A JAR file can hold data about the files it contains, such as vendor and version information.
  • Portability: The mechanism for handling JAR files is a standard part of the Java platform's core API.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.