This question already has answers here:
How do I read the manifest file for a webapp running in apache tomcat?
(7 answers)
Closed 8 years ago.
I want to use my manifest.mf file as an InputStream so I can retrieve some of the data that is inside. I use the following line of code:
inputStream = new FileInputStream("../../../../WebContent/META-INF/MANIFEST.MF");
Because the java class and the manifestfile are located in the following directory:
Unfortunately, this path always give me a FilenotFoundException. What is the correct path to refererence this file?
You want to load a file that is bundled with your web application application. This file will be part of the war file of your deployed app. It thus won't be on the file system. So loading it with a FileInputStream is not the right solution.
BTW, file paths are not relative to the class creating the FileInputStream. They're relative to the directory from which the application server is launched.
The way to load webapp resources is to use the ServletContext.getResourceAsStream() method. Read its javadoc (and the javadoc of ServletContext.getResource()) carefully.
You should also realize that WebContent is the name of the directory where the sources of your webapp are. Once packaged and deployed on a server, there won't be any WebContent directory anymore.
try this:
File file = new File("../../../../WebContent/META-INF/MANIFEST.MF");
System.out.println(file.getCanonicalPath());
see where your app think the file is located and fix the path
Related
This question already has answers here:
FileNotFoundException in src/main/resources
(4 answers)
Closed 2 years ago.
I am trying to build my project to a jar file. When I first opened the jar file I noticed nothing happened. I then launched the jar file from cmd. Then I saw it throws an error because it cannot find a txt file. This is the error:
Executing the program in IntelliJ works fine so I am assuming the txt file is not included in the artifact. This is how I use the txt file in the code:
This is what my file structure looks like:
This is what my artifact settings look like:
I tried putting the txt file in the src folder and manually including it in the artifact settings, I tried putting the txt file in the resources folder (It is marked as Resources Root) and I tried using the resources folder and also manually adding the resources folder to the artifact settings (this is the last thing I tried and is visible in the picture of my artifact settings).
I am really confused why the txt file doesn't seem to be in the jar file. Any help would be really appreciated.
Place the file in the resources folder, and instead of File try using getResourceAsStream to get the contents of the file:
InputStream in = getClass().getResourceAsStream("/data.txt");
Scanner pathScanner = new Scanner(in);
Make sure to set ?*.txt as resource pattern in the compiler options.
This question already has answers here:
Where to place and how to read configuration resource files in servlet based application?
(6 answers)
Closed 5 years ago.
I have a java web application. This app reads 2 excel file and process them and display the result on the screen. I am using Eclipse and tomcat v9 to implement and run the application locally. I have put the excel files in the root of the application. I have a function in ProcessFileUtility.java class that takes the file name of two excel files and process them. I give the absolute path of those two excel files to that function and it works on my local. But when I export the web application as .war file and deploy it on the tomcat server, I don't see my data displayed and my immediate guess is that the file path provided is an absolute path and should be some thing else. I know this question has been asked already but after reading many of them I am still struggling what to do.
Can some please help me with this problem. I have also put an screen shot of the structure of my application.
Here is the path to excel files :
ArrayDataModel<Record> records = this.processDataSources("C:\\EEworkspace\\PVvalidation\\ppmsOrigin.xlsx", "C:\\EEworkspace\\PVvalidation\\product_database-reverse-column.xlsx");
The problem is actually the path you are using. Don't expect the server to have that path and file unless you create it.
Please refer to this answer and see how the file is inside the src folder. That way your application will be able to find the file inside it's folder.
I am rather new to web applications, I have a Jersey Application. I would need to deploy the jar on a server.
I am reading my resource file successfully using the following approach (suggested in most answers to read a resource from JAR)
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("public/" + uriInfo.getPath());
When I try to look at the path the file is being loaded from it is something like
> C:\Users\....\..\myservice\target\classes\public\meta-data\myfile.txt
Is it correct to say that the resource is loaded from the jar? Because I would need to load it from the jar and not the local file system since it is a web app.
If not, what do I need to be doing so that I can read my resource file in a Web Application.
This question already has answers here:
How to get the path of a running JAR file?
(33 answers)
Closed 9 years ago.
How to reference directories correctly when launching a .jar from a batch file?
I am trying to launch a jar file from a .bat . One of the first things my code does is open and read from a file. I reference the file like this:
final ArrayList<EmailAccount> emailList = FileIO.getListOfAccountsFromFile(".\\EmailList\\list.txt");
Everything works fine if the batch file is in the same directory as my jar. HOWEVER, if I put my batch file somewhere else and try to run it (like i plan to have it work), the program thinks that I'm trying to reference a file that is in the
".bat directory"\EmailList\list.txt
instead of
".jar directory"\EmailList\list.txt
and comes up with a fileNotFoundException.
Is there any way that I can run my batch file from a different directory while correctly referencing files in relation to the jar without hard coding in the jar's file path?
You can ask the JVM about the location of a given class. If you know it is inside the jar you refer to, then you can then extract the location of the jar file, and then construct your file objects relative to that.
See https://stackoverflow.com/a/320595/53897 for details about
return new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());
This question already has answers here:
How to read file from relative path in Java project? java.io.File cannot find the path specified
(15 answers)
Closed 6 years ago.
I have exported a project to a runnable JAR in eclipse. There are a few places in my code where I've done the following.
String file = "src/Files/...";
loadFile(file); // purely for example
Now that the project is in the form of a JAR, those directories don't seem to exist and loading those files fails. I'm pretty sure that the files I'm referencing are packed in the JAR. Do the directories change in any particular way when exported to JAR? Any other ideas on how to make this work?
You need to treat them as a classpath resource, not as a local disk file system path. This isn't going to work when you package the files in a JAR and you also don't want to be dependent on the working directory. Files inside a JAR are part of the classpath already.
Assuming that you've a foo.txt file in package com.example, then you can get an InputStream of it as follows
InputStream input = getClass().getResourceAsStream("/com/example/foo.txt");
// ...
Or when you're inside static context
InputStream input = SomeClass.class.getResourceAsStream("/com/example/foo.txt");
// ...
Or when you want to scan the global classpath
InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("/com/example/foo.txt");
// ...
See also:
How to read file from relative path in Java project? java.io.File cannot find the path specified
getResourceAsStream() vs FileInputStream