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 :)
Related
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);
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.
I'm trying to incorporate a css file to my JavaFX appllication, by the following snippet:
public void loadExternalCSS() {
System.out.println("CLASSPATH: "+System.getProperty("java.class.path"));
try{
skinCSS = getClass().getResource("css/default_skin.css").toExternalForm();
}
catch(Exception e){
System.err.println("Exception: " + e);
e.printStackTrace(System.err);
}
}
Which yields, at runtime:
java.lang.NullPointerException
at
robotikosanomologitos.RobotikosAnomologitos.loadExternalCSS(RobotikosAnomologitos.java:529)
at robotikosanomologitos.RobotikosAnomologitos.start(RobotikosAnomologitos.java:491)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
The weird thing is that a few lines above this function, I have a small function that reads from a properties file.
public void readPropertiesFile() {
Properties props = new Properties();
InputStream is;
try {
File f = new File("properties");
is = new FileInputStream(f);
}
/* etcetera */
}
This works like a charm. The properties file is located at the root of the project directory, C:\~my_projects_folder~\RobotikosAnomologitos
After searching around for a solution, I saw that getClass().getResource() attempts to find a resource in the classpath. I tried printing the classpath at every run, and I get:
RobotikosAnomologitos\dist\run125323585\RobotikosAnomologitos.jar which is logical enough.
After looking inside this temporary folder while running the program, though, I can find no css folder nor css file.
But the file is indeed located in my working project directory, under RobotikosAnomologitos\css\default_skin.css. For some reason it doesn't make it in the classpath at runtime, causing getResource() to return null when looking for it.
Any ideas on how to include it?
EDIT: I forgot to mention that I have also placed css/default_skin.css under the src package, and shows up in Netbeans' package tree (src/css/default_skin.css).
In the same way, I have some graphics that are located under src/graphics/ which get loaded fine by getClass().getResourceAsStream(). Which also bafflesss me as to why the css file can't be found. Maybe it doesn't get compiled in the jar?
If you call getResource() for a class and do not prepend a /, the path is considered to be relative to the package of the class.
If you've properly added the resources to the classpath, this should work:
skinCSS = getClass().getResource("/css/default_skin.css").toExternalForm();
You need to add css folder in classpath if you want retrieve using getClass().getResource("css/default_skin.css").toExternalForm();
Check if you are not using binary encode of CSS files (see Project Properties>Packaging). If you are using, the mentioned file extension should be ".bss".
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.
I am trying to initialise a FileInputStream object using a File object. I am getting a FileNotFound error on the line
fis = new FileInputStream(file);
This is strange since I have opened this file through the same method to do regex many times.
My method is as follows:
private BufferedInputStream fileToBIS(File file){
FileInputStream fis = null;
BufferedInputStream bis =null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bis;
}
java.io.FileNotFoundException: C:\dev\server\tomcat6\webapps\sample-site (Access is denied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.(Unknown Source)
at java.io.FileInputStream.(Unknown Source)
at controller.ScanEditRegions.fileToBIS(ScanEditRegions.java:52)
at controller.ScanEditRegions.tidyHTML(ScanEditRegions.java:38)
at controller.ScanEditRegions.process(ScanEditRegions.java:64)
at controller.ScanEditRegions.visitAllDirsAndFiles(ScanEditRegions.java:148)
at controller.Manager.main(Manager.java:10)
Judging by the stacktrace you pasted in your post I'd guess that you do not have the rights to read the file.
The File class allows you to performs useful checks on a file, some of them:
boolean canExecute();
boolean canRead();
boolean canWrite();
boolean exists();
boolean isFile();
boolean isDirectory();
For example, you could check for: exists() && isFile() && canRead() and print a better error-message depending on the reason why you cant read the file.
You might want to make sure that (in order of likely-hood):
The file exists.
The file is not a directory.
You or the Java process have permissions to open the file.
Another process doesn't have a lock on the file (likely, as you would probably receive a standard IOException instead of FileNotFoundException)
This is has to do with file permissions settings in the OS. You've started the java process as a user who has no access rights to the specific directory.
I think you are executing the statement from eclipse or any java IDE and target file is also present in IDE workspace. You are getting the error as Eclipse cant read the target file in the same workspace. You can run your code from command prompt. It should not through any exception.