Java – Difference between .jar and .dll file

cjava

I am learning Java these days and I've spent a lot of time with .NET so when I want to export or import libraries, they are usually in .dll format which is called assemblies in .NET environment and they are compiled to IL and they can have resources like images, XML, audio and so on, anyways.

I am wondering the same process in Java as well. I've read documents but they actually confused me a little bit and to clarify things out I need your help guys.

Questions:

  1. .NET Assembly is the same thing as Java .jar?

  2. .dll contains compiled IL code and .jar contains compiled .class/byte code files?

  3. They say resources, what kind of resources we are talking about here? Images, .txt files, etc. or all of them possible?

  4. I've examined AWS (Amazon Web Service API for java) and I saw three .jar file and they are

    1. aws-java-sdk-1.1.1.jar
    2. aws-java-sdk-1.1.1-javadoc.jar
    3. aws-java-sdk-1.1.1-sources.jar

and they contain .class files – java documentation in html format and .java files which are still not compiled. So then I've realized .jar doesn't just include compiled byte codes (.class) and also other things.

  1. When I want to import java libraries, will I always need to import .jar files?
  2. When I want to export my own java libraries, should I need to export in .jar file.

Thanks for help in advance.

Best Answer

Is a .NET Assembly the same thing as a Java .jar?

They play the same role, yes.

.dll contains compiled IL code and .jar contains compiled .class/byte code files?

Yes. Although JARs are just zip files (you can open them in your favorite Zip tool), so they can really contain just about anything.

They say resources, what kind of resources we are talking about here? Images, .txt files, etc. or all of them are possible?

Any file type is allowed in a JAR. Your Java code can access the contents of files within the jar via, for example, getResourceAsStream or getResource. Obviously .class files in a JAR are treated specially (as bytecode).

I've examined AWS (Amazon Web Service API for Java) and I saw three .jar files and they are: aws-java-sdk-1.1.1.jar, aws-java-sdk-1.1.1-javadoc.jar, aws-java-sdk-1.1.1-sources.jar

The above packaging is fairly common. Sometimes people use .jar as an alternative file extension to .zip. I find this practice confusing, but it is not uncommon, and Amazon has done it in this case. I think this practice became more common with the launch of Maven, which stores reference source code in files named .jar.

aws-java-sdk-1.1.1.jar - This is the only file necessary for compilation and execution. It contains the classes (.class files) and resources necessary for the library to function.

aws-java-sdk-1.1.1-sources.jar - This is just a zip file that contains the source code (.java files) for the library. It is not necessary for compilation or execution. It is provided to help you troubleshoot problems you may encounter while using the library. You could unzip this file and read the source code from your hard drive. However, your IDE can probably utilize this file directly by creating a "source attachment" to the main library JAR. With this library->source association set up in your IDE, your IDE will be able to show you the source code of the library when you use your IDE's "Go to Definition" feature on a library class.

aws-java-sdk-1.1.1-javadoc.jar - This is just a zip file that contains the JavaDoc for the library. Again, it is not necessary for compilation or execution. JavaDoc is just HTML, so you could unzip this file and read the JavaDoc HTML directly from your hard drive. However, most IDEs can create a "Javadoc attachment" to the main library, which allows you to pull up context-sensitive JavaDoc for the library from within the IDE. Actually, most IDEs can generate the JavaDoc on the fly from the -sources.jar, so I rarely use Javadoc jars anymore.

When I want to import Java libraries, will I always need to import .jar files?

It's usually called "adding JARs to the classpath", but yes. You pretty much always need to help Java find all the JARs you're using by constructing a classpath whenever you build or execute. In general, using JARs and classpaths is a much more manual and explicit process than using DLLs in .NET. For example Java has no direct equivalent of .NET's "global assembly cache"; also vanilla Java will not automatically load .jars from the working directory.

There are a few other techniques for creating a more "automatic" feeling classpath, but they are kind of advanced: Manifest.MF classpaths are possible, for example -- but this approach is used infrequently as it is brittle. As of Java 6, Java has limited support for using wildcards in the classpath.

When I want to export my own Java libraries, should I need to export in .jar file

This is standard, yes.