zoukankan      html  css  js  c++  java
  • how to get file from classpath using jboss7.x.1 --reference

    question:

    I want to convert smooks xml-java, so that i need to load source file from mobeeadmin.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-INFsample.xml
        10:10:14,114 ERROR [stderr] (http-localhost-127.0.0.1-8080-2)   File System: E:jboss-as-7.1.1.FinalinWEB-INFsample.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.FinalinWEB-INFsample.xml .I want load from E:jboss-as-7.1.1.Finalstandalonedeploymentsmyproject.warWEB-INFsample.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 could take an absolute one too. So E:/data/sample.xml should just work fine.
    2. /WEB-INF/sample.xml can't be on the classpath, as WEB-INF then would 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 it by ServletContext.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 could 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

  • 相关阅读:
    linux 配置Apache 、PHP
    SQL Server DML(SELECT)常见用法(二)
    SQL Server DML(UPDATE、INSERT、DELETE)常见用法(一)
    PropertyGrid—添加EventTab
    PropertyGrid—添加属性Tab
    PropertyGrid—默认属性,默认事件,属性默认值
    PropertyGrid—为复杂属性提供下拉式编辑框和弹出式编辑框
    PropertyGrid--为复杂属性提供编辑功能
    PropertyGrid--基本功能
    Intellij IDEA使用(一)项目模板类型
  • 原文地址:https://www.cnblogs.com/davidwang456/p/3978564.html
Copyright © 2011-2022 走看看