Xiangru Lian
Xiangru Lian

Reputation: 968

Java Program doesn't work after exported from eclipse

When I open the runnable jar file, it still could be opened but it get stuck after half a second like this. (I cannot post an image, so I post the image here: https://pbs.twimg.com/media/B4RN2_MCYAAi1DD.png)

It works well in eclipse. When I run it in CMD, it says:

Exception in thread "game" java.lang.NullPointerException: in
        at javazoom.jl.decoder.Bitstream.<init>(Unknown Source)
        at javazoom.jl.player.Player.<init>(Unknown Source)
        at javazoom.jl.player.Player.<init>(Unknown Source)
        at lian.xiangru.game.AudioHandler.<init>(AudioHandler.java:12)
        at lian.xiangru.game.GameBoard.playSound(GameBoard.java:410)
        at lian.xiangru.game.GameBoard.move(GameBoard.java:224)
        at lian.xiangru.game.GameBoard.moveTiles(GameBoard.java:271)
        at lian.xiangru.game.GameBoard.checkKeys(GameBoard.java:340)
        at lian.xiangru.game.GameBoard.update(GameBoard.java:146)
        at lian.xiangru.game.Game.update(Game.java:42)
        at lian.xiangru.game.Game.run(Game.java:77)
        at java.lang.Thread.run(Unknown Source)

It seems something goes wrong with my resources.

This is my playSound method:

    private void playSound() {
    // how to play mp3 https://www.youtube.com/watch?v=f-7cgX_I220
    AudioHandler sound = new AudioHandler(
            SOUND_LIST[(int) Math.round((Math.log(highestValue) / Math.log(2))) - 1]);
    sound.start();

}

This is my AudioHandler class:

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

class AudioHandler extends Thread {

    private Player playMP3;

    public AudioHandler(String mp3) {
        try {
            playMP3 = new Player(getClass().getResourceAsStream(mp3));
        } catch (JavaLayerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void run() {

        try {
            playMP3.play();
        } catch (JavaLayerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

The lists are:

public static final String[] SOUND_LIST = { "/mayuri.mp3", "/mikoto.mp3",
        "/gai.mp3", "/shougo.mp3", "/gintoki.mp3", "/inori.mp3",
        "/yuzuru.mp3", "/misaki.mp3", "/armin.mp3", "/alphonse.mp3",
        "/alphonse.mp3", "/akane.mp3", "/armin.jpg" };

public static final String[] QUOTE_LIST = { "/mayuri.txt", "/mikoto.txt",
        "/gai.txt", "/shougo.txt", "/gintoki.txt", "/inori.txt",
        "/yuzuru.txt", "/misaki.txt", "/armin.txt", "/alphonse.txt",
        "/alphonse.txt", "/akane.txt", "/armin.txt" };

public static final String[] ICON_LIST = {"/mayuri.jpg", "/mikoto.jpg",
    "/gai.jpg", "/shougo.jpg", "/gintoki.jpg", "/inori.jpg",
    "/yuzuru.jpg", "/misaki.jpg", "/armin.jpg", "/alphonse.jpg",
    "/alphonse.jpg", "/akane.jpg", "/armin.jpg" 
};

Thank you !!

Upvotes: 0

Views: 217

Answers (2)

Xiangru Lian
Xiangru Lian

Reputation: 968

I just found the reason!

There is a file called Mikoto.mp3 in my resources folder and I typed it as mikoto.mp3 in my code. This could be allowed when I use eclipse to run it. But when I run the runnable jar file, it fails because it is case sensitive.

When I change the file name to mikoto.mp3, it works!

Thanks for your answer!

Upvotes: 1

BillRobertson42
BillRobertson42

Reputation: 12883

NullPointerException when loading a resource usually means that the resource could not be found. This behavior is different than the normal file open, no exception is thrown.

Are your audio files included in your jar? If not, make sure that when you export the Jar, you set the right options so they are included. A better alternative might be to introduce a real build process into your application if you need repeatability.

Upvotes: 1

Related Questions