Why embedded File not found exception in executable jar file? [duplicate] - java

This question already has answers here:
Load a resource contained in a jar
(3 answers)
Closed 6 years ago.
There is binary file for update my switch configuration in src/main/resources directory in run time. when i try it with debug mode in eclipse the file exist and there is no problem, but when i create an executable jar file for real application this file does not exist.
Where is the problem?
String filePath = new File("/resources/firmwares/myConfigFile.bin").getAbsolutePath();
File f = new File(filePath);
if (f.exists())
upload();

Assuming that you are using Maven and you added the resource plugin so during maven build it will copy your resources to the target dir and then during packaging it will also package them
Once your JAR is created you will need to use another method to access your file since now your file is packaged within the JAR file
To access a file that is located inside a JAR file use:
InputStream is = this.getClass().getClassLoader().getResourceAsStream("firmwares/myConfigFile.bin");
Or
URL url = this.getClass().getClassLoader().findResource("firmwares/myConfigFile.bin");

Related

IntelliJ txt file doesn't get included when building jar file [duplicate]

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.

How to read all files in a folder inside a package in an ear [duplicate]

This question already has answers here:
Get a list of resources from classpath directory
(15 answers)
How to load a folder from a .jar?
(4 answers)
Closed 3 years ago.
I have this following folder structure inside my ear
-proj1
- src
-loader.java
-proj2
- folder1
-1.json
-2.json
So I have to load these json files one by one in loader.java file to perform my operation. Project 1 has the reference of project2. I have deployed my ear in jboss6.3 eap server.
I have tried using the classloader to point to the directory, but the directory it gives is something else, but if I specify the file name directly to the loader it loads perfectly.
InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("folder1/1.json");
It loads correctly but I dont know to get the list of files in that folder directory

Using a manifest file as input in a webapplication [duplicate]

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

How can i find the path of my file in my Java project file?

I put a file inside my Java project file and i want to read it but how can i find the path name with Java.
Here i put it in C driver but i just want to find path by just writing the name of file. Is there a function for it?
FileInputStream fstream1 = new FileInputStream("C:/en-GB.dic");
If the file is inside the jar file generated for your project (or in the classpath used by your project, generally), under the package com.foo.bar, you can load it using
SomeClassOfYourProject.class.getResourceAsStream("/com/foo/bar/en-GB.dic");
If it's not in the classpath, and you launch the application (using java.exe) from the directory c:\baz, and the file is under c:\baz\boom\, the you can load it using
new FileInputStream("boom/en-GB.dic");
Place it in your classpath, If it is a web application WEB-INF/classes/yourfile.ext, if it is a standalone application place it in bin directory of your application (default class directory is bin).
Then you could read by using one of the following ways.
InputStream in = this.getClass().getClassLoader().getResourceAsStream("yourfile.ext");
Or
InputStream in = this.getClass().getResourceAsStream("/yourfile.ext");
You can read online for the differences between above two approaches.

Java: Pathnames not working once I export to JAR [duplicate]

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

Categories