I am developing an application in Spring-Boot with an embedded tomcat server.
My program downloads documents to the public class path inside the resources folder. Also I am accessing the files downloaded in Iframe tag in HTML, so the file should be in server for me to access it.
When I run the code from my eclipse IDE , it works fine, and I am able to access the files but when I run the war from the command prompt(Since it has an embedded tomcat server) I am unable to locate the folders. Please note that I opened the war and was able to find the folders but the program couldn`t access it.
Any Idea on how to access class resources folder from tomcat embedded war in java? Any help would be much appreciated.
TIA
Try:
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream inputDataStream = classLoader.getResourceAsStream("some-file.txt");
Related
I have an application running fine on localhost but I am having issues when It is deployed on tomcat
The code I am using to read the file is :
File jasperFile = new File(getClass().getClassLoader().getResource("reports/Header.jasper").getFile());
I get this error in catalina :
net.sf.jasperreports.engine.JRException: java.io.FileNotFoundException: file:/usr/local/apache-tomcat9/webapps/com.peek.facture.server/WEB-INF/lib/facture.server-1.0.0-SNAPSHOT.jar!/reports/Header.jasper
What triggers me is the "!" at the end of the jar name, where does it come from?
Also I have tried to download the jar, extract it, and my Header.jasper is correctly in the resources/reports/ folder
When you run on your local a stand-alone physical file Header.jasper exists (you can physically see it when you browse the reports directory).
However when you deploy to a tomcat server, that stand-alone physical file no longer exists. Instead, if you set-up your build correctly, when you open up your jar (facture.server-1.0.0-SNAPSHOT.jar), there should be a directory called reports in it with the file Header.jasper within that directory.
So when your try get a resource via getClass().getClassLoader().getResource(...).getFile() you are actually trying to access a stand-alone physical file. Instead you need to get the resource as an InputStream and then work with if from there...
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("reports/Header.jasper");
When working with resources, it's always better to rather access them this way. Especially if you are planning to deploy anywhere with a single artifact, because your resources should be packaged in with your artifact.
Application Server : IBM Websphere
Java 6
Issue :
Below is the folder structure of web application :
WEB-INF/libs/props/
The issue is below code is not working :
this.getClass().getResourceAsStream('/props'/+fileName) where fileName is a valid file name inside props folder of WEB-INF/lib/pros.
The class which is calling above method is inside a JAR and included in lib
The above call returns NULL.
The same set up works on tomcat with web application deployed in expanded mode integrated inside Inteliij IDE. On environment where this issue is coming , the application is deployed as ear
Please help as I not able to get any clue on this
The jar files under WEB-INF/lib are in the classpath. Not WEB-INF/lib itself.
The file should be in one of those jars, or under WEB-INF/classes (which is in the classpath)
My GWT project runs nicely in developement mode but when I put it on server it can't find an XML file.
My file is in src/main/webapp and when I do mvn install it shows up in target/<projectname>-1.0-SNAPSHOT
I try to access the file like this:
FileInputStream inputStream = new FileInputStream("testobj.xml");
and it throws
java.io.FileNotFoundException: testobj.xml (The system cannot find the file specified)
really puzzled by it .. haven't found any useful links on this either.
FileInputStream inputStream = new FileInputStream("testobj.xml");
To make that a valid path you have to place the xml in same folder where your class file is there.
And the good practice is that put the file in WEB-INF folder and access the path like
getSevletContext.getRealPath("/WEB-INF/resources/testobj.xml");
You might placed the file in src and it is taking from system path. Once you compile the project, your java files converts to class files and places in WEB-INF/classes folder, where the context has been changed.
To maintain the consistency for both in development mode and live environment access files from WEB-INF folder with real path.
It can be seen in development because testobj.xml is able to be found on the path in development. After your project is packaged and built it needs to be in your WEB-INF folder in the war. It is generally good practices to put your resources in src/main/resources as well, not the root folder.
Whatever you are using for your build will need to copy your resources to WEB-INF when creating a war. If you are using maven see this thread for how to accomplish this: Maven: how to get a war package with resources copied in WEB-INF?
FileInputStream inputStream = new FileInputStream("testobj.xml");
This line tries to access "testobj.xml" in the process's current directory. When you run this within a web app, it'll look for the file within the application server process's current directory. This directory could be anything, and it's unlikely that the file will be there.
The normal way to read resources packaged with the web app is to use the web app's ClassLoader:
InputStream is = getClass().getClassLoader().getResourceAsStream("resources/testobj.xml");
This will automatically search the web application's deployment files for the named file. See this question for more discussion.
I've got some weird problem. I get access to my resources files like this:
File xmlFile = new File(getClass().getResource(xmlPath).getPath());
Where xmlPath is "/META-INF/file.xml".
When I run from Eclipse, everything works fine. Unfortunately, when I pack everything to jnlp file, upload with my web app on tomcat (from where I download all jar's by jnlp) it stops work.
When I run my jnlp, it downloads all jar's like it should and fails to start. Throwing this exception:
java.io.FileNotFoundException: C:\Users\A050868\Desktop\http:\address:port\webapp\downloads\lib\package.jar!\META-INF\componentContext.xml (The filename, directory name, or volume label syntax is incorrect)
How can I get access to my file, which is in resources/META-INF folder, in cached locale jar copy? For now it seems, like Java try get access to jar on server side - no this local, downloaded by jnlp.
Any ideas?
All the files are packed together in your jnlp file. They don't to exist as individual files on filesystem when you port your package.
That said, they are available on the classpath. You can access the content of your package using the appropriate classloader.
getClass().getClassLoader().getResourceAsStream(...)
This may help you
I have a Java web application. Inside the WAR I have a folder containing configuration files for the application. I need to know the path of the folder in order to load the files at runtime.
I also need the solution to work in Tomcat and in WebSphere.
Thanks.
I would suggest placing the files under WEB-INF/classes and simplying loading them from the classpath, not from the filesystem. This way, the path is always the same.
You can use something like:
InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("path");