0

I have a java file which uses XSL file to perform transformation. Code used is:

FileInputStream inXLSFile = new FileInputStream("ListApi.xsl");
TransformerFactory factory = TransformerFactory.newInstance(); StreamSource xslStream = new StreamSource(inXLSFile); After this a jar file is generated. from the jar file I am not able to use the XSL. It says 'File not found'. Please give pointers.

1 Answers1

4

change

FileInputStream inXLSFile = new FileInputStream("ListApi.xsl");

to

InputStream inXLSFile = this.getClass().getResourceAsStream("ListApi.xsl");

for more information read about how do I load a file from a jar?

Community
  • 1
  • 1
Markus
  • 1,649
  • 1
  • 22
  • 41
  • this.getClass().getResourceAsStream("ListApi.xsl") returns a InputStream and it cannot be used in creating a new FileInputStream object. Kindly help me to out with some other option. – user3440451 Mar 28 '14 at 11:04
  • Actually i need to do some transformation using this XSL. If its InputStream then my tranfromFactory is throwing a error. Its expecting FileInputStream.Code used is: TransformerFactory factory = TransformerFactory.newInstance(); StreamSource xslStream = new StreamSource(inXLSFile); Transformer transformer; transformer = factory.newTransformer(xslStream); – user3440451 Mar 28 '14 at 11:39
  • Which StreamSource do use use? Look at the documentation. StreamSource has a constructor that takes a InputStream: http://docs.oracle.com/javase/7/docs/api/javax/xml/transform/stream/StreamSource.html#StreamSource%28java.io.InputStream%29 – Markus Mar 28 '14 at 11:44
  • Yes. It works fine. the path where XSL resides was wrong. Corrected the same. Thank you Markus! Have a nice day. – user3440451 Mar 28 '14 at 11:51
  • 1
    In that case (XSL files in the JAR), how can we import others XSL files from the first one (all XSL files are in the same jars) Like – sritmak May 05 '14 at 14:02