简体   繁体   English

运行.jar文件时出错

[英]Error in running .jar file

I created a .jar file of my java game. 我创建了Java游戏的.jar文件。 The program ran well with a java compiler, but when I try to run the .jar file, it showed no result. 该程序在Java编译器上运行良好,但是当我尝试运行.jar文件时,没有显示任何结果。 I ran it again via CMD using: 我再次使用CMD运行它:

java -jar PokemonGame.jar`

...it gave me an error: ...给我一个错误:

Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.(init)(ImageIcon.java:167)
at MainFile.(init)(MainFile.java:25)
at MainFile.main(MainFile.java:86)

I traced the line numbers which I commented: 我跟踪了评论的行号:

public class MainFile extends JPanel implements MouseListener{
    MainFile m;
    static JFrame mainWindow = new JFrame("POKEMON MEMORY GAME");
    static TimerFile timerPanel;
    static GridFile gridPanel;
    static LogsFile logsPanel;
    static ButtonMenuFile buttonMenuPanel;
    JPanel blockPanel;
    URL url;
    BufferedImage winlose;
    JPanel winlosePanel;
    //MainFile line 25
    ImageIcon gameBackground = new ImageIcon(getClass().getResource("Assets\\Pokedex.png"));
    Image gameImage = gameBackground.getImage();
    GameSoundFile gameSound = new GameSoundFile();
    GameSoundFile screenSound = new GameSoundFile();

    //...some codes here

    public static void main(String[] args) {    
        //MainFile line 86
        MainFile mainPanel = new MainFile();
        mainPanel.setMainPanel(mainPanel);          

        mainWindow.add(mainPanel);
        mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        mainWindow.pack();
        mainWindow.setLocationRelativeTo(null);
        mainWindow.setResizable(false);
        mainWindow.setVisible(true);

    }

    public MainFile(){
        //...MainFile codes here
    }
}

Can anyone tell me the flaw here because it seems like a weird behavior that this program was able to run with a java compiler but not on the .jar execuatble file. 谁能在这里告诉我这个缺陷,因为该程序能够使用Java编译器运行但在.jar execuatble文件上却无法运行,这似乎是一种奇怪的行为。

Pack Pokedex.png into jar at the top level: Pokedex.png到顶层的jar中:

jar uf PokemonGame.jar Pokedex.png

and reference it like that: 并像这样引用它:

getClass().getResource("/Pokedex.png")

Within JAR files, the windows directory separator character doesn't exist. 在JAR文件中,Windows目录分隔符不存在。 Use / to separate your directories, and assuming the file is there, it will be found. 使用/分隔您的目录,并假设文件在那里,将找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM