Não consigo criar jar intellij

16/09/2019

0

Olá, quando tento fazer um jar com os códigos a seguir, e abro o executável, ele lança a seguinte exceção:

java.io.FileNotFoundException: res/world/world1.txt (Arquivo ou diretório inexistente)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
at java.base/java.io.FileInputStream.(FileInputStream.java:157)
at java.base/java.io.FileInputStream.(FileInputStream.java:112)
at java.base/java.io.FileReader.(FileReader.java:60)
at br.gabefgonc.javaxswingtests.utils.Utils.loadFileAsString(Utils.java:13)
at br.gabefgonc.javaxswingtests.worlds.World.loadWorld(World.java:68)
at br.gabefgonc.javaxswingtests.worlds.World.(World.java:25)
at br.gabefgonc.javaxswingtests.main.states.GameState.(GameState.java:20)
at br.gabefgonc.javaxswingtests.main.Game.init(Game.java:95)
at br.gabefgonc.javaxswingtests.main.Game.run(Game.java:45)
at java.base/java.lang.Thread.run(Thread.java:834)

java.lang.NumberFormatException: For input string: “”
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:662)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at br.gabefgonc.javaxswingtests.utils.Utils.parseInt(Utils.java:26)
at br.gabefgonc.javaxswingtests.worlds.World.loadWorld(World.java:71)
at br.gabefgonc.javaxswingtests.worlds.World.(World.java:25)
at br.gabefgonc.javaxswingtests.main.states.GameState.(GameState.java:20)
at br.gabefgonc.javaxswingtests.main.Game.init(Game.java:95)
at br.gabefgonc.javaxswingtests.main.Game.run(Game.java:45)
at java.base/java.lang.Thread.run(Thread.java:834)
Exception in thread “Thread-0” java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
at br.gabefgonc.javaxswingtests.worlds.World.loadWorld(World.java:72)
at br.gabefgonc.javaxswingtests.worlds.World.(World.java:25)
at br.gabefgonc.javaxswingtests.main.states.GameState.(GameState.java:20)
at br.gabefgonc.javaxswingtests.main.Game.init(Game.java:95)
at br.gabefgonc.javaxswingtests.main.Game.run(Game.java:45)
at java.base/java.lang.Thread.run(Thread.java:834)

códigos:

World.java:

package br.gabefgonc.javaxswingtests.worlds;

import br.gabefgonc.javaxswingtests.entities.EntityManager;
import br.gabefgonc.javaxswingtests.entities.Player;
import br.gabefgonc.javaxswingtests.entities.Tree;
import br.gabefgonc.javaxswingtests.inventory.Inventory;
import br.gabefgonc.javaxswingtests.items.Item;
import br.gabefgonc.javaxswingtests.items.ItemManager;
import br.gabefgonc.javaxswingtests.main.Game;
import br.gabefgonc.javaxswingtests.main.Handler;
import br.gabefgonc.javaxswingtests.main.tiles.Tile;
import br.gabefgonc.javaxswingtests.utils.Utils;

import java.awt.*;

public class World {
private int width, height, spawnX,spawnY;
private int[][] tiles;
private Handler handler;
private EntityManager entityManager;
private ItemManager itemManager;
public World(Handler handler, String path){
this.handler = handler;
entityManager = new EntityManager(handler, new Player(handler, 0, 0));
loadWorld(path);
entityManager.getPlayer().setX(spawnX);
entityManager.getPlayer().setY(spawnY);

itemManager = new ItemManager(this.handler);



}

public void tick(){
entityManager.tick();
itemManager.tick();
}

public void render(Graphics g){
int xStart = (int) Math.max(0, handler.getCamera().getxOffset() / Tile.TILE_WIDTH);
int xEnd = (int) Math.min(width, (handler.getCamera().getxOffset() + handler.getWidth()) / Tile.TILE_WIDTH + 1);
int yStart = (int) Math.max(0, handler.getCamera().getyOffset() / Tile.TILE_HEIGHT);
int yEnd = (int) Math.min(height, (handler.getCamera().getyOffset() + handler.getHeight()) / Tile.TILE_HEIGHT + 1);
for(int y = yStart; y < yEnd; y++){
for(int x = xStart; x < xEnd; x++){
getTile(x, y).render(g, (int) (x * Tile.TILE_WIDTH - handler.getCamera().getxOffset()),
(int) (y * Tile.TILE_HEIGHT - handler.getCamera().getyOffset()));
}
}
itemManager.render(g);
entityManager.render(g);
}

public Tile getTile(int x, int y){
Tile t;
if(x < width && y < height)
t = Tile.tiles[tiles[x][y]];
else t = null;

if(t == null){
return Tile.grassTile;
}
return t;
}

private void loadWorld(String path){
String file = Utils.loadFileAsString(path);
String[] tokens = file.split("\\\\\\\\s+");
System.out.println(tokens[0]);
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]);
if(Utils.parseInt(tokens[(x + y * width) + 4]) == 5){
entityManager.addEntity(new Tree(handler, x * Tile.TILE_WIDTH, y * Tile.TILE_HEIGHT));
}
}
}

}

public int getWidth() {
return width;
}

public int getHeight() {
return height;
}

public EntityManager getEntityManager(){
return entityManager;
}

public Handler getHandler() {
return handler;
}

public void setHandler(Handler handler) {
this.handler = handler;
}

public ItemManager getItemManager() {
return itemManager;
}

public void setItemManager(ItemManager itemManager) {
this.itemManager = itemManager;
}
}

Utils.java:

package br.gabefgonc.javaxswingtests.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();
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
try {
BufferedReader br = new BufferedReader(new FileReader(path));
String line;
while((line = br.readLine()) != null){
builder.append(line + "\\\\n");
}
}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;
}
}
}

GameState.java:

package br.gabefgonc.javaxswingtests.main.states;

import br.gabefgonc.javaxswingtests.entities.EntityManager;
import br.gabefgonc.javaxswingtests.entities.Player;
import br.gabefgonc.javaxswingtests.entities.Tree;
import br.gabefgonc.javaxswingtests.gfx.ImageLoader;
import br.gabefgonc.javaxswingtests.gfx.SpriteSheet;
import br.gabefgonc.javaxswingtests.main.Game;
import br.gabefgonc.javaxswingtests.main.Handler;
import br.gabefgonc.javaxswingtests.main.tiles.Tile;
import br.gabefgonc.javaxswingtests.worlds.World;

import java.awt.*;

public class GameState extends State {
private World world;

public GameState(Handler handler){
super(handler);
world = new World(handler, "res/world/world1.txt");
handler.setWorld(world);
}

public void tick() {
world.tick();
}

public void render(Graphics g) {
world.render(g);
}
}

classPath:

/home/gabriel/Programacao/Games/JavaxSwingTests/res

Obrigado!
Gabriel

Gabriel

Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar