Win a copy of Clojure in Action this week in the Clojure forum!
    Bookmark Topic Watch Topic
  • New Topic

Read File from jar file

 
Eddie Howard
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
 
Peter Chase
Ranch Hand
Posts: 1970
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
I think you probably want ClassLoader.getResourceAsStream()
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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 ]
 
saurabh suman
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 14
  • 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
Ok.....
I will try that.....
Thanks!!
 
Eddie Howard
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
    Posts: 13459
    Android Eclipse IDE Ubuntu
    • 0
    • Mark post as helpful
    • send pies
    • Report post to moderator
    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
    Posts: 4
    • 0
    • Mark post as helpful
    • send pies
    • Report post to moderator
    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");
     
    himanshu.harish agrawal
    Ranch Hand
    Posts: 47
    Java Linux Oracle
    • 0
    • Mark post as helpful
    • send pies
    • 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.



    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
    Pie
    Posts: 47293
    52
    • 1
    • Mark post as helpful
    • send pies
    • Report post to moderator
    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
    Posts: 47
    Java Linux Oracle
    • 0
    • Mark post as helpful
    • send pies
    • Report post to moderator
    Thanks Campbell !!!
     
    himanshu.harish agrawal
    Ranch Hand
    Posts: 47
    Java Linux Oracle
    • 0
    • Mark post as helpful
    • send pies
    • Report post to moderator
     
    I agree. Here's the link: http://aspose.com/file-tools
      Bookmark Topic Watch Topic
    • New Topic