Not able to get JSON file from resource folder using relative path - java

I am trying to get a JSON file form my resource folder in the main method using relative path. The code works using absolute path but this breaks once I build a jar file from my project which is want I want.
public static void main(String[] args) throws FileNotFoundException {
// Read in database
db = Database.read(Thread.currentThread().getContextClassLoader().getResource("JSON/inhabitants.json").toExternalForm());
names = db.getAllNames();
Read calls a method in Database which uses a inputstream to read the file.
public static Database read(String filename) throws FileNotFoundException {
InputStream is = new FileInputStream(filename);
Reader reader = new InputStreamReader(is);
return gson.fromJson(reader, Database.class);
}
The error I am getting is the following :
java.io.FileNotFoundException:
file:/Users/timpelser/IdeaProjects/TurfApp/target/classes/JSON/inhabitants.json
(No such file or directory) at java.io.FileInputStream.open0(Native
Method) at java.io.FileInputStream.open(FileInputStream.java:195) at
java.io.FileInputStream.(FileInputStream.java:138) at
java.io.FileInputStream.(FileInputStream.java:93) at
Core.Database.read(Database.java:22) at Main.main(Main.java:51) ...
11 more
The file in directory /Users/timpelser/IdeaProjects/TurfApp/target/classes/JSON/inhabitants.json
does exist however so I have no idea what is going wrong.
Here is my folder structure (Maven basic structure):
Is there a solution which will still able me to deploy it as a jar file ?
EDIT (25/09) : If I use getResourceAsStream instead of getResource, I am getting the following error :
Caused by: java.io.FileNotFoundException: java.io.BufferedInputStream#4f8e5cde (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at Core.Database.read(Database.java:22)
at Main.main(Main.java:51)
... 11 more

You have to use getResourceAsStream to read files from within the running jar (which contains the files within src\main\resources)!

Use This,
Resource resource = new classpathResource(json);
new ObjectMapper.readValue(resource.getInputStream(),Object.class);

Related

File not found When it's there

For some reason (new to Java), when I'm trying to read an Excel file from my resources folder, it shows that it is there, but when I use FileInputStream to read it I get a FileNotFound Exception. Any ideas?
Code:
public static void openExcelSheet() throws IOException {
FileInputStream fileInputStream = null;
if(applicationSettings.class.getResourceAsStream("/files/Employees.xlsx") != null) {
System.out.println("File Found");
fileInputStream = new FileInputStream("/files/Employees.xlsx");
}else {
System.out.println("File Not Found");
}
XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
//int numberOfSheets = workbook.getNumberOfSheets();
System.out.println(workbook.getAllNames());
workbook.close();
}
Here is the Output that I am receiving:
File Found
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:941)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: java.io.FileNotFoundException: /files/Employees.xlsx (No such file or directory)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:220)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:158)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:113)
at applicationSettings.openExcelSheet(applicationSettings.java:32)
at loginScreen.start(loginScreen.java:70)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
Exception running application loginScreen
The problem seems to be some incorrect assumptions in your code:
if (applicationSettings.class.getResourceAsStream("/files/Employees.xlsx") != null) {
System.out.println("File Found");
fileInputStream = new FileInputStream("/files/Employees.xlsx");
} else {
System.out.println("File Not Found");
}
So this is saying: "if I can find "Employees.xlsx" on the resource path, I can find it in the file system with the same path".
There are two incorrect assumptions here:
You are assuming that since you found "Employees.xlsx" on the resource path will be in the file system at all. This is not a valid assumption:
The resource could be (in fact, typically will be) a member of a JAR file or a similar file.
The resource or the resource's container could have been downloaded on the fly to a temporary file, or into memory.
The resource could have been created on the fly; e.g. by a clever class loader that decrypts or uncompresses something else.
You are assuming that "Employees.xlsx" will have the same resource path as file system path. This is pretty much guaranteed to not be the case. (It can only be the case if you put the root of the filesystem on the classpath ....)
I am not sure why you are trying to do this at all. Per #fabian's answer, POI allows you to open a spreadsheet from a InputStream. You should not need a FileInputStream here.
But in situations where you do need a FileInputStream for a resource on the resource path, the portable solution is to copy the resource to a temporary file, and then open a FileInputStream on the temporary file.
Resources are not necessarily files; They could be stored as entries in a .jar archive. Even if they are stored as files in a directory structure of the file system, the working directory may not match the current working directory. You should use the InputStream returned by getResourceAsStream directly instead of trying to open a new one:
InputStream inputStream = applicationSettings.class.getResourceAsStream("/files/Employees.xlsx");
if (inputStream != null) {
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
//int numberOfSheets = workbook.getNumberOfSheets();
System.out.println(workbook.getAllNames());
workbook.close();
} else {
System.out.println("Resource not found");
}
Provided that your file is residing inside the project folder, here is the problem remove the slash from the front of the name
fileInputStream = new FileInputStream("Employees.xlsx");
should work then. If it is inside the files folder inside the project folder then
fileInputStream = new FileInputStream("files/Employees.xlsx");
Or you can provide the complete path to the file and that should work
fileInputStream = new FileInputStream("/users/myfolder/files/Employees.xlsx");
I encountered the same issue today, which took me about two hours to partially figured it out. It was so annoying. Depending on how your class code is structured, Java does not allow you to read text file within the method definition. Try reading it in the main method, then take that FileInputStream object as input to your openExcelSheet() method. Let me know if it works :)

Java.io.FileNotFoundException for a file that's there

