aspose file tools*
The moose likes Java in General and the fly likes Read File from jar file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic
Author

Read File from jar file

Eddie Howard
Greenhorn

Joined: Jul 27, 2006
Posts: 14
Hi

I am creating some XML in my code....... I gave an xsd that I want to validated my XML against. I have shipped xsd as part of my jar file but I am having trouble accessing xsd from jar....

Can anyone pls advise..

My XSL/XSK code looks like....

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
factory.setAttribute(
"http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
factory.setAttribute(
"http://java.sun.com/xml/jaxp/properties/schemaSource",
new File(new URI("com.package.schemFile.xsd")));
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler(new GenericErrorHandler());
builder.parse(new InputSource(new StringReader(xmlString)));

How ever I am getting an error reading URI\xsd file.... can anyone advise

Rgds
Ed


The men of Ireland were hurling when the gods of Greece were young
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
I think you probably want ClassLoader.getResourceAsStream()


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
If you have put your XSD in package com.package with name schemFile.xsd and assume that java runtime would identify this , then I do not think this would work.Runtime would look for classes in this way not for other resources.

You might try putting the XSD file in the root of the jar and changing the file as below.


[ August 13, 2007: Message edited by: Rahul Bhattacharjee ]

Rahul Bhattacharjee
LinkedIn - Blog
saurabh suman
Greenhorn

Joined: Nov 01, 2006
Posts: 16
Hi Eddie,
I am not sure what exact error you are getting. if you are getting acess denied then you need to give permission of read to the jar file.
If possible can you post your error , that will help me more


Sun Certified Enterprise Architect<br />Saurabh
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
If you are not running the program with security manager enabled , then no problem.You will not get access controller exception.
Have you tried the solution that I suggested you in my previous post?
Eddie Howard
Greenhorn

Joined: Jul 27, 2006
Posts: 14
Hi Guys

Thank for comments. Original details were a bit sketchy so for that I apologies.
My original code didn't work as I could find xsd file.

I tried a few approaches so let me try yo explain

EFFORT 1
I moved it up to root of jar file and used following code:


So basically using class loader to get access to resource, creating URI and passing URI to new file.
I work fine in my IDE when I am developing (gsk2c.xsd is local file) but when I test shipped jar, I get following error.
Exception : com.gsk.gskgaa.exceptions.GSKGAAException: java.lang.IllegalArgument
Exception: URI is not hierarchical


From some research I gather this is widely reported. And of course there is problem because I am using new File(uri), but when deployed my uri NOT a file.... it is embedded in jar file...

Now I as use CLassLoader.getResourceAsINputStream, this give me accurate handle to xsd...... however when setting attribute, I require either a file or file location (as far as I know...), I tried reading actually xsd contents and passin it to setAttribte but I got error again there.... cannot read xsd file etc

EFFORT2
I hoped to explicity read file using about approach, so i took file out of jar & added to classpath, however clasloader still looking for file in jar.... I get following NPE as classloader trying to read file from
URL jar:file:/C:/NewAdapter/lib/gskgaa3.012Beta.jar!/gsk2c.xsd

FINALLY EFFORT3
I have an external properties file. I am left with specifying explicitly where file is & using the file location when setting attribute. This is very crude but all I'm left with....

So to sumarise.....
I can create an inputstream from file in jar..... but I don't where to go then..... as far as I know I need either a File object or a file location....

Any comments
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

You did skip one possibility:

you are able to pass a URI when it is on the local file system, but you missed the fact that gskgaa3.012Beta.jar!/gsk2c.xsd represents the URL for the file within the JAR.
Eddie Howard
Greenhorn

Joined: Jul 27, 2006
Posts: 14
Ok.....
I will try that.....
Thanks!!
Eddie Howard
Greenhorn

Joined: Jul 27, 2006
Posts: 14
Ok I have a URI from which I can create a URL......


However when I use:


I get URI is not hierarchical at this line. However when I use



I get my old favourite..... schema_reference.4: Failed to read schema document 'jar:file:\C:\Projects\GSK\GSKGAAAdapter\lib\gsk2c.jar!\gsk2c.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.

I am assumeing
  • it's not 1) as it knows where doc is
  • its not 3) as .xsd has worked fine for me in past
  • it could be 2) ???
  • Have I covered your approach or am I still missing something?
    David O'Meara
    Rancher

    Joined: Mar 06, 2001
    Posts: 13459

    You cannot get a File reference to a File in a Jar, it just won't work. You can, however get a URL or an InputStream. These are your only real options to try if you want the file in your jar.
    Nirav Naik
    Greenhorn

    Joined: Jul 20, 2009
    Posts: 4
    The idea is that if the class (the code) was able to be loaded from the jar file, then you ought to be able to ask that same classloader that loaded the class to load something else for you. Like so:

    InputStream is = getClass().getClassLoader().getResourceAsStream("com/foo/something.xml");


    I do not seek. I find. - Pablo Picasso
    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.



    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.

    In case, if you parse the XSD and prepare your Document object then use below line of code instead of creating a BufferedReader object and so on.

    Document document = dBuilder.parse(inputStream);
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36478
        
      16
    Welcome to the Ranch

    Why have you posted two apparently identical answers on rather old posts?

    Please use the code button; since you are new I shall edit your post (maybe both) and you san see ho wmuch easier it is to read.
    himanshu.harish agrawal
    Ranch Hand

    Joined: Oct 18, 2010
    Posts: 47

    Thanks Campbell !!!
    himanshu.harish agrawal
    Ranch Hand

    Joined: Oct 18, 2010
    Posts: 47

     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Read File from jar file
     
    Similar Threads
    Throws IllegalArguementException when validating XML with schema
    Help for warning: src-import.0: Failed to read imported schema document
    validating xml
    How to validate XML against DTD
    xml parsing problem using xerces