Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

Open a file embbeded in a jar file

Thread view: 
khanhly - 17 Aug 2006 01:46 GMT
I need to get data out from a text/properties file which embedded inside a
jar file.  Here is the sample code
        String filename = "com.config.myfile.resources.loggingKeyList.
properties"
        File fn = new File(filename);
        System.out.println (fn.exists());
When run, it prints "false".  It can not locate the file.

Is there the way that I can open a file embedded inside a jar file?
Thanks
Arne Vajhøj - 17 Aug 2006 02:09 GMT
> I need to get data out from a text/properties file which embedded inside a
> jar file.  Here is the sample code
[quoted text clipped - 5 lines]
>
> Is there the way that I can open a file embedded inside a jar file?

Try:

getClass().getResourceAsStream(filename)

Arne
Andrew Thompson - 17 Aug 2006 04:48 GMT
> I need to get data out from a text/properties file which embedded inside a
> jar file.  Here is the sample code
[quoted text clipped - 5 lines]
>
> Is there the way that I can open a file embedded inside a jar file?

Sure.  Here is an example of finding the 'Object'
class by it's name - note
a) the '/'s
b) the firstmost '/' of the successful string.

<sscce>
class GetResourceTest {
 public static void main(String args[]) {
   Object o = new Object();
   String location = "java/lang/Object.class";
   java.net.URL url = o.getClass().getResource(
     location);
   System.out.println( url );
   url = o.getClass().getResource(
     "/" + location);
   System.out.println( url );
 }
}</sscce>

HTH

Andrew T.
khanhly - 18 Aug 2006 23:13 GMT
Thank you for your help.
I have tried your suggestion, but it still does not work since the properties
file does not locate loosely in the system.  It is embbeded inside a jar file.

Here my situation:
 I have a war file which contains several jar files.  The properties file is
embbeded inside one of the jar file.
 I tried to load that properties file and I have tried this way

     String name = "com.myjar.resources.log.properties";
     InputStream st = ClassLoader.getSystemClassLoader().getResourceAsStream
(name);

 It can not find the properties file

>> I need to get data out from a text/properties file which embedded inside a
>> jar file.  Here is the sample code
[quoted text clipped - 24 lines]
>
>Andrew T.
Moiristo - 19 Aug 2006 00:02 GMT
> Thank you for your help.
> I have tried your suggestion, but it still does not work since the properties
[quoted text clipped - 10 lines]
>
>   It can not find the properties file

The location you are looking for is incorrect. You must not specify the
extension. In your case, it looks for
'com/myjar/resources/log/properties.properties. So, maybe it works when
you change 'name' to "com.myjar.resources.log".
Andrew Thompson - 19 Aug 2006 01:52 GMT
> Thank you for your help.

Please demonstrate your appreciation by not top-posting
in future.

> I have tried your suggestion, but it still does not work since the properties
> file does not locate loosely in the system.

Neither does ....

> >    String location = "java/lang/Object.class";

>...It is embbeded inside a jar file.

Which is embedded in the rt.jar.

> Here my situation:
>   I have a war file which contains several jar files.  The properties file is
> embbeded inside one of the jar file.

A WAR file is a compressed file.  If it remains
unexpanded, the class.getResource() will only
find the other 'jar file the properties file is embedded in',
but not the *properties* *file* itself.

Is your WAR file expanded?

Andrew T.
khanhly - 19 Aug 2006 16:25 GMT
>> Thank you for your help.
>
[quoted text clipped - 11 lines]
>
>Which is embedded in the rt.jar.

The properties file

>> Here my situation:
>>   I have a war file which contains several jar files.  The properties file is
[quoted text clipped - 6 lines]
>
>Is your WAR file expanded?

Yes, Tomcat expands the war file when it starts up

>Andrew T.
Andrew Thompson - 19 Aug 2006 17:08 GMT
...
> >> Here my situation:
> >>   I have a war file which contains several jar files.  The properties file is
> >> embbeded inside one of the jar file.
..
> >Is your WAR file expanded?
>
> Yes, Tomcat expands the war file when it starts up

OK.  AFAIR, servlet containers add jars in the
WEB-INF/lib/ directory to the classpath.
Is your jar inside that directory?

Andrew T.
khanhly - 20 Aug 2006 17:40 GMT
>...
>> >> Here my situation:
[quoted text clipped - 8 lines]
>WEB-INF/lib/ directory to the classpath.
>Is your jar inside that directory?
Yes, after expanded, all the jar files are in the WEB-INF/lib/ dir

When I runs in the Eclipse IDE, it find the properties file OK, but not when
it runs in jar file under Tomcat env.
I have tried several different ways declaring the file path
       /com/myjar/log/config.properties
      com/myjar/log/config.properties
      com/myjar/log/config
      com.myjar.log.config
None of those works

>Andrew T.
Thomas Fritsch - 19 Aug 2006 22:16 GMT
> Here my situation:
>  I have a war file which contains several jar files.  The properties file
[quoted text clipped - 8 lines]
>
>  It can not find the properties file

I have no experience with .war files. But I think you'll more have success
with:
  String name = "/com/myjar/resources/log.properties";
  InputStream st = ...;
[Note the leading '/' !]

Signature

Thomas



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.