Link to home
Start Free TrialLog in
Avatar of pzaprianov
pzaprianovFlag for United States of America

asked on

Look for a resource inside a jar file?

Hi all,

I have two components, a jar file and a shell script that executes that jar file, but i need to look for a properties xml file that is inside the jar file, any idea of how can I do that?

Thanks guys
Avatar of gnoon
Flag of Thailand image

You can open jar file using a ZIP utility, such as WinZip, Winrar.
JDK also comes with such utility, jar.exe in the bin folder.
>> but i need to look for a properties xml file that is inside the jar file,

Where do you want to look for that.. inside the shell script or inside the java code..

For inside the shell script, you can use the following command to extract the xml file.. before executing the script..

jar -xvf Jarfilename.jar pathToXmlFileInsideTheJar

Avatar of sciuriware

Do you mean that the program would lookup the resource in its own .jar during execution?

    * Primitive: surely get the path to a resource file via Class info.
    * @param  classInfo   typically YourClass.class
    * @param  filename    the relative pathname from the .class file inside the .jar to the resource file.
    * @return             an URL to the resulting resource.
   URL findResource(Class classInfo, String filename)
   URL resourceURL;

      if(classInfo == null)
         // error

      if((resourceURL = classInfo.getResource(filename)) == null)
         // error: missing

Sample usage to retrieve a picture in the same folder as the class Xyz:

ImageIcon picture = new ImageIcon(_findResource(Xyz.class, pictureFileName));

You can also use getResource method, like:
That's the same.

Avatar of CEHJ

jar tf yourjar.jar | find "properties.xml"


jar tf yourjar.jar | grep "properties.xml"
Oh, yes .... It is, sorry .... Administrators, please delete my comments ...
jar file uses the same format as zip so you can open it with any zip utility, eg. winzip

Avatar of pzaprianov
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
the one that is very close to that what i am looking for is:

jar -xvf Jarfilename.jar pathToXmlFileInsideTheJar

That is the command to *extract* an entry from a jar. How can that be like 'looking for' one?
What exactly are you trying to do?
Avatar of pzaprianov


I want to do that:

java -jar myjar.jar myXmlFileAsParameter.xml

The problem is that myXmlFileAsParameter.xml is inside the myjar.jar
You can only do that if the main class can deal with what's inside the jar. How does it treat the parameter? (Please post code)
main class:
   public class MainClass extends RunnerClass {
     public static void main(String[] args) {
       MainClass runner = new MainClass();;

Open in new window

Well the code i need to see is the run method
exactly, that is the code maybe that i need, any suggestion?

because when I look for the resource it is not there because I have only the jar file and not the folder with all the xml properties files
I think there's some confusion: i'm asking you to post the source code of your run method
I am using this:


it works if i have the xml outside of the jar, but i need everithing inside, and that is my question how to reach this xml file inside de jar?
It will work if you address it correctly. Please post the result of the following command:

jar tf yourjar.jar