Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Read from Jar

 
kandarp mehta
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.
 
Freddy Wong
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.
 
David Newton
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.
 
himanshu.harish agrawal
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic