• 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

Property File inside the jar

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have one java file with main method in it. I have a property file and the java file is using this property file.
I have jarred that class file along with the property file. Can the java file can able to read the property file by giving the url only? or Since the property file is inside the jar, the only option for the java files is to use getAsStream() and read it. PLease advice.
 
Marshal
Posts: 79231
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds more difficult than the usual beginner's question. Moving.
 
Mike Thomson
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please throw some light on it.

Emp.java file in com.test.Emp. Emp java file readng some values from entry.properties file.
For example:
C:\test\com\test\entry.properties
C:\test\com\test\Emp.java
C:\test\com\test\Emp.class

The java file accessing the property directly using by above full url.

Now I am jarring the class file along with the property file. Whether the class file able to pick the propery file, since it's inside the jar?
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All I know is you can use:



Why are you trying to use an URL? What URL?
 
Mike Thomson
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually i want 2 know a java file inside the jar can only able to access the property file inside the same jar thru getAsstream only.or we can able to access it by giving full path of the file inside the jar.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use absolute paths with getResource(AsStream) too (given that C:\test is the root):

It must start with a / to make it absolute.
 
Mike Thomson
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I think I have confused my question it seems. Please forgive me If I have confused. I know using getResourceAsStream, we can able to read the property file.
My Question is without getResourceAsStream, whether we can able to read the property file from the java file, where both java file and the property file are jarred in the same jar file.
For Example:

emp.jar\com\test\Emp.java
emp.jar\com\test\Emp.class
emp.jar\com\test\entry.properties

In Emp.java (can I able to read the property file without getResourceAsStream? Is there any other way..
----------------
Properties properties = new Properties();
properties.load(new FileInputStream("com\test\entry.properties"));

Whether the above code will work, I have tested it's not working. But the getResourceAsStream is working. But i want to know it's possible to read it without getResourceAsStream and given the path name as above in the coding.


 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only other way (using java.util.zip and java.util.jar) requires you to know where the JAR file is located; that's a luxury you rarely have. In fact, the only way I know to get the JAR file location uses getResource(), so you would be using getResource() to prevent using getResource()*.

*getResourceAsStream() uses getResource() in the background.
 
Greenhorn
Posts: 3
Java ME Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am creating the automation code application.

I have one xyz.jar file in my project jar file "myproject.jar" at com.my.resource location.
My application creates one java file at location say "c:\". And now I want to copy the xyz.jar file at the same location c:\

The problem is How can i copy the "xyz.jar" file at "c:\" location? That is how can i access this "xyz.jar" within the "myproject.jar"

I tried but no luck.

Please help me.

Thanks in advance

Sahihaijava
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic