0

I have some problem with read file .txt in .jar exutable. I know I need to use getResourceAsStream method, but I cannot read it.

I have .jar file and I have the file "Baza pytan.txt" in mainclass, so I make it like this:

InputStream ten=getClass().getResourceAsStream("Baza pytan.txt");

if(ten == null){
    throw new FileNotFoundException();
}

Scanner in = new Scanner(ten,"UTF-8");
String linia;
String[] tokens;

Can anyone help me in this problem?

I use read file in GUI program. In other GUI project I made the same method to read file and it worked perfectly with ResourceAsStream, but in this GUI I don't really know why not.

enter image description here

structure in Quiz.jar\Programik:

enter image description here

Luka Kerr
  • 4,161
  • 7
  • 39
  • 50
  • 1
    What is the problem? – ROMANIA_engineer Jan 10 '16 at 21:00
  • Which exception do you see in the console ? – bvdb Jan 10 '16 at 21:01
  • 2
    Possible duplicate of [How to read a file from jar in Java?](http://stackoverflow.com/questions/3369794/how-to-read-a-file-from-jar-in-java) – A_Di-Matteo Jan 10 '16 at 21:01
  • I always have FileNotFoundExeption, i know how to get to path where the file is, i use many differents methods, but anyone really works. – G. Olszewski Jan 10 '16 at 21:03
  • Lets start from basics. Are you sure that this file is inside Jar? In which location it is precisely? – Pshemo Jan 10 '16 at 21:05
  • Could you update your post with link to image of your project structure and information to which class this code belongs to? – Pshemo Jan 10 '16 at 21:09
  • I update my project structure. I Have only main class Programik.java and the file "Baza pytan.txt". In .jar I have Programik folder and there I have this:(other photo). – G. Olszewski Jan 10 '16 at 21:17
  • I just installed NetBeans, and tried to reproduce your problem but it works fine for me. – Pshemo Jan 10 '16 at 21:50
  • But maybe issue is that I made GUI projector (add buttons, frames etc.) in Netbeans and I dont know why the .jar file have that many fragments of main class. Do you see this on the screen? – G. Olszewski Jan 10 '16 at 21:53
  • Ok, i understand, but this won't make any issue, right? I name my class "Programik" as you see, the skeleton of code was made by netbeans (by adding buttons, panels etc.), then just I add listeners to every components. Everythink work good iside IDE but in .jar the only issue is reading as i tell – G. Olszewski Jan 10 '16 at 21:58
  • Both method dont work. I try many combines and I didnt find solutions on Internet. I am really confused already with that just simply thing.... – G. Olszewski Jan 10 '16 at 22:12
  • Start new project and add minimal amount of code and resources which will let us reproduce your problem. In other words create and post [SSCCE](http://sscce.org)/[MCVE](http://stackoverflow.com/help/mcve). – Pshemo Jan 10 '16 at 22:24
  • I removed some of my comments since they ware based on false assumptions. You can remove some of yours too if you wish (we can also remove our entire conversation when you update your question with SSCCE to remove this possible noise about lack of proper example). – Pshemo Jan 10 '16 at 22:34
  • I dont know how to remake my problem to simple project. I also made GUI with Card Layout, one panel with one button, but this just work... The project also create the anonymous listoners.... – G. Olszewski Jan 10 '16 at 22:45
  • Try to recreate only part of structure of your code which can't find the file. Skip the rest of code not involved with it. – Pshemo Jan 10 '16 at 22:56

1 Answers1

0

This will look for the file in the same package as the class. i.e. the same directory.

InputStream ten = getClass().getResourceAsStream("Baza pytan.txt");

Say your class is com.mycom.pkg.MyClass, the file must be in /com/mycom/pkg/

This way each package can have files "local" to them and the same resoruce name can be used in multiple JARs without conflict (provided they use different packages)

However, if the file is at the top level, you need to ignore the class with

InputStream ten = getClass().getClassLoader().getResourceAsStream("Baza pytan.txt");

This will look for the file in the top level directory or any JAR in your class path.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130