Difference between a jar file and a library in Java

15,054

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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.

Share:
15,054

Related videos on Youtube

Bo Persson
Author by

Bo Persson

I have been a software developer for almost 40 years, using a variety of programming languages like COBOL, Pascal, Ada, C, assembly, and some scripting languages. Now very interested in following the development of C++17 and beyond.

Updated on September 19, 2022

Comments

  • Bo Persson
    Bo Persson over 1 year

    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.

    • Edwin Dalorzo
      Edwin Dalorzo over 11 years
      I am not a Netbeans user but if it gives you the option of loading a JAR or a library then the library could refer to a native library (i.e. a dll). Java can also execute native code through JNI, and some Java applications require such libraries to be available in your project to work properly.
    • Premraj
      Premraj almost 9 years
  • Eelke
    Eelke over 11 years
    and a library can also contain the source code for debugging and javadocs for in IDE documentation tooltips.
  • Admin
    Admin over 11 years
    jar also groups classes..what is the difference then
  • Juvanis
    Juvanis over 11 years
    a library can contain a single jar, too. that is not a good explanation.
  • Admin
    Admin over 11 years
    so jar file cannot contain jar files but library does..is it the difference..sorry if it sounds funny but am new to java
  • Admin
    Admin over 11 years
    @DelShekasteh what can be the difference then..am getting damn confused
  • Admin
    Admin over 11 years
    but those resources can also be in jar file..right..sorry if it sounds funny..new to java world
  • Deepak Singhal
    Deepak Singhal over 11 years
    Theoretically yes, a library can contain a single jar too. But that is not the idea behind a library.
  • Deepak Singhal
    Deepak Singhal over 11 years
    @cSharper Yes theoretically a jar file can contain another jar too because jar is also like a zip file. But putting a jar inside a jar will not help in anyway; because classes inside that embedded jar will not be available.
  • Admin
    Admin over 11 years
    @Deepak so classes inside that embedded jar will be available if we use library right!
  • Deepak Singhal
    Deepak Singhal over 11 years
    If we use library and put 10 jar files into it; yes all those jars will be available.
  • Admin
    Admin over 11 years
    also does lib only contains jar files
  • Deepak Singhal
    Deepak Singhal over 11 years
    Library doesnt have .jar extension. Library is not a concept of java as such. It is used by different IDEs or many other places; but core java doesnt have library concept.
  • John Ballard
    John Ballard over 11 years
    Yes you are correct. I was attempting to simplify the problem. In reality a jar is nothing more than a zip file with a different extension. The jar can contain whatever you put in there. There is a specific format that java likes things to be in. The library is nothing more than an "abstraction" that Netbeans (and other IDEs) have created to indicate a collection of jars (or more generically resources). Usually this is store in configuration with your project, e.g. an xml file. The notion of library is not specific to java or the sdk, rather it is specific to the IDE.