So I'm trying to use BufferedImages necause their eaier for and it seems loading them is a bit more finiky. I think you need the absolute path to load them and I have tried just doing src/Package.image.png but when I export to runnable jar it doesn't work. This code works inside eclipse but for some reason it doesn't work when I export it as a .jar .
package MainGame;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
public class GraphicsPath {
public static String getGraphicsPath(){
String path = null;
PrintWriter writer = null;
try {
writer = new PrintWriter("text.txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
path = GraphicsPath.class.getProtectionDomain().getCodeSource().getLocation().getPath();
System.out.println(path);
writer.println("orignal path " + path);
//FOR LINUX
String[] splited = path.split("/");
//FOR WINDOWS
//String[] splited = path.split("\");
writer.println(path);
path = path.replace("%20", " ");
path += "MainGame/";
writer.println(path);
writer.close();
System.out.println(path);
return path;
}
}
Here is the contents of text.txt when its exported to the jar
orignal path ./
./
./MainGame/
Here is the contents of text.txt while still in the java project
orignal path /home/mini/workspace/Pokemon%20Game/bin/
/home/mini/workspace/Pokemon%20Game/bin/
/home/mini/workspace/Pokemon Game/bin/MainGame/
If I understand you correctly, you have a package.image.png file that you need to read, it is inside the .jar file.
I have had this problem before, and I think you cannot use path to locate this file. In one of my projects I actually used a URL to specify the location of the file. I'll give you an example of what I did
so In my jar file, there is a images folder. say i have /images/x.png
I do this
I used this to create imageIcon, but I think you can check how to read a file with URL.
EDIT: I just tried this to read a file, it works.
So you can wrap the InputStreamReader with other stuff to get the image