What are the differences between javadoc.jar, sources.jar and .jar? - java

Looking at Google gson 2.8.5 , I see several jars are distributed here https://repo1.maven.org/maven2/com/google/code/gson/gson/2.8.5/
gson-2.8.5-javadoc.jar
gson-2.8.5-sources.jar
gson-2.8.5.jar
By reading other posts, I understand that sources.jar contains source code, but jar contains the compiled class files.
Does this mean that, given the sources.jar, I can generate the jar myself? What is the general relationship between these three jars?
What is javadoc.jar? Does it only contain documentation, or source code / compiled classes too?

Does this mean that, given the sources.jar, I can generate the jar myself?
Yes, you could extract the Java code from the sources.jar using the jar command.
E.g.
jar xf gson-2.8.5-sources.jar
And than compiling the Java files using javac.
But you need to have all the referenced dependencies in your classpath which are required when you call javac. These dependencies can be found in the project pom.xml
What is the general relationship between these three jars?
The .jar file contains the compliled code which is contained in the sources.jar. So using the sources.jar you could create the .jar yourself (as mentioned having the required dependencies).
The javadoc.jar contains a static html site which content is extracted from all the javadocs which are present in the Java source files.

Related

Usage of jar with .java files and odd behavior of the compiler

I was curious about the differences between .jar with .class files and .jar with .java files. I partially got the answer here, But then what is the usefulness of .java files in the jar?
My guess is that the java files in the jar are like an interface that prevents compilation error, because I solved the IllegalAccessError thrown on runtime by replacing jar files with .class with jar files with .java specifically when using Xposed Framework. (Got the hint from this thread.)
Also
Thank you for your explanations and they were helpful. But I want to learn more about the differences in compiler's view, because I am wondering why my app works fine even if I only included the jar with java files, not class files (zxing). Also there are some cases that throws IllegalAccessException when I include the jar with class files, but not thrown when I include the jar with java files(xposed), even though I have to include at least one of them to make the compiler(AIDE) not complain about references, like unknown package. Why does the compiler not complain when I include only jar with java files though the compiler would not be able to resolve the actual implementation of the referred classes?
A .jar file is basically just a .zip file with another extension.
A .jar file with .class files have a special purpose and may have special meta-data (e.g. in META-INF folder).
A .jar file .java files is just a .zip file.
It is however common for open-source libraries to provide 3 .jar files:
One with .class files, to be used by your code, both to compile and to run your code.
One with .java files, to be used by your IDE, so you can drill into the library code and see it. Especially useful when stepping through the code with a debugger.
One with javadoc files (.html files), to be used by your IDE, so you can read the documentation about the classes and methods in the library. You do read the documentation, right?
None of those 3 files have to be named .jar. They could be renamed .zip so you could easily open them in your favorite Zip utility, or they could be renamed .foo just because...
They should be named .jar, to clarify that they are Java ARchives.
Its simple - *.java files are sources, *.class files are compiled classes.
What is used on runtime by JVM?? *.class files. Why would you put source files inside library? IDK, usally sources are distributed as separate jar, but all in all it is done to allow you to check library code without decompilation.

What is a sources JAR?

I was recently trying to import a library to use for something I am working on.
https://sites.google.com/site/piotrwendykier/software/jtransforms
I was having some difficulty because the JAR that I added to the build path was the "JTransforms-3.0-sources.jar" and not the "JTransforms-3.0.jar".
Now I am sort of scratching my head and just wondering what that first sources.jar was if it didn't have any of the classes that I wanted to use in it.
What is it and what is it for?
That sources jar is a jar that contains only the source code (the .java files) corresponding to the compiled artifact. It is useful to add it as a source attachment in your IDE, to publish the sources, etc.
As it only contains sources, not compiled classes (.class files), it is of no use as a library dependency.
What is it: Source JARs are the JAR file which contains only source code files i.e. .java files, and no compiled .class files. For example, you can download OpenJDKâ„¢ Source files from http://download.java.net/openjdk/jdk8/
What is it for: It is useful for other developers so that they can 'attach source' and debug into their project source code.

Difference between a jar file and a library in Java

NetBeans allows the programmer to add a library and a jar file.
What is the difference between a jar file and a library? Is library similar to GAC assembly as in Windows.
There are similar questions, but they are way too specific and I was not able to understand the difference.
to put things very simple : library is a collection of jars
You could like create a global library java-ee which contains all Java EE related jar files. Then you could use this global library in your different projects. It will be simpler to manage them; and adding in new projects.
A JAR serves the same function an an Assembly in the C#/.net world. It's a collection of java classes, a manifest, and optionally other resources, such as properties files.
A library is a more abstract concept, in java, a library is usually packaged as a JAR (Java ARchive), or a collection of JARs.
A jar file is zip archive containing among other files, the java class files. A Netbeans library contains resources required by the project, including jar files.
If well understood: A library is simply a folder that groups classes. For example in JDK, a library present there is a group of classes stored together.
If not mistaken a .jar file is a group of compiled classes in .class format and was created by Java creators so a program will be OS independent; which means within a JVM you will run your app in .jar format on a Linux, Windows, etc without re-coding tour app for various OSs.
This article explains it all..
It states
Java's libraries are commonly known as class libraries. However, Java
refers to class libraries as packages.

Working with Java JAR files from cmdline

