19

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.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • 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. – Edwin Dalorzo Jan 10 '13 at 18:06
  • probably duplicate http://stackoverflow.com/q/4411028/1697099 – Premraj Jul 16 '15 at 07:38

5 Answers5

21

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.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Deepak Singhal
  • 10,568
  • 11
  • 59
  • 98
  • 3
    and a library can also contain the source code for debugging and javadocs for in IDE documentation tooltips. – Eelke Jan 10 '13 at 18:07
  • 2
    a library can contain a single jar, too. that is not a good explanation. – Juvanis Jan 10 '13 at 18:08
  • 1
    so `jar` file **cannot** contain `jar` files but `library` does..is it the difference..sorry if it sounds funny but am new to java –  Jan 10 '13 at 18:12
  • @DelShekasteh what can be the difference then..am getting damn confused –  Jan 10 '13 at 18:13
  • 1
    Theoretically yes, a library can contain a single jar too. But that is not the idea behind a library. – Deepak Singhal Jan 10 '13 at 18:18
  • @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. – Deepak Singhal Jan 10 '13 at 18:19
  • @Deepak so classes inside that `embedded jar` will be available if we use library right! –  Jan 10 '13 at 18:53
  • If we use library and put 10 jar files into it; yes all those jars will be available. – Deepak Singhal Jan 10 '13 at 18:54
  • also does lib only contains jar files –  Jan 10 '13 at 19:13
  • 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. – Deepak Singhal Jan 10 '13 at 19:24
5

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.

Paul Sanwald
  • 10,899
  • 6
  • 44
  • 59
1

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.

Joseph Farrugia
  • 327
  • 1
  • 3
  • 14
1

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.

  • but those resources can also be in `jar` file..right..sorry if it sounds funny..new to java world –  Jan 10 '13 at 18:15
  • 1
    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. – John Ballard Jan 17 '13 at 16:52
1

This article explains it all..

It states

Java's libraries are commonly known as class libraries. However, Java refers to class libraries as packages.