• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Read from Jar  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How can i read a file from jar? Actually, I want to read a XML file which is stored in jar.

 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at java.net.JarURLConnection.
 
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or just get as resource. But this has nothing to do with ORM, so I'm moving to JiG.
 
Ranch Hand
Posts: 47
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is a working solution for loading a file/XSD from JAR at runtime. Also, to cross check if it worked you can print the content on console.

try {
InputStream is = null;
BufferedReader br = null;
String line;
ArrayList list = new ArrayList();

try {
is = RequestXMLValidator.class.getResourceAsStream("/schemas/wac-querySubscriber.xsd");
br = new BufferedReader(new InputStreamReader(is));
while (null != (line = br.readLine())) {
list.add(line);
}
}
catch (Exception e) {
e.printStackTrace();
}

Iterator it = list.iterator();
System.out.println(" ################Printing Starts ##################### ");
while(it.hasNext()) {
System.out.println(it.next());
}
} catch (Exception e) {

e.printStackTrace();
}

In the above code "RequestXMLValidator" is the name of the class, so just replace it with you class name and "/schemas/wac-querySubscriber.xsd" is the path of the file/XSD in JAR.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!