Open a file embbeded in a jar file

  • Thread starter khanhly via JavaKB.com
  • Start date
K

khanhly via JavaKB.com

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
 
G

Guest

khanhly said:
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?

Try:

getClass().getResourceAsStream(filename)

Arne
 
A

Andrew Thompson

khanhly said:
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?

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.
 
K

khanhly via JavaKB.com

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


Andrew said:
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.
 
M

Moiristo

khanhly said:
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

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".
 
A

Andrew Thompson

khanhly said:
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 ....
...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.
 
K

khanhly via JavaKB.com

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


Neither does ....



Which is embedded in the rt.jar.

The properties 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?

Yes, Tomcat expands the war file when it starts up
 
A

Andrew Thompson

khanhly said:
Andrew Thompson wrote: ....

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.
 
T

Thomas Fritsch

khanhly via JavaKB.com said:
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 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 '/' !]
 
K

khanhly via JavaKB.com

Andrew said:
...

OK. AFAIR, servlet containers add jars in the
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,433
Messages
2,571,683
Members
48,796
Latest member
Greg L.

Latest Threads

Top