File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Read from Jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Read from Jar" Watch "Read from Jar" New topic
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

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Read from Jar