Extracting files from jar windows

www.makeuseof.com

Follow MUO

How to Open JAR Files in Windows 10

Have you ever come across the JAR file type? Here’s what a JAR file is and how to open JAR files in Windows 10.

A JAR file is a Java Archive file. It’s used to package Java files and metadata together into a single file. If you want to know how to open a JAR file in Windows 10, you’ve come to the right place. We’re going to explain what JAR files contain and how to open them.

You might also encounter errors when trying to open a JAR file. We’ve got you covered with solutions for that too.

What Is a JAR File?

JAR stands for Java ARchive. As the name suggests, it’s an archive file. This means that it’s a single file containing other files packaged together for reasons such as portability and decreased storage space.

If you’re familiar with ZIP files, then a JAR file is essentially the same thing. The difference is that JAR files are applications designed to be used by the Java Runtime Environment.

The unique element of JAR files is that they contain a manifest. This is a special meta-file that, simply put, tells the JAR how to behave and holds information about the files within.

A JAR file can also contain CLASS files (compiled Java code), audio files, image files, and more. The JAR file can then be read and run as a single request by the Runtime Environment.

JAR can be used for all sorts of purposes on desktop and mobile. For example, it could be a game, application theme, or browser add-on.

How to Open JAR Files

If you want to execute a JAR file—that is, run the actual application contained within—then you need to install the Java Runtime Environment. You may already have it on your computer but grab it again from the official site to ensure you’re running the latest version.

You should then download a lightweight utility called Jarx. This makes JAR files behave like ordinary programs on your computer. As such, once Jarx is installed, you can then open a JAR file like any other.

You can also run it from Command Prompt. Press Windows key + X and click Command Prompt (Admin). Then input the following command, but replace sample.jar with the full file path of the JAR:

How to Extract a JAR

Alternatively, you can unpackage the JAR file. You can do this to see what files are contained within it or if the JAR file isn’t designed to be an executable file.

To unpackage a JAR, you need a program that can extract compressed files. Windows includes functionality for this, but you can also use file extraction software like 7-Zip or WinRAR to get the job done.

Open the JAR file within the software, and you can browse all the folders and files within it. You can also extract the JAR if you want to use or inspect the individual files further.

Читайте также:  Виртуальный контейнер для windows

Alternatively, you can use an online extractor if you don’t want to download an extracting program. A good choice is Archive Extractor because it’s easy to use and, most importantly, supports JAR files (not all online extractors do).

Simply upload your JAR, and it will show you the contents of the file within a tree structure. Click a folder to open or expand it and a file to download it individually.

Finally, if your computer is having problems recognizing an executable JAR file, you can right-click the file, click Rename and then append the .ZIP file extension. This works because the file types are so closely linked.

How to Make JAR Files

Are you sure you need to make a JAR file? Remember, they serve a specific purpose: bundling files together to be used as a Java application.

If that’s what you need to do, head over to Oracle’s packaging programs in JAR files documentation. This will give you all the information you need on how to create, package, and sign a JAR file.

How to Fix Problems Opening JAR Files

You might get an error when trying to open a JAR file, often due to incompatibility or security settings.

Update the Java Runtime Environment

You should always run the latest version of the Java Runtime Environment.

To see what version of Java you’re running, open the Start menu, type configure java, and select the result. The Java Control Panel will open.

On the General tab, click About and check that the version you’re running aligns with the latest one available directly from Oracle’s website. If not, download it and update it.

JAR Opens in the Wrong Program

Windows might not know what to do with the JAR file and could try to open it in a program that won’t support the format.

To resolve this, right-click the file, and hover Open with. There are several default programs to choose from, and one of these might be the right one. If not, click Choose another app.

You can change Windows 10’s file associations and default programs if you want JAR files to always open in the same program.

If you want to run the file within the Java Runtime Environment directly:

  1. Navigate to Program Files\Java.
  2. Open the only folder (named jre1.8.0_281 or similar).
  3. Open the \bin folder.
  4. Open java.exe.

JAR Gives Security Error When Opening

Java has been seen as a security risk, so don’t be surprised if you get a warning when opening a JAR file.

To resolve this, open the Start menu, type configure java, and select the result. The Java Control Panel will open. Then:

  1. Click the Security tab.
  2. Ensure you have Enable Java content for browser and Web Start applications checked.
  3. Set the security level to High.
  4. Add to the exception site list if necessary.
  5. Click OK.

Java Isn’t Just for Windows

Hopefully, you now understand what a JAR file is, how to open it, and how to resolve any problems you might encounter.

While this guide has focussed on Windows, you can also get Java on other operating systems. That includes Ubuntu, where Java is useful whether you’re coding in the language or using an app.

Need to install Java on your Ubuntu system? Whether you’re coding in Java or using an app, here’s how to get it running.

Joe was born with a keyboard in his hands and immediately started writing about technology. He has a BA (Hons) in Business and is now a full-time freelance writer who enjoys making tech simple for everyone.

Читайте также:  С windows system32 wow64 dll что это

Subscribe To Our Newsletter

Join our newsletter for tech tips, reviews, free ebooks, and exclusive deals!

One More Step…!

Please confirm your email address in the email we just sent you.

Extracting files from a Jar more efficently

