This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Extracting a JAR without a JDK installed." Watch "Extracting a JAR without a JDK installed." New topic
Author

Extracting a JAR without a JDK installed.

Joshua Napier
Greenhorn

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
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

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.


Steve
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

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

And welcome to the Ranch
Joshua Napier
Greenhorn

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
Bartender

Joined: Aug 07, 2007
Posts: 2175
    
  47
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?
 
 
subject: Extracting a JAR without a JDK installed.
 
Similar Threads
Problems adding packages.
Using DOS commands in Java
How to Unjar a JAR file without JDK and without winzip/winrar ??
Creating and building Standalone application
JRE specified for JAR's Application