File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Extracting a JAR without a JDK installed. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Extracting a JAR without a JDK installed." Watch "Extracting a JAR without a JDK installed." New topic

Extracting a JAR without a JDK installed.

Joshua Napier

Joined: Nov 28, 2012
Posts: 2
Hey everyone,

Thank you in advance for any help you can give me, it is very much appreciated!

I am trying to build a little tool to help automate some admin tasks. I would like this tool to be easily distributed to environments that have certain software packages installed. However, part of the task is extracting a jar file, modifying some xml properties included in the jar file, and repackaging the jar file. This is easy enough using a jdk with the jar command, but I would like to not require the user of this utility to install a jdk, as the installed software packages come with a JRE, and I was hoping to use that JRE.

Two questions:
1. Is there a way to extract a Jar and compress a Jar using only a JRE?
2. Is this the right approach? Is there an easier way?
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

It isn't too easy to use, but there is a JarFile class in the java.util.jar package that can be used to read the JAR file (and unpack its contents), and a JarOutputStream (same package) to package it back up.

An alternative is to extract the XML file, then leave the file out (in a path relative to the JAR) after changing its properties. Then your app could look for the XML file in the <path relative to JAR> and use it if present - then fall back to the one stored in the JAR file if the free file is not.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15092

As far as I know a JAR file has the same format as a ZIP file, so if you have a tool that can zip/unzip, it can also unpack JAR files.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46397
Jesper is correct; any unzipping tool will extract a .jar.

And welcome to the Ranch
Joshua Napier

Joined: Nov 28, 2012
Posts: 2
Sounds like there are two options:

1. Use the java.util.jar.JarFile class to load the file into java, do my work, write back to jar file, and exit
The only problem with this is that I will now have to do all my work in Java, which isnt horrible, but would mean I would have to throw away previous work - this may be ok, because I was going to have to rewrite it to run in unix as well anyway.

2. Use winzip, etc. The only disadvantage here is that we then require the user to install or have installed some type of zip utility. The only thing I know for sure that the user will have installed is the software we are editing, which has a JRE.

Thank you for the responses and for the welcome to the ranch - I think I'll like it here
Tony Docherty

Joined: Aug 07, 2007
Posts: 2839
Can you explain why are you allowing/expecting your user to manually open and edit the contents of a jar file? Does this jar file contain Java class files as well?
I agree. Here's the link:
subject: Extracting a JAR without a JDK installed.
It's not a secret anymore!