FileNotFoundException while running as a jar
Asked Answered
J

4

12

FileInputStream fstream = new FileInputStream("abc.txt")

is throwing a FileNotFoundExceptionn while running as a jar. Why ? Normally it is able to find while running from main method.

Johanajohanan answered 20/1, 2017 at 11:3 Comment(1)
you should be sure that the path of the file is the same running the app from the .jar. do you have abc.txt in the same folder as the .jarDownstroke
J
3
class MyClass{

    InputStream fstream = this.getClass().getResourceAsStream("abc.txt");

}

This code should be used. And the files(in this case abc.txt) should be kept , in the Object references class location. That means , this.getClass refers to the location of some folder i.e, com/myfolder/MyClass.java folder .

So we should keep the abc.txt in com/myfolder this location.

Johanajohanan answered 23/1, 2017 at 6:3 Comment(0)
S
2

If your file is packaged with your jar then you should to get information using getClass().getResource(url):

FileInputStream inputStream = 
new FileInputStream(new File(getClass().getResource(/path/to/your/file/abc.txt).toURI()));

Else you need to create it always in the same path with your jar and you can get it like you do :

src/myJar.jar
src/folder/abc.txt

FileInputStream fstream = new FileInputStream("folder/abc.txt");

You can read here also :

How do I load a file from resource folder? and File loading by getClass().getResource()

Singlephase answered 20/1, 2017 at 11:9 Comment(2)
if this code runs fine while I am running the project directly, then why compiler is unable to find the txt file after creating it as a jar ? While creating jar does the files present outside the src folder does not get included in the jar ?Johanajohanan
because the file not exist in the same folder of your jar, what IDE you are using Netbeans or Eclipse? if you are using Netbeans then your jar is under a folder project\dist\jarfile.jar, hope you get my point @subhajitSinglephase
J
1

You can use FileInputStream only when you actually have a file on the computer's filesystem. When you package your text file in the jar file for your program, it is not a file in the filesystem. It is an entry inside the jar file.

The good news is that it is even easier, in Java, to access the file this way: it is in your classpath, so you can use getResourceAsStream().

InputStream stream = getClass().getResourceAsStream("abc.txt");

If you have your classpath set up correctly, this will work regardless of whether it is a file in a directory (such as during development), or an entry in a jar file (such as when released).

Jarboe answered 20/1, 2017 at 22:9 Comment(0)
I
0

It's because your working directory will probably be different under the two environments. Try adding the line

System.out.println(new File("abc.txt").getAbsolutePath());

to see where it is actually looking for the file.

Ie answered 20/1, 2017 at 11:38 Comment(3)
File out2 = new File("folder/mytext.txt"); System.out.println("path2 : "+out2.getAbsolutePath()); FileInputStream fstream = new FileInputStream(out2.getAbsolutePath()); path2 : E:\Projects\GLB\folder\mytext.txt java.io.FileNotFoundException: E:\Projects\GLB\folder\mytext.txt (The system cannot find the path specified) Getting this exception.Johanajohanan
Does 'E:\Projects\GLB\folder\mytext.txt' exist and do you have permission to read it?Ie
Yes @stevesmith .Johanajohanan

© 2022 - 2024 — McMap. All rights reserved.