I found several similar topics, like reading from inner structure etc, however it still does not solve my problem.
Project structure:
whole project -> war, which has:
several jar's connected as dependency
in pom.xml;
context.xml in META-INF folder, which I need to read from one of jars.
part of it -> jar, which is dependency in war's pom.xml
I've tried a few solutions like:
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("META-INF/context.xml");
However I did not expect that to work as I think my jar searches for this resource in its structure, not in war.
I need jar project to read context.xml from war project. So, jar is like inner structure and war is outer. Is that possible to do?
With getResourceAsStream() you have access to all resources in the classpath.
The classpath of a webapplication consists of every jar under WEB-INF/lib and every file under WEB-INF/classes
META-INF is not in the classpath.
Please read the question: How to get resource from the context.xml file in tomcat webapp?
Related
This might be a duplicate question, but after trying almost all the possible solutions, I am still unable to read the correct manifest file in my war.
here is how my war file looks like;
META-INF
maven/com.abc.pack/console
MANIFEST.MF
context.xml
WEB-INF
classes com.abc -configurations
-MainController
lib -(multiple jars)
I want to access /META-INF/MANIFEST.MF at the root from the MainController class .
Used
getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF")
and
this.getClass().getPackage().getImplementationVersion()
Both return null.
Tried several other solutions provided but it then read manifest file of the one of jars inside the lib folder but not the root.
Some solution also had the jcabi plugin to use read manifest but i want to try without a plugin support.
Can anyone help me find what I am doing wrong here?
I need to read a properties file located inside an ear. In jboss 5.1 I put the file in the root of the ear and it works, but in wildfly it doesn´t.
In wildfly, I managed to read the property file outside the ear according to this
http://blog.jyore.com/?p=58
But I haven´t found the way to do the same but with the file inside the ear. I´ve tried without success putting the file in the METAINF folder, also tried with the "Class-Path: ." in the manifest...
Please any ideas?? Do I have to put the file in any specific location??
I found the solution:
In WildFly 8, to get those properties available in the classpath, package them within your application. For example, if you are deploying a .war then package those properties in WAR WEB-INF/classes/ folder. If you want those properties accessible to all components in a .ear, then package them at the root of some .jar and place that jar in EAR lib/ folder.
https://docs.jboss.org/author/display/WFLY8/How+do+I+migrate+my+application+from+AS5+or+AS6+to+WildFly
Thanks for your help
I'm porting a app that ran in tomcat to Jboss where it will be deployed as a .WAR file. I have a line of code that looks for a configuration .xml file :
appContext = new ClassPathXmlApplicationContext("cmdAppContext.xml");
Now I tried putting it in WEB-INF/classes (After comparing with eclipse generated .WAR file), and also tried the WEB-INF folder as it was referred in that location in the web.xml. However each time I get a NullPointerException. Am I missing anything here?
Please note: I've already seen the similar question in forums which was not resolved
It's not a good idea to look into context.xml from source code. You should use a .properties file to configure your app. .WAR is like a .ZIP file with all sites files inside, so META-INF are inside at root folder from .WAR.
I have a dynamic web project in eclipse. I have a file a.xml in my WEB-INF which is present on classpath. I have class named Test in my project.
I have following code in a JSP scriptlet -
<%#page import="com.kshitiz.Test"%>
<%
System.out.println(ClassLoader.getSystemClassLoader().getSystemResource("a.xml"));
System.out.println(this.getClass().getClassLoader().getSystemResource("a.xml"));
System.out.println(Test.class.getClassLoader().getResource("a.xml"));
%>
The output is -
null
null
C:/Users/kshitiz/eclipse/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/test-project/WEB-INF/classes/a.xml
Apparently the system class loader and the JSP classloader are not able to find my file.
Could you explain the output? Is tomcat using a different classloader to load JSP? Why so? Why isn't system classloader able to find my file?
I already know that I can use following to get to my file -
getServletContext().getResourceAsStream("/WEB-INF/a.xml");
I'm interested in understanding the above scenario not the various methods to load that file.
Each deployed web application has its own class loader. That's what makes it possible to deploy two different webapps on the same container and to undeploy them, even if they use conflicting versions of the same library.
The webapp's classloader is a child of the tomcat classloader, and its classpath contains the webapp's WEB-INF/classes directory, as well as every jar file in WEB-INF/lib. I don't know how you set your classpath, but you shouldn't set any classpath in eclipse, and the WEB-INF directory itself should definiitely not be in the classpath.
The first and second line are basically equivalent, since they both use getSstemResource, which uses the system classloader. It's normal that they don't find your file, since the webapp's classpath is not in the system classpath.
The third line should find the file if it's in the webapp's classpath, i.e. under WEB-INF/classes or in a jar under WEB-INF/lib.
I created a Java project that contains three configuration file
log4j.XML
QueueConfig.xml
rabbitmq.properties
I put these three files into a resource folder.
Then I created a jar file of my project. This jar file is added to another project. It isn't able to find the correct location of the configuration files.
My file structure:
Thanks
you can use it like.
ClassFromWhichYouAreACcessingTheseFiles.class.getResources("resources/log4j.properties");
than if you add this jar to another project you will be able to access it.
If your config files end up in the WEB-INF/classes folder
ClassFromWhichYouAreACcessingTheseFiles.class.getClassLoader().getResources("log4j.properties");
otherwise it include the "package-path" from the ClassFromWhichYouAreACcessingTheseFiles