Let us have a Java application, contained in files A.java and B.java with dependency on somejar.jar file. The questions are:
how to create a self-runnable JAR file with all the dependencies and sources compiled within? The main condition is using the standard Java utilities only (given with JDK; e.g.: java, javac and jar) and NOT any of build tools like Maven, Ant or any other.
how to use external JAR files within my application? For example, the algorithm is: if the 'otherjar.jar' is present near the main application JAR, we should call method Moo::method1 from that class, passing the new instance of Foo class to it. Moo and Foo should be present in 'otherjar.jar' file. Still, the 'config.xml' file should be there too.
From the link that #michael667 gave, you may be more interested in this section: Adding Classes to the JAR File's Classpath. In particular this note:
Note:
The Class-Path header points to classes or JAR files on the local network, not JAR files within the JAR file or classes accessible over internet protocols. To load classes in JAR files within a JAR file into the class path, you must write custom code to load those classes. For example, if MyJar.jar contains another JAR file called MyUtils.jar, you cannot use the Class-Path header in MyJar.jar's manifest to load classes in MyUtils.jar into the class path.
It's not a possible thing to do with the standard java tools if you don't code your own classloader. There are tools out there, like One-jar which can provide you of such classloader.
Of course, you could always manually use the exploding-jar approach, but that doesn't seem to be what you really want.
You may also find the answers to this question useful: Classpath including JAR within a JAR
This should answer most of your questions: http://download.oracle.com/javase/tutorial/deployment/jar/basicsindex.html

How to combine library with my jar?

Ok so i wrote a program that makes use of a 3rd party open source library and i want to package it with my program in a single jar. I'm using netbeans 6.8 and everything I've tried java always spit back the error:
java.lang.NoClassDefFoundError: libraryname;
off topic:also i would like to know how to make an executable-jar(exe) through netbeans if it is possible. (ive seen programs that were written in java but were an .exe)
EDIT discovered a plugin for eclipse called FatJar which can do what i want, but i cant find something similar for netbeans, is there such thing?
I'll start off with the obligatory disclaimer: Java executable JARs do not work this way. An executable JAR has a main class defined in the JAR's MANIFEST.MF file, and the MANIFEST also allows the definition of a class path to include libraries that the code in the executable JAR will need. The class path definition in the MANIFEST must enumerate every JAR or folder to put on the class path, relative paths are relative to the location of the executable JAR - not to paths contained inside the executable JAR. Executable JARs are launched with the "-jar" argument to the java executable, and both the java "-cp" flag and the CLASSPATH environment variable are ignored. As for why executable JARs were designed this way, you should be aware of the primary disadvantage of loading classes from JARs contained within JARs, even though the rest of this reply will focus on doing just that.
NOTE: I lost the original sun forum topic that explained it fully, but essentially it is because entries in the top level JAR can be read in a random access manner, but the entire embedded JAR must be read before any entries can be accessed, because the top level JAR might have compressed its entries.
I have used One-Jar successfully in the past, but the structure of the final resulting jar may not be what you expect. Essentially the One-Jar classes are the only non-JARd classes in the final jar; all other code (your code and any dependent library code) is included in the resulting as JAR as JAR files. Your application is JARed as a regular JAR file named "main.jar" in the final JAR's "main" folder. Any libraries your code needs is placed, as JAR files, in the final JAR's "lib" folder. And last but not least the final JAR's MANIFEST.MF file tells One-Jar what your main class is. Execution is a dead simple "java -jar final.jar [args your app uses]". I don't know how to take the next step of converting to an OS-native EXE regarding your off-topic question, but it would probably be best to use a different packaging mechanism than One-Jar anyway. I'm not sure how to go about this with NetBeans, my advice there is to use a build tool to package the final jar. Fortunately One-Jar provides instructions on generating the final jar with Ant, and that should be easily integratable into NetBeans.
I believe the Eclipse FatJar plugin creates a One-Jar executable JAR, so if that plugin seems to do what you want, then One-Jar is the way to do it. Personally, I used a Maven assembly.
There is a caveat - any signed libraries that require (or desire) to take advantage of Java's signed JAR verification may not work this way - Java Cryptographic Extension (JCE) implementations like BouncyCastle are a notable example. I think the reason is that the signature verification runs against the final JAR, not the signed library. Fortunately One-Jar allows the end user to add additional libraries to the classpath, something that is explicitly precluded when running an executable JAR; to workaround this you might be better off delivering the problematic JARs with the final JAR and an OS dependent launch script (.bat, .sh, etc).
I realize that this doesn't achieve exactly what you want, but I'll describe the customary method of distributing a standalone application. If it does meet your needs, you'll find that it's better supported by tools and more readily understood by users, because it follows established conventions.
Put your code in a jar (I'll call it app.jar) along with a META-INF/MANIFEST.MF file with entries like this:
Main-Class: com.y.app.AppMain
Class-path: third-party.jar blort.jar foo.jar
Then, you can throw all of the jars into a directory and run AppMain like this:
java -jar app.jar
If you want, you can put the third-party libraries in a single directory like lib and refer to them in the Class-path attribute using a path relative to the main jar: lib/third-party.jar That helps keep your distribution tidy.
My generic answer to your off-topic question is a (rather lengthy) article: Convert Java to EXE - Why, When, When Not and How. It has lots of links to free and commercial tools, but I have never seen a Netbeans plugin with such functionality, sorry.
To include another jar in your jar, you might find jarjar useful.
Executable jars just have a class defined as 'Main', if I'm not mistaken. This may be useful.
If there's not any concern of repackaging 3rd party jars into your final big jar, then this should be the easiest method.
If there are no licencing issues then the most preffered way is to unjar the actual jar and rejar it with your class files in it, to a new jar.
You can simply use the jar cmd itself for this, no big deal!!
if you use MAVEN, use "maven-shade-plugin" plugin. It will compile jar with all dependencies(3rd party and etc.)

Categories

Resources