Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Resources in JAR

 
Ikram Soomro
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can we place images and other IO Files (Resources other than .class files) in jar that are used in application ? if yes than how?
 
Sonny Gill
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, Yes.
Just as you would place class files in a jar; by using the jar tool. jar command doesnt differentiate between class files or any other files, all files in the target directory are included in the generated jar.

cheers
Sonny
 
Jim Hicks
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A classloader loads the jars so, the files in a jar are accessed from the classloader. Use the method classloader.getResourceAsStream(String), or URL classloader.getResource(String).

To make sure you are using the same classloader as the one that loaded the jar, you should get the classloader from a object who's class is in the jar.

Classloader classloader = someObject.getClass().getClassloader();

If you want to access the jar with jar apis, you can get a url then use jar api's.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic