title: How could I use Java Input/Output streams to make my eclipse .jar game work?
body: <p>I spent the last 10 hours trying to solve <strong>jar file issues</strong>, I know I have to change code to make both my game works in Eclipse and the jar file, but I can't manage to do it !</p>
  
  <p>I have a <strong>resources source folder</strong> in which we can find the <strong>res</strong> package containing a .png and a package <strong>sounds</strong> of .wav.</p>
  
  <p>I have this <strong>resources</strong> and <strong>res</strong> folders in the folder where I put the jars as all-in jar file generated even more errors.</p>
  
  <p>I tried many different solutions found on the net, such as <em>Class.class.getResourceAsStream</em>, <em>MyClass.class.getResourceAsStream</em>, <em>getClass().get</em>..., I used <em>InputStreamReader</em> for reading a file and <em>OutputStreamWriter</em> for writing in it but this does not work in both Eclipse and jar while a simple PrintWriter works in Eclipse only.</p>
  
  <p>Here is, for instance, one of the methods which gives <em>NullPointerException</em> at Windows command prompt when running jar file with the java -jar x.jar command:  </p>
  
  <pre><code>public static ArrayList&lt;String&gt; loadScores(){
          createFile("./resources/res/scores.txt");
          ArrayList&lt;String&gt; scoresInfos = new ArrayList&lt;String&gt;();
  
          InputStreamReader isr = null;
          try {
              isr = new InputStreamReader(Class.class.getResourceAsStream("/res/scores.txt"));
              //fileReader = new FileReader("./resources/res/scores.txt");
          } catch (Exception e1) {
              e1.printStackTrace();
          }
  
          String line;
          try(BufferedReader br = new BufferedReader(isr);) {
              line = br.readLine();
              while(line != null){
                  scoresInfos.add(line);
                  line = br.readLine();
              }
          } catch (IOException e) {
              e.printStackTrace();
          }
          return scoresInfos;
      }
  </code></pre>
  
  <p>Create file looks like this, I do write a "0" to avoid the emptiness of the file:</p>
  
  <pre><code>private static void createFile(String path){
      try{
          File file = (new File(path));
          if(file.exists()) return;
  
          PrintWriter writer = new PrintWriter(path, "UTF-8");
          writer.println("0");
          writer.close();
      }
      catch (IOException e) {
  
      }
  }
  </code></pre>
  
  <p>So the loadScores method works in Eclipse but when I launch the jar (generated with Eclipse and then JarSplice for the main class) I get : </p>
  
  <pre><code>java.lang.NullPointerException
          at java.io.Reader.&lt;init&gt;(Unknown Source)
          at java.io.InputStreamReader.&lt;init&gt;(Unknown Source)
          at toolbox.Save.loadScores(Save.java:53)
          at gameTester.Game.loadScores(Game.java:103)
          at gameTester.Game.&lt;init&gt;(Game.java:64)
          at gui.Menu.main(Menu.java:177)
  Exception in thread "main" java.lang.NullPointerException
          at java.io.Reader.&lt;init&gt;(Unknown Source)
          at java.io.BufferedReader.&lt;init&gt;(Unknown Source)
          at java.io.BufferedReader.&lt;init&gt;(Unknown Source)
          at toolbox.Save.loadScores(Save.java:61)
          at gameTester.Game.loadScores(Game.java:103)
          at gameTester.Game.&lt;init&gt;(Game.java:64)
          at gui.Menu.main(Menu.java:177)
  </code></pre>
  
  <p>Which is aiming this line :</p>
  
  <pre><code>isr = new InputStreamReader(Class.class.getResourceAsStream("/res/scores.txt"));
  </code></pre>
  
  <p>I also have same type of issues using Audio,            </p>
  
  <pre><code>AudioInputStream ais =
                  AudioSystem.getAudioInputStream(
                      Class.class.getResourceAsStream(s)
                  );
  </code></pre>
  
  <p>works very well in Eclipse but not in the jar neither (s being the absolute path).</p>
  
  <p>I sometimes manage to avoid errors on Eclipse by changing the file paths, but I never had both Eclipse and the .jar file working !</p>
  
  <p>It would be awesome that someone helps me find my error(s) !</p>
  
  <p><strong>What am I missing / doing wrong ?</strong></p>
  
  <p><strong>Thanks a lot for reading !</strong></p>