How to Get file from classpath using jboss7.x. 1 -- Reference

Source: Internet
Author: User
Tags jboss

Question:

I want to convert smooksXML-Java, So that I need to load source file frommobeeadmin.war/WEB-INF/sample.xml.

Smooks smooks = new Smooks("/WEB-INF/sample.xml");

It is throwing following exception:

java.io.IOException: Failed to access data stream for resource [/WEB-INF/sample.xml]. Tried (in order):    10:10:14,113 ERROR [stderr] (http-localhost-127.0.0.1-8080-2)   File System: E:\WEB-INF\sample.xml    10:10:14,114 ERROR [stderr] (http-localhost-127.0.0.1-8080-2)   File System: E:\jboss-as-7.1.1.Final\bin\WEB-INF\sample.xml    10:10:14,117 ERROR [stderr] (http-localhost-127.0.0.1-8080-2)   Classpath: /WEB-INF/sample.xml    10:10:14,125 ERROR [stderr] (http-localhost-127.0.0.1-8080-2)By default it looks in File System: E:\jboss-as-7.1.1.Final\bin\WEB-INF\sample.xml .I want load from E:\jboss-as-7.1.1.Final\standalone\deployments\myproject.war\WEB-INF\sample.xml.

 

Answer:

  1. What smooks takes as a string is a file name. if you take a relative one it's interpreted relative to the start location of your Java application. but of course you cocould take an absolute one too. soE:/data/sample.xmlShoshould just work fine.
  2. /WEB-INF/sample. XML can't be on the classpath, as WEB-INF then wocould be a package name. but those must not contain dashes. in fact it's a resource file of your web application and you can get a stream to load itServletContext.getResourceAsStream(java.lang.String path). As jboss7 is servlet 3.0 compilant you can get the servletcontext from the httpservletrequest. However some modern frameworks give you neither.
  3. If you 'd like to get your file from the class path you cocould move it to WEB-INF/classes and load it via a classloader. however, Java is pretty picky with the right one. most reliable is the contextclassloader (it's now in the root package ):

Thread.currentThread().getContextClassLoader().getResourceAsStream("sample.xml");

 

Reference from: http://stackoverflow.com/questions/15633371/how-to-get-file-from-classpath-using-jboss7-x-1

 

How to Get file from classpath using jboss7.x. 1 -- Reference

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.