[英]Java program runs fine in Eclipse but not as a .jar file (Is the config path wrong??)
[英]Java program runs fine in Eclipse but not as a .jar file
我的.jar文件有問題。 它在Eclipse中運行良好,但是一旦導出,它就不會打開。 我檢查了清單文件,看起來還可以。
我嘗試將其導出為可運行的jar,以及僅使用jar構建器。 沒事。
我試圖在命令提示符下運行它,並說它無法訪問jar文件...我在這里搜索了一段時間,但尚未找到答案。
我不確定自己在做什么錯。 我唯一能想到的是我沒有正確獲取圖像。
我正在為程序的精靈使用.png文件,這是如何為程序獲取它們的示例。
這段代碼從.png文件開始構建關卡。
public class SpawnLevel extends Level{
public SpawnLevel(String path) {
super(path);
}
protected void loadLevel(String path){
try{
System.out.println("classpath is: " + System.getProperty("java.class.path"));
BufferedImage image = ImageIO.read(SpawnLevel.class.getResource(path));
int w = width = image.getWidth();
int h = height= image.getHeight();
tiles = new int[w*h];
image.getRGB(0,0,w,h,tiles,0,w);
}catch(IOException e){
e.printStackTrace();
System.out.println("EXEPTION FOUND!!! Could not load the level file!");
}
}
protected void generateLevel(){
System.out.println("Tiles: " + tiles[0]);
}
}
我之前為另一個程序制作了另一個.jar,但沒有問題。 任何幫助將不勝感激。
如果有幫助,我將使用此代碼顯示資源文件夾路徑信息。
System.out.println("classpath is: " + System.getProperty("java.class.path"));
這是資源文件夾的當前路徑。 在從Eclipse導出之前。
classpath is: C:\Users\name\workspace\Rpg_Game\bin;C:\Users\name\workspace\Rpg_Game\res
我導出到.jar后
classpath is: 2ndGameTest.jar
如果圖像位於src
的resources
包中,則應為getResource()
使用的路徑類似於
class.getResource("/resources/levels/level1.png")
用測試程序更新
import java.awt.Image;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class TestImage {
public static void main(String[] args) throws IOException {
Image image = ImageIO.read(TestImage.class.getResource("/resources/images/image.png"));
ImageIcon icon = new ImageIcon(image);
JLabel label = new JLabel(icon);
JOptionPane.showMessageDialog(null, label);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.