I am having some serious troubles with my Jar files not running correctly. My project works just fine in Eclipse, but when exported it just doesn't work at all. I am having a similar problem to this: Runnable JARs missing Images/Files (Resources) and almost the exact problem to this Runnable exported JAR not running as it should when out of Eclipse
I used some of the methods here but none of them worked, the only one that did was one telling me to run the jar file in the Command Prompt to see if there are errors. And I think I might have narrowed it down the my resources folder. It isn't in the jar itself, it contents are instead. Is there any way to fix this? Because this may be the problem. Another thing is, this program is a game and the way I have the maps designed they have to load from "res/maps/map.txt". For some reason, not explained to me while learning how to do this, "res/" needs to be before maps, but when I load textures its a different story, so maybe my source of knowledge isn't so smart. For reference here is the code for loading maps and images.
Images (Works fine according to Cmd Prompt):
package com.mthomps.engine.gfx;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageLoader {
public static BufferedImage loadImg(String path){
try {
return ImageIO.read(ImageLoader.class.getResource(path));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(1);
}
return null;
}
}
Utils (Reads txt file for the map):
package com.mthomps.engine.utils;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Utils {
public static String loadFileAsString(String path){
StringBuilder builder = new StringBuilder();
try{
BufferedReader br = new BufferedReader(new FileReader(path));
String line;
while((line = br.readLine()) != null)
builder.append(line + "\n");
br.close();
}catch(IOException e){
e.printStackTrace();
}
return builder.toString();
}
public static int parseInt(String number){
try{
return Integer.parseInt(number);
}catch(NumberFormatException e){
e.printStackTrace();
return 0;
}
}
}
Map:
package com.mthomps.engine.main.map;
import java.awt.Graphics;
import com.mthomps.game.tiles.Tile;
import com.mthomps.engine.main.Handler;
import com.mthomps.enigne.utils.Utils;
public class Map {
private Handler handler;
private int width, height;
private int spawnX, spawnY;
private int[][] tiles;
public Map(Handler handler, String path){
this.handler = handler;
loadMap(path);
}
public void tick(){
}
public void render(Graphics g){
int xStart = (int) Math.max(0, handler.getGameCamera().getxOffset() / Tile.TILEWIDTH);
int xEnd = (int) Math.min(width, (handler.getGameCamera().getxOffset() + handler.getWidth()) / Tile.TILEWIDTH + 1);
int yStart = (int) Math.max(0, handler.getGameCamera().getyOffset() / Tile.TILEHEIGHT);
int yEnd = (int) Math.min(height, (handler.getGameCamera().getyOffset() + handler.getHeight()) / Tile.TILEHEIGHT + 1);
for(int y = yStart; y < yEnd; y++){
for(int x = xStart; x < xEnd; x++){
getTile(x, y).render(g, (int) (x * Tile.TILEWIDTH - handler.getGameCamera().getxOffset()), (int) (y * Tile.TILEHEIGHT - handler.getGameCamera().getyOffset()));
}
}
}
public Tile getTile(int x, int y){
if(x < 0 || y < 0 || x >= width || y >= height)
return Tile.devWhite;
Tile t = Tile.tiles[tiles[x][y]];
if(t == null)
return Tile.devWhite;
return t;
}
private void loadMap(String path){
String file = Utils.loadFileAsString(path);
String[] tokens = file.split("\\s+");
width = Utils.parseInt(tokens[0]);
height = Utils.parseInt(tokens[1]);
spawnX = Utils.parseInt(tokens[2]);
spawnY = Utils.parseInt(tokens[3]);
tiles = new int[width][height];
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
tiles[x][y] = Utils.parseInt(tokens[(x + y * width) + 4]);
}
}
}
}
GameState (where map is loaded. requires "res/" for some odd reason):
package com.mthomps.game.states;
import java.awt.Color;
import java.awt.Graphics;
import com.mthomps.game.entity.mob.Player;
import com.mthomps.engine.main.Handler;
import com.mthomps.engine.main.map.Map;
public class GameState extends State{
private Player player;
private Map maze;
public GameState(Handler handler){
super(handler);
player = new Player(handler, 256, 256);
maze = new Map(handler, "res/maps/maze/maze.txt");
handler.setMap(maze);
}
@Override
public void tick() {
maze.tick();
player.tick();
}
@Override
public void render(Graphics g) {
maze.render(g);
player.render(g);
}
}
Like I said, that code there is just for reference, I feel like I need the "res" folder in my jar, not just it's contents. If there is any way i could do this, or any errors in the code, please tell me. Command Prompt didn't say anything was wrong with my ImageLoader class, but maybe there is a detail I missed. Nothing is shown when I run the program, just the window.