0

I'm joking with a java trying to program a game, I'm not going to define useless details, the program works, but it works only if I get it from the point where I developed it (Netbeans), but if I try to get it started Jar nothing, I read on the internet that to access files, such as images or txt files, you have to use the Class.class.getResourceAsStream (path), but the latter does not work from starting it from the jar, in fact or me From a NullPointerException or goes smooth, but does not load anything.

Disappeared I would explain the structure of my project and show you my code:

List build

 -build
    - classes
        - mario
           -all the package and the file example: crediti/ringraziamenti.txt

List dist

    -dist
         -jarFile
              -mario
                    -all the package and the file example: crediti/ringraziamenti.txt
              -meta inf...

List java

    -src
        -mario
              - all the package and the file example: crediti/ringraziamenti.txt

my code is:

        InputStream io =(getClass().getClassLoader().getResourceAsStream("mario/crediti/ringraziamenti.txt"));
        BufferedReader br = new BufferedReader(new InputStreamReader(io));

        String temp = "";
        while((temp = br.readLine())== null){
            System.out.println(""+temp);
        }
  • Do you get any errors? – Rabbit Guy Jul 20 '17 at 14:16
  • 1
    `while((temp = br.readLine())== null){` - the `==` is very suspicious. – Mat Jul 20 '17 at 14:16
  • Cabbage that figure ... Tiredness must have given me the head to commit such a mistake ... And think that it was two hours I did not understand where the problem was, what a shame ... Thanks anyway to have pointed out to me my mistake. – Christian Mantini Jul 20 '17 at 14:34

2 Answers2

-1

Specify a resource folder, put your files there. Assuming you have structure:

-src -mario - all the package and the file example: crediti/ringraziamenti.txt

add the resource folder on the same level as your packages, move your files there and try:

InputStream io =(getClass().getClassLoader().getResourceAsStream("/crediti/ringraziamenti.txt"));
lazyneuron
  • 537
  • 5
  • 12
-2

Fix your code as follows:

  1. Put a slash in front of the path in the getResourceAsStream call, otherwise it will start searching from the package of the current class.
  2. In the while loop, compare != null rather than == null - you want the loop to continue running as long as there lines of text, not as long as there are no lines of text.
InputStream io =(getClass().getClassLoader().getResourceAsStream("/mario/crediti/ringraziamenti.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(io));

String temp;
while((temp = br.readLine()) != null){
    System.out.println(""+temp);
}
Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
  • “otherwise it will start searching from the package of the current class” is not correct. Class.getResourceAsStream works that way; Class**Loader**.getResourceAsStream does not. An initial slash is never valid for the latter method. – VGR Jul 20 '17 at 14:46