• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Extracting a JAR without a JDK installed.

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jesper is correct; any unzipping tool will extract a .jar.

And welcome to the Ranch
 
Joshua Napier
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
bacon. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic