Questions tagged [jar]

JAR file (or Java ARchive) aggregates many files into one. JAR files build on the ZIP file format.

A file allows runtimes to efficiently deploy a set of classes and their associated resources. The elements in a JAR file can be compressed, which, together with the ability to download an entire application in a single request, makes downloading a JAR file much faster than separately downloading the many uncompressed files which would form a single Java Application. External libraries for Java language are very often represented as JAR files too. The package java.util.jar contains classes that read and write JAR files.

JAR file usually contains a manifest file - /META-INF/MANIFEST.MF. Manifest can contain additional meta information about the packaged code, such as version note, package vendor, packaging utility (e.g. ant or maven) used to build the package, implemented specification reference and so on. For executable JAR file manifest is mandatory.

JAR files can also be signed with a certificate to prevent unathorized modification. For Java applets and Java Web Start applications a proper JAR signature is quite important, as modern browsers won't trust unsigned or self-signed Java applications. Oracle jarsigner tool can be used for both JAR signing and signature verification.

Proper MIME type for JAR file is "application/java-archive".

To run your JAR file use: java -jar YourApp.jar

Frequently Asked Questions

People often ask these questions about this topic:

18886 questions
289
votes
31 answers

Viewing contents of a .jar file

What would be the easiest way to view classes, methods, properties, etc. inside a jar file? I'm looking for something equivalent to the very useful Lutz Roeder .NET Reflector - for Java
Dani
  • 4,267
  • 4
  • 29
  • 37
266
votes
12 answers

How to run a JAR file

I created a JAR file like this: jar cf Predit.jar *.* I ran this JAR file by double clicking on it (it didn't work). So I ran it from the DOS prompt like this: java -jar Predit.jar It raised "Failed to load main class" exceptions. So I extracted…
moon
263
votes
22 answers

What version of javac built my jar?

How can I tell what version of the Java compiler was used to build a jar? I have a jar file, and it could have been built in any one of three JDKs. We need to know exactly which one, so we can certify compatibility. Is the compiler version…
Ron Romero
  • 9,211
  • 8
  • 43
  • 64
261
votes
21 answers

What causes java.lang.IncompatibleClassChangeError?

I'm packaging a Java library as a JAR, and it's throwing many java.lang.IncompatibleClassChangeErrors when I try to invoke methods from it. These errors seem to appear at random. What kinds of problems could be causing this error?
Zombies
  • 25,039
  • 43
  • 140
  • 225
250
votes
16 answers

How to add a jar in External Libraries in Android Studio?

I am new to Android Studio. How can I add a few jar files in the External Libraries below the < JDK > folder?
Manoj
  • 3,947
  • 9
  • 46
  • 84
233
votes
10 answers

Controlling Maven final name of jar artifact

I'm trying to define a property in our super pom which will be used by all child projects as the destination of the generated artifact. For this I was thinking about using project/build/finalName yet this does not seem work, even for simple…
Maxim Veksler
  • 29,272
  • 38
  • 131
  • 151
229
votes
6 answers

How to run a class from Jar which is not the Main-Class in its Manifest file

I have a JAR with 4 classes, each one has Main method. I want to be able to run each one of those as per the need. I am trying to run it from command-line on Linux box. E.g. The name of my JAR is MyJar.jar It has directory structure for the main…
Bhushan
  • 18,329
  • 31
  • 104
  • 137
226
votes
10 answers

java.lang.NoClassDefFoundError: Could not initialize class XXX

public class PropHolder { public static Properties prop; static { //code for loading properties from file } } // Referencing the class somewhere else: Properties prop = PropHolder.prop; class PropHolder is a class of my own. The class…
Leon
  • 8,151
  • 11
  • 45
  • 51
214
votes
10 answers

Create aar file in Android Studio

I'd like to create an aar file for my library in Android Studio, i would've gone with a jar option but my library has resources. Any idea how to create an aar file from a library?
AndroidEnthusiast
  • 6,557
  • 10
  • 42
  • 56
209
votes
7 answers

Add JAR files to a Spark job - spark-submit

True... it has been discussed quite a lot. However, there is a lot of ambiguity and some of the answers provided ... including duplicating JAR references in the jars/executor/driver configuration or options. The ambiguous and/or omitted details The…
YoYo
  • 9,157
  • 8
  • 57
  • 74
208
votes
5 answers

Run class in Jar file

If you have a jar file called myJar.jar located in /myfolder and you want to use the class called myClass from it, how do you go about doing it from the command line? I thought it would be to go into the directory and say java -cp myJar.jar.myClass…
jim
  • 2,155
  • 2
  • 14
  • 6
206
votes
22 answers

Access restriction: The type 'Application' is not API (restriction on required library rt.jar)

Here is the code: package mscontroller; import javax.swing.*; import com.apple.eawt.Application; public class Main { public static void main(String[] args) { Application app = new Application(); …
www139
  • 2,111
  • 3
  • 13
  • 8
203
votes
11 answers

Extract source code from .jar file

Is there a way to extract the source code from an executable .jar file (Java ME)?
hiba
  • 2,079
  • 2
  • 13
  • 5
197
votes
40 answers

What causes "Unable to access jarfile" error?

I want to execute my program without using an IDE. I've created a jar file and an exectuable jar file. When I double click the exe jar file, nothing happens, and when I try to use the command in cmd it gives me this: Error: Unable to access…
Joseph Smith
  • 3,243
  • 5
  • 20
  • 18
192
votes
12 answers

Creating runnable JAR with Gradle

Until now I created runnable JAR files via the Eclipse "Export..." functionallity but now I switched to IntelliJ IDEA and Gradle for build automation. Some articles here suggest the "application" plugin, but this does not entirely lead to the result…
Hannes
  • 5,002
  • 8
  • 31
  • 60