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
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
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
         return(null);
      }

      if((resourceURL = classInfo.getResource(filename)) == null)
      {
         // error: missing
         return(null);
      }
      return(resourceURL);
   }

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

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

;JOOP!
You can also use getResource method, like:
getClass().getResource("MyXML.xml")
That's the same.

;JOOP!
Avatar of CEHJ
Windows

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

Unix

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

ASKER CERTIFIED SOLUTION
Avatar of pzaprianov
pzaprianov
Flag of United States of America image

Link to home
membership
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

ASKER

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();
       runner.run(args);
     }
   }

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:

getClass().getResource("MyXML.xml")

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