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.

user85421
  • 28,957
  • 10
  • 64
  • 87
Subhajit
  • 876
  • 3
  • 17
  • 37
  • 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 .jar – Karim Jan 20 '17 at 11:07

4 Answers4

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.

Subhajit
  • 876
  • 3
  • 17
  • 37
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()

Community
  • 1
  • 1
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • 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 ? – Subhajit Jan 20 '17 at 11:37
  • 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 @subhajit – Youcef LAIDANI Jan 20 '17 at 11:41
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).

dsh
  • 12,037
  • 3
  • 33
  • 51
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.

Steve Smith
  • 2,244
  • 2
  • 18
  • 22
  • 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. – Subhajit Jan 20 '17 at 14:11
  • Does 'E:\Projects\GLB\folder\mytext.txt' exist and do you have permission to read it? – Steve Smith Jan 20 '17 at 14:52
  • Yes @stevesmith . – Subhajit Jan 21 '17 at 06:02