I have an issue where I want to load a json file that is in the root folder of my app directory.So i did the simple
File file = new File("assetlinks.json");
when I run the application on my local server, the file is served correctly
but when I push online it the file is not found
it throws FileNotFoundException.
Please what am I missing here?
If it is the root of a web app, you need:
File file = new File(servletRequest
.getServletContext().getRealPath("/assetlinks.json"));
This turns an URL like path (/) relative to the web app's root into a file system File.
(BTW a subdirectory might be a better idea.)
After logging the current working directory, I discovered that it returned an empty string, this is because the app is bundled up in a container and to get resources those resources have to be explicitly added in the docker file, so simply adding the resource to docker solved the problem
ADD assetlinks.json assetlinks.json
After I did that I was able to get the file using
File file = new File("assetlinks.json");
Related
I'm currently facing a problem any time i try to access to WEB-INF/classes folder.
Basically if I run the whole project on a local server it works and find the given file, but when i try to access to the same file after all the jar files have been created and deployed it gives my an error - the file doesn't exists
this is the current path on the server :
file:/product/WebSphere85/AppServer/profiles/opntship_node_1/installedApps/opntship/xxxService.ear/lib/xxxServiceService.jar!/xxx.pdf (A file or directory in the path name does not exist.)
this is the path on my local server:
/C:/Users/foo42/IBM/rationalsdp/workspace/TpdPrintServiceService/target/classes/xxx.pdf
this is how I get the file :
getClass().getResource("/"+fullFileName);
fileToBytes(new File(filePath.getFile()));
I tried in many ways to get access to the folder but still, it works locally but not on server :(
any idea how to fix this problem and access to the web-inf folder ?
Thanks in advance!
We cannot read the entries within an archive (xxxService.ear and xxxServiceService.jar) like it was a plain old File. You may want to try Thread.currentThread().getContextClassLoader().getResourceAsStream(path) instead.
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.
I am working on a web app i have java files in it which uses certain files.I want to specify these files using relative path in java so that it doesn't produce mobility issue.But Where should i place a file in a web app so that i can use relative path.? I have tried placing the files under source package, web folder, directly under the web-application.Please help.Thanks in advance
The simplest way to get the current directory of a java application is :
System.out.println(new File(".").getAbsolutePath());
Like that you can consider the given path as the root of your application.
Cheers,
Maxime.
Read the file as a resource. Put it somewhere in the src. For instance
src/resources/myresource.txt
Then you can just do
InputStream is = getClass().getResourceAsStream("/resources/myresource.txt");
Note: if you are using maven, then you are more accustomed to something like this
src/main/resources/myresource.txt
With maven, everything in the main/resources folder gets built to the root, so you would leave out the resources in your path
InputStream is = getClass().getResourceAsStream("/myresource.txt");
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 am working on web application.I invoke on my jsp request.getContextPath(), but strangely I got address /streetshop.
Then I am appending some path as request.getContextPath() + "abc" and create folder.
Then its creating folder in D:// instead of my webapplication folder.
Please, tell me, I want to upload an image in put it in my web-application root/images/images.gif.
You mix things up here. HttpServletRequest.getContextPath() returns your web application root path. In your example this is /streetshop, so your URL may look similar to www.myapp.com/streetshop. If you want to access the internal file system path, you must obtain it from the ServletContext using request.getServletContext().getRealPath("/"). This should return the location of your WAR files' WebContent folder.
Keep in mind that if you modify contents of this path during runtime, you're going to loose everything when redeploying your application.