I’m extending a utility class that bundles a set of images and .xml description files. Currently I keep all the files in a directory and load them from there. The directory looks like this:

Here’s my current constructor. It is lightning fast and does what I need it to do. (I’ve stripped out some of the error checking to make it easier to read):

For the sake of webstart and cleanliness, I’ve been trying to load these resources from a Jar instead. I’ve got it working, but the load time went from instantaneous to a few seconds, and that’s not acceptable. Here’s the code I tried (again, error checking stripped):

(This isn’t the best way to do what I want to do, it’s a mock-up to see if the idea worked. It didn’t. The two for-loops is in no way the source of the problem; it’s the process of creating all those InputStreams that slows it down)

Anyone know of a better way to work with .jar files besides the way I’ve tried here? Here’s the AngelCodeFont API. If it was absolutely necessary I could submit a patch for that, but I’d rather not have to. It seems to me that there’s probably a way to do what I want to do, I’m just not familiar with it.

I’m not terribly against quickly dumping the jar to a temporary directory and then reading the files from there, but if there’s a way to do it reading directly from the jar quickly, I’d much rather do that.

Also: Compression isn’t an issue at all. The only reason I’m using a jar is for the packing issue.

How to write a Java program which can extract a JAR file and store its data in specified directory (location)?

I have created a JAR file. Now, I created another Java program. I want to unpack that JAR file in some other directory, meaning I want to do something like unzip.

If I run jar -xf filename.jar this causes some error:

10 Answers 10

Or try this code:

Extract the Contents of ZIP/JAR Files Programmatically

Suppose jarFile is the jar/zip file to be extracted. destDir is the path where it will be extracted:

You can use this code snippet as a reference to get your task done.Its almost the same as the code snippet shown above by @JuanZe except that for those who were getting the FileNotFoundException, i have added a small code snippet that will check if the file does exist and if it doesn’t then it will create the parent folder along with the files and will extract the contents of jar file inside the specified destination folder.

Here is what I would do to extract my whole «resources» folder from my jar. It is way more faster to use BufferedReader and BufferedWriter.

Old question.
Here is a updated answer using :

Reading a resource file from within jar

I would like to read a resource from within my jar like so:

and it works fine when running it in Eclipse, but if I export it to a jar, and then run it, there is an IllegalArgumentException:

and I really don’t know why but with some testing I found if I change

then it works the opposite (it works in jar but not eclipse).

I’m using Eclipse and the folder with my file is in a class folder.

Читайте также:  Linux восстановить при загрузке

15 Answers 15

Rather than trying to address the resource as a File just ask the ClassLoader to return an InputStream for the resource instead via getResourceAsStream:

As long as the file.txt resource is available on the classpath then this approach will work the same way regardless of whether the file.txt resource is in a classes/ directory or inside a jar .

The URI is not hierarchical occurs because the URI for a resource within a jar file is going to look something like this: file:/example.jar!/file.txt . You cannot read the entries within a jar (a zip file) like it was a plain old File.

This is explained well by the answers to:

To access a file in a jar you have two options:

Place the file in directory structure matching your package name (after extracting .jar file, it should be in the same directory as .class file), then access it using getClass().getResourceAsStream(«file.txt»)

Place the file at the root (after extracting .jar file, it should be in the root), then access it using Thread.currentThread().getContextClassLoader().getResourceAsStream(«file.txt»)

The first option may not work when jar is used as a plugin.

I had this problem before and I made fallback way for loading. Basically first way work within .jar file and second way works within eclipse or other IDE.

Up until now (December 2017), this is the only solution I found which works both inside and outside the IDE.

Use PathMatchingResourcePatternResolver

Note: it works also in spring-boot

In this example I’m reading some files located in src/main/resources/my_folder:

The problem is that certain third party libraries require file pathnames rather than input streams. Most of the answers don’t address this issue.

In this case, one workaround is to copy the resource contents into a temporary file. The following example uses jUnit’s TemporaryFolder .

In my case I finally made it with

Make sure that you work with the correct separator. I replaced all / in a relative path with a File.separator . This worked fine in the IDE, however did not work in the build JAR.

I have found a fix

Replace «Main» with the java class you coded it in. replace «path» with the path within the jar file.

for example, if you put State1.txt in the package com.issac.state, then type the path as «/com/issac/state/State1» if you run Linux or Mac. If you run Windows then type the path as «\com\issac\state\State1». Don’t add the .txt extension to the file unless the File not found exception occurs.

This code works both in Eclipse and in Exported Runnable JAR

I Tried all the mentioned things here , so let me rephrase my problem ,

This wsdl file is in the java project I will be using a jar , so when I import this jar to a different project , I am getting file not found exception since the project is trying to look into its resources folder to start the spring boot app .

what changes I need to make to make this project start with this jar as dependency .

You can use class loader which will read from classpath as ROOT path (without «/» in the beginning)

For some reason classLoader.getResource() always returned null when I deployed the web application to WildFly 14. getting classLoader from getClass().getClassLoader() or Thread.currentThread().getContextClassLoader() returns null.

getClass().getClassLoader() API doc says,

«Returns the class loader for the class. Some implementations may use null to represent the bootstrap class loader. This method will return null in such implementations if this class was loaded by the bootstrap class loader.»

may be if you are using WildFly and yours web application try this

Оцените статью