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-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 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

时间: 2024-11-19 08:15:53

how to get file from classpath using jboss7.x.1 --reference的相关文章

【转】Could not write file XXX\.classpath解决

原文网址:http://www.sjsjw.com/kf_other/article/323_11877_12218.asp 环境 MyEclipse 8.6 + Windows 7 Ultimate English Edition 问题 更改工程的Build Path,出现如下问题:   Could not write file: G:\Java\myJavaPro\EJBEntityBean\.classpath. G:\Java\myJavaPro\EJBEntityBean\.class

Using properties file in java application

Properties files are a cool place to store your configuration details like database connection properties, error messages and other configurations for your program. You can use properties file in your application using following utility class. This c

jboss mq

sudo yum install java-1.8.0-openjdk-devel java-1.8.0-openjdk - sudo vi /etc/profile JAVA_OPTS="$JAVA_OPTS -Djboss.bind.address.private=10.10.10.10"(local ip) export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64export CLA

catalina.bat

startup.bat在最后调用catalina.bat,并且传递了start参数,设置了CATALINA_HOME和CURRENT_DIR俩个临时环境变量.那么catalina.bat都做了什么? 1 @echo off 2 rem Licensed to the Apache Software Foundation (ASF) under one or more 3 rem contributor license agreements. See the NOTICE file distrib

Spring 数据源配置三:多数据源

在上一节中,我们讲述了多数据的情况: 1. 数据源不同(数据库厂商不同, 业务范围不同, 业务数据不同) 2. SQL mapper 文件不同, 3. mybatis + 数据方言不同 即最为简单的多数据, 将多个数据源叠加在一起,不同service--->dao--->sessionFactory; 如果上述的所有条件都相同,仅仅是数据的的多个拷贝情况,想做主备(读写分离),那我们当然可以用2套复制的代码和配置,但是显然不是最佳的实现方式. 这里,我们就讲解一下多数据源的读写分离,怎么实现.

Spring中配置和读取多个Properties文件

public class PropertiesFactoryBeanextends PropertiesLoaderSupportimplements FactoryBean, InitializingBean Allows for making a properties file from a classpath location available as Properties instance in a bean factory. Can be used to populate any be

OSGi原理与最佳实践:第一章 OSGi框架简介(2)

OSGi原理与最佳实践:第一章 OSGi框架简介(2) 由  ValRay 发布 已被浏览4884次 共有3条评论 已被3个人收藏 2013-08-16 21:23 顶(0) 踩(0) osgi原理与最佳实践 1.1.4 开发传统类型的应用 1.1.4.1 B/S 我们首先来看一下,如何基于 OSGi 来开发 B/S 结构的应用.B/S 结构应用程序的开发,可有两个选择:一个是在 OSGi 的框架中嵌入 Http 服务器,另外一个是在 Servlet 容器中嵌入 OSGi 框架.下面分别介绍这两

从零开始写一个Tomcat(贰)--建立动态服务器

上文书说道如何通过http协议建立一个静态的服务器来访问静态网页,但我们选择tomcat最主要的原因还是因为它能动态的执行servlet,这边文章将引导你实现一个能够运行servlet的服务器,这个简易的服务器符合tomcat的基本原理,但真的tomcat远不是这么简单,即使是15年前的tomcat4. 1.主程序逻辑分离 既然要实现动态的服务器,首先我们要实现模式的识别,在tomcat中,tomcat通过读取web.xml,在servlet的配置中通过url的模式来匹配servlet,我们这里

利用URLClassLoader加载两个位置的Class

内容:分别位于\myApp\WEB-INF\classes下的类和\webroot下的类,利用URL数组指定多个仓库位置加载. MyClassLoader: public class MyClassLoader { public static final String WEB_ROOT = System.getProperty("user.dir") + File.separator + "webroot"; public static final String W