Reading file from WEB-INF folder - NoSuchFileException - java

I have created a Jersey REST project, and I am running on a Tomcat server having exported it as a WAR file. One of the operations that the project carries out is reading a CSV file from the WEB-INF directory.
However when attempting to do this, I am seeing a java.nio.file.NoSuchFileException being thrown. The read is being done from the following location:
Reader reader = Files.newBufferedReader(Paths.get("WebContent/WEB-INF/resources/mycsvfile.csv";
..which corresponds to the location in my project structure. The application is having no issue reading the file in JUnit tests, it's only when deployed that I see this issue.
Is there a specific location where resources such as files should be placed in a web based project?
Thanks for any help.

Related

Create and access zip file from resource folder during runtime in java spring boot application

My requirement is, I have to create one zip file and store in resource folder and once it's created, i have to pass this zip file to another api(which one will accept only .zip file as a input)through rest template.
Now my problem is after creating zip file im not able to access it. I think this is because the file is not available in target folder.
How to solve this problem?
Note: all these activities should happen in a single call.
Technology: java 11, spring boot, maven Latest
the resource folder is normaly part of a jar. i would create a temp folder (java.nio.file.Files.createTempDirectory()) and store the zip there.

Query regarding reading the file from within jar

I am developing a spring boot application which would be used as a non-executable jar by some other user application. The spring boot application(jar) reads the xsd file present in resources folder. When we run it as a standalone application it executes fine.
Code to read the xsd file from resources folder -
File xsdValue = new ClassPathResource("xsd/" + xsdFileName + ".xsd").getFile();
But the problem is, When user application calls the jar then it tries to find the xsd in its own resources folder rather than that of jar's resources folder.
Please advise !!
Any help is much appreciated.
Resources in jar-files can not be gotten as File class, use getResourceAsStream method of ClassLoader class instead.

(Tomcat 7) Deployed jar not finding files

I deployed a war file onto a Tomcat 7 instance running on a remote Linux machine and I'm getting FileNotFoundExceptions.
One of the referenced jars in the project, which contains code that I did not write, uses several files (which I have included, but it is not finding). These files are located in the classes folder. It appears the classpath I have set for the project is being ignored by this jar. These files that it uses, e.g. .properties files are external to the jar.
Here is an example of how it is invoking the files:
FileOutputStream fos = new FileOutputStream("Key.ser");
I was getting these errors when developing the source project in Eclipse. I was able to configure the project to tell it where to find these files via Run Configurations -> Arguments -> Other but the exported .war file appears to not have this bundled with it, only the source project has it. Now I'm seeing them again when trying to deploy the application to Tomcat on another server via war file.
How do I configure the deployed jar file in the deployed Tomcat 7 webapp to find these files that the jar uses? I am loathe to change the code since I did not write it so am really hoping to avoid this.
I am able to get this to work on a local Tomcat 7 running on Windows instance integrated with Eclipse as explained earlier so I'm wondering if maybe this can be duplicated?
You will not be able to find the file by simply referencing the file name using FileOutputStream. You are correct to place the file in the 'WEB-INF/classes' directory, which will allow it to be located on the classpath.
To load the file, you need to load it as a classpath resource using something similar to this:
String classpathLocation = ""Key.ser"";
URL classpathResource = Thread.currentThread().getContextClassLoader().getResource(classpathLocation);
// Or if you want it as an inputstream:
InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream(classpathLocation);

What is the difference between getResourceAsStream("Words.txt") and FileInputStream("./src/package/Words.txt")? [duplicate]

This question already has answers here:
getResourceAsStream() vs FileInputStream
(6 answers)
Closed 7 years ago.
I am currently writing a servlet based application (the client side). I tried to get a text file inside the same package where the code is located. All of the methods that I have come across used either MyClass.class.getResourceAsStream("Words.txt") or classLoader.getResourceAsStream("Words.txt") to get the text file (eg: SO1, SO2). But I have tried FileInputStream("./src/package/Words.txt") and the text file can still be successfully loaded.
What are the differences? And why is the method getResourceAsStream encouraged?
At the moment, you're on your developer workstation, and are probably running your app from your IDE. Tomcat happens to be started from the IDE project root directory, and thus using
new FileInputStream("./src/package/Words.txt")
allows reading the file stored in your project src directory.
But that's not how the project will be run in production. In production, you'll have a Tomcat server started from a totally different directory, using a shell script. And the production server won't have the source project at all. All it will have is Tomcat, and the war file constituting the artifact built from the project.
So there will be no src directory at all, and the file Words.txt won't even be anywhere on the file system. It will only be en entry of the war file (which is in fact a zip file), located under WEB-INF/classes/package along with the .class files produced by the compiler from your Java source files.
So, in order to be able to read that "file", you can't use file IO: the "file" doesn't exist in the file system. You need to use the ClassLoader that will locate the "file" inside the war file and load it from there.
That will also go fine during development, when the app is run from an exploded war structure: the class loader will find the class under the target directory used by your IDE to store the class files and resource files.
Note that what you need to load that resource, if it's in the package com.foo and MyClass is in that same package, is
MyClass.class.getResourceAsStream("Words.txt")
or
AnyOtherOfYourClassesWhateverThePackageIs.class.getResourceAsStream("/com/foo/Words.txt")
or
classLoader.getResourceAsStream("com/foo/Words.txt")

where should i put the configuration files in a webservice

Could some one help me on this problem. i have webservice , which reads data from configuration files. When i run this webservice from eclipse , i give absolute the path for these webservices of these configuration files , but when i shift the webservice in to server and run, it can not read the config file. so how can i solve this problem. is there a relative path that webservice can understand during run time.
You can put your configuration files in the root of the AAR archive or in the classes folder. Then use getResourceAsStream to read them.
ClassLoader loader = getClass().getClassLoader();
InputStream inputstream = loader.getResourceAsStream(sFilePath);
How are you deploying these configurations. If they're packed inside the .aar file I would expect them to reside in the classpath, and you could access them via Class.getResourceAsStream()
If they're deployed in the .aar file, however, they're going to be difficult to edit post deployment. In that case you may want to deploy them separately as files and put them in a location well-known to the application, and just read them as files.

Categories

Resources