Access to web-inf from deployed jar - java

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.

Related

java.io.FileNotFoundException when using tomcat

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.

Java: FileNotFound after deploying to server

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");

Playframework 2.2 : Uploaded Files in Public directory not accessible

I am having problems with accessing file in public directory after deployment.
The stage command give me a folder target\universal\bin where my .exe file is.
In development mode I used to upload my files to public\uploads\pictures and access them from this location. But, after deployment I am unable to upload the pictures. I read this Stack Link that has two options. Is it possible to define a folder directory that is not absolute.
Application Conf
myUploadPath="public/Upload/Pictures/"
Accessing folder
String myUploadPath = Play.application().configuration()
.getString(myUploadPath);
Please tell me a solution to overcome this..
Found a solution
During development we are using public directory to store anything extras(in my case uploaded files) that we have. But, while deploying the application it is important that we change the reference to these extra files. I have changed the path from public directory to the absolute path of where the file is executed using
Play.application().path().getAbsolutePath()
and store the files in this directory.
If you don't want to use this than you can also specify an External Asset

Getting reources path creates weird path (in jar works)

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

Where to put jsp project folder in tomcat for hosting?

I am right now working with JSP and using tomcat Apache for that.what my problem is when i am creating one single JSP page and putting in root directory of tomcat then its working fine.but now i have one project that contains some useful jars and other java classes so how to put that whole project directory in tomcat.
I had put that in web app directory but its giving me error as follows
The requested resource (/dailymotion-cloudkey-java-73f6f35/examples/upload.jsp) is not available.
I am Giving snapshot of my web app folder where i had put this folder name as dailymotion-cloudkey-java-73f6f35
i know that i am doing mistake while putting dailymotion-cloudkey-java-73f6f35 directory in tomcat.
but i am totally new in this so i couldn't find out so can anyone tell me
tree Structure of my project
I think dailymotion-cloudkey-java-73f6f35, does not have WEB-INF folder with web.xml file. Due to which it is not able to locate the resources.
or
You can copy your dailymotion-cloudkey-java-73f6f35 project directory into ROOT folder and then try the same URL.
Put it anywhere, but put a context file for your webapp to TOMCAT_HOME/conf/Catalina/localhost which points to for webapps's directory with the docbase attribute. Read details here.

Categories