Author
Read from Jar
kandarp mehta
Greenhorn
Joined: May 04, 2010
Posts: 6
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
Joined: Sep 11, 2006
Posts: 959
Take a look at java.net.JarURLConnection .
SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
David Newton
Author
Rancher
Joined: Sep 29, 2008
Posts: 12617
posted Jul 30, 2010 05:20:46
0
Or just get as resource. But this has nothing to do with ORM, so I'm moving to JiG.
himanshu.harish agrawal
Ranch Hand
Joined: Oct 18, 2010
Posts: 47
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.
subject: Read from Jar