I am currently creating a err... annoying program that repeatedly plays a ding sound, but there is a error whenever I run it. I have tried everything, and the file IS in the correct spot. Here is my current code:
public class PlaySound {
public static void main(String[] args) throws Exception {
while (true) {
String path = PlaySound.class.getProtectionDomain().getCodeSource().getLocation().getPath().replaceAll("%20", " ");;
InputStream in = new FileInputStream(path + "//src//ding.wav");
AudioStream audioStream = new AudioStream(in);
AudioPlayer.player.start(audioStream);
TimeUnit.SECONDS.sleep(1);
}
}
}
And yes, I have used other formats of the code like //src//ding.wav
Any help would be appreciated.
EDIT: also the error is
Exception in thread "main" java.io.FileNotFoundException: C:\Users\*** ******\Desktop\ding.jar\src\ding.wav (The system cannot find the path specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at net.lookatthis.PlaySound.main(PlaySound.java:19)
EDIT2: The error is older before I renamed the file. I changed the error to reflect the current filename
I think you're trying to read a resource in a jar file specifying the absolute path of your hard disk drive.
ding.jar\src\hit.wav
So there are two alternatives, or unzip your ding.jar file into a directory.
Or specify the relative path and access to the file using the classloader resource reader.
Using the resource reader you can find to the hit.wav file using
InputStream in = PlaySound.class.getResource("/ding.wav").openStream();
AudioStream audioStream = new AudioStream(in);
You probably need to unjar your ding.jar to expand its file structure. FileInputStream (probably?) can't access the jar'ed version of this file.

Java Exception error i/o

I am getting an error when I try to read my file abc.txt in my D drive.
Even I tried format : "D:\EDU\java\abc.txt"
Here's my code :
package javapro;
import java.io.FileInputStream;
public class office {
public static void main (String[] args)throws Exception {
FileInputStream apple = new FileInputStream ("D:/EDU/java/abc.txt");
int din;
while ((din=apple.read())!=-1){
System.out.println((char)din);
}
apple.close();
}
}
My Error :
Exception in thread "main" java.io.FileNotFoundException: D:\EDU\java\abc.txt (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at javapro.office.main(office.java:8)
Make sure the file is actually located in that directory. Right-click and click on Properties to check the path.
If you've done that, change all the \ to / or \\.
The error is self-explanatory. The file isn't where you have told the application it is. Check your path to make sure that it leads to the file.
1) Change the code as below
FileInputStream apple = new FileInputStream ("D:\\EDU\\java\\abc.txt");
or
InputStream is = getClass().getResourceAsStream("abc.txt");
//if abc.txt is present in classpath
From InputStream, you have to read the data.
EDIT: Resolve non static error
InputStream is = office.class.getClass().getResourceAsStream("abc.txt");

How to scan an a txt within the same package?

I am trying to use Java.util.scanner on a txt to parse it. I am having trouble trying to scan the file because the file isn't found.
My class is within the same package of the txt file. So can you please tell me how to scan a file within the same package of the class.
public class PhoneBook {
private ArrayList<PhoneBookEntry>[] buckets;
public void load() {
try {
Scanner scan = new Scanner(new File("phone.txt"));
} catch (FileNotFoundException e) {
System.out.println("File Not Found");
}
}
public static void main(String[]args) {
PhoneBook phone = new PhoneBook();
phone.load();
}
}
Here is the StackTrace of the error
java.io.FileNotFoundException: phone.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at HashSet.PhoneBook.load(PhoneBook.java:13)
at HashSet.PhoneBook.main(PhoneBook.java:23)
Put the file in the root of your class path (after compilation, your file should appear in the root folder where .class files are generated) and use the code syntax below:
InputStream is = getClass().getClassLoader().getResourceAsStream("phone.txt");
Scanner fileScanner = new Scanner(is);
new File("phone.txt")
looks up the file in the current working directory. This is the directory where you started the java virtual machine.
You say that it is located in the same package as the class, which is most likely a different directory (e.g. src/com/mypackage).
Try moving the file to the root directory of your project. You should then be able to load it.
A different approach is shown in the answer from #Yogendra, which should work also when the file is located in the same package binary directory as your .class file. But I am not sure if you want to load this kind of file as a resource.
As stated in one of the comments, you need to put the file where the .jar is generated. If you're running from Netbeans/Eclipse, this will be the root folder of your project, if you're running from the compiled .jar, it will be the same directory as the .jar. Packages are only folders used to organise and separate source files.
But if you want to have the file in the same package anyway, you can always do something like (presuming you only have one package):
new File("./src/myPackage/myFile.txt")

Fetching a resource in Java main method

I'm trying to open a resource in my Java application by calling MainClass.class.getResource("/Resources/file.extension") and passing it to File's constructor with getPath(). Next, when I initialize a new FileInputStream with the File, I get a FileNotFoundException. The complete stack trace looks this.
java.io.FileNotFoundException: E:\user\Documents\NetBeansProjects\Project name\build\classes\Resources\file.csv (The system cannot find the path specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at my.secret.project.MainClass.main(MainClass.java:27)
Here's my code.
File file = new File(MainClass.class.getResource("/Resources/file.extension").getPath());
...
InputStream in = new FileInputStream(file);
Your whole code can be replaced with simple:
InputStream in = MainClass.class.getResourceAsStream("/Resources/file.extension");
No need to use File. In fact the file on your CLASSPATH might be pointing to some location inside JAR/WAR, which definitely won't work. Have a loot at Class.getResourceAsStream() for details.

Categories