[SOLVED] Game (Executable .jar) won't Load Files

I am creating a desktop 2D Game and I have a problem with the executable .jar . In Eclipse everything works fine but when I generate the jar and execute it it won’t load the files(files are images converted into arrays of bytes).the files are located in a folder named “Graphics” with the code below java finds his path to the .jar file and add “Graphics\” to his URL so it can load the files from the Graphics folder.Graphics folder and the the executable .jar are in the same folder.

public class Imageloader {
    	public BufferedImage loadimg(String graf) throws IOException{
    		URL jarLocation = Imageloader.class.getProtectionDomain().getCodeSource().getLocation();
    		String pathg = jarLocation.toString();
    		System.out.println(pathg);
    		while(pathg.indexOf("/")!=-1){
    			pathg=pathg.replace("/", "\\" );	
    		}
    		pathg=pathg.replace("file:","");
    		pathg=pathg.substring(1, pathg.length());
    		String gr="Graphics\\";
    		System.out.println(pathg+gr+graf);
    		Path path = Paths.get(pathg+gr+graf);
    		byte[] data = Files.readAllBytes(path);
    		InputStream in = new ByteArrayInputStream(data);
    		BufferedImage bImageFromConvert = ImageIO.read(in);
    	    return bImageFromConvert;
}
}

I have replaced “/” with “\” and deleted the String “file:” and the first character of the String pathg witch is “” so path.get() can work fine .as i mentioned In Eclipse everything works perfectly and loads every single file, this is what the console shows :

C:\workspace\SpaceGameTest\bin\Graphics\menu.amg
C:\workspace\SpaceGameTest\bin\Graphics\spacegamespritesheet.amg
C:\workspace\SpaceGameTest\bin\Graphics\playersprites.amg
C:\workspace\SpaceGameTest\bin\Graphics\background.amg
C:\workspace\SpaceGameTest\bin\Graphics\enemy.amg

this is whats in the Main class :

Imageloader imgload =new Imageloader();
        	try{
        		menu=imgload.loadimg("menu.amg");
        		spritesheet=imgload.loadimg("spacegamespritesheet.amg");
        		playerspt=imgload.loadimg("playersprites.amg");
        		Background=imgload.loadimg("background.amg");
        		enemy=imgload.loadimg("enemy.amg");
        		}catch(IOException e) {
                e.printStackTrace();
             }	

.amg is just a random format but its basically file of byte[]
I don’t want to load the files as a resources
EDITED
After compiling the .jar using windows cmd it seems like the application is not getting the path of the .jar file while in Eclipse it is getting the complete path

http://img15.hostingpics.net/pics/778776javahh.jpg

so whats wrong with

URL jarLocation = Imageloader.class.getProtectionDomain().getCodeSource().getLocation();

Hello jonjava,thanks for the links, :slight_smile:

          I dont want to load the images or the files as a resources, i want the [b].amg[/b] files to be on an external folder named "Graphics" with the same folder as my executable jar ,also i dont want to find the files inside the .jar when i open it as an archive, the point of this that i have encrypted my game sprites and stored it on the  [b].amg[/b] file , so when i open the game it loads and decrypt all the graphics from "Graphics" folder ,everything is working fine in Eclipse , when i export it to runnable .jar it loads nothing from "Graphics" folder,

Thanks again and sorry for my bad english.

solved ! ;D ;D ;D ;D ;D

public class Imageloader {
	public BufferedImage loadimg(String graf) throws IOException{
	
	
		final String dir = System.getProperty("user.dir");
        System.out.println("current dir = " + dir);	
		String gr="\\Graphics\\";
		System.out.println(dir+gr+graf);
		Path path = Paths.get(dir+gr+graf);
		byte[] data = Files.readAllBytes(path);
		InputStream in = new ByteArrayInputStream(data);
		BufferedImage bImageFromConvert = ImageIO.read(in);
	return bImageFromConvert;
}

https://dl.dropboxusercontent.com/u/4856429/zips/javaFolderTest.zip

import java.nio.*;
import java.nio.file.*;
import java.nio.charset.*;
import java.io.*;

import javax.swing.*;

class Main extends JTextArea {
  String text = "";

  public Main() {
    this.setRows(6);
  };

  public void run() throws IOException {
    Path path = FileSystems.getDefault().getPath("res", "list.txt");
    BufferedReader br = Files.newBufferedReader(path, StandardCharsets.UTF_8);

    String str;
    while (true) {
      str = br.readLine();
      if (str == null)
        break;
      print(str);
    };
    print("Done!");
  };

  private void print(String str) {
    text += str + "\n";
    this.setText(text);
    System.out.println(str);
  };

  public static void main(String[] args) {
    Main m = new Main();
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(m);
    frame.pack();
    frame.setVisible(true);

    try {
      m.run();
    } catch (Exception e) {
      e.printStackTrace();
    };
  };
}

[EDIT]:

[quote]solved ! Grin Grin Grin Grin Grin
[/quote]