0

I programmed an app that displays images and plays sounds. That's why it needs to load images and sounds from a folder called "recources" that is located in the src folder of the project.

For the images I'm using the following code to load the png and then display it on my frame:

ImageIcon img = new ImageIcon(getClass().getResource("/recources/img.png"));

That works fine for running the app inside Netbeans and also after I built it into a jar.

The sounds are being loaded like that:

public Sound(){

    try
    {

        File audiodata = new File("/recources/audio.wav");
        if(audiodata.exists()){
            audioInput = AudioSystem.getAudioInputStream(audiodata);

            clip= AudioSystem.getClip();
            clip.open(audioInput);
            clip.start();

        }
    }
    catch(Exception e){
        e.printStackTrace();
    }
}

That also works inside Netbeans but does not in the finished jar. --> NullPointerException

What do I need to change in my code?

What also doesnt work:

I also want to load a sqllite database with a jdbc driver with this code:

public void getConnection() {
    try {
        Class.forName("org.sqlite.JDBC");
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Lagerverwaltung.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        Connection con = DriverManager.getConnection("jdbc:sqlite:recources/database.db");
    } catch (SQLException ex) {
        Logger.getLogger(Lagerverwaltung.class.getName()).log(Level.SEVERE, null, ex);
    }

}

It has the same issue, when I run it in Netbeans it finds the database but in the built jar its not found.

I added the resources folder as a source to the properties of my project.

1 Answers1

0

Try changing the sound class to:

public Sound(){

    try
    {

        File audiodata = new File(this.getClass().getResourceAsStream("/recources/audio.wav").toString());
        if(audiodatei.exists()){
            audioInput = AudioSystem.getAudioInputStream(audiodata);

            clip= AudioSystem.getClip();
            clip.open(audioInput);
            clip.start();

        }
    }
    catch(Exception e){
        e.printStackTrace();
    }
}

and getConnnection to:

public void getConnection() {
    try {
        Class.forName("org.sqlite.JDBC");
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Lagerverwaltung.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        Connection con = DriverManager.getConnection("jdbc:sqlite:"+this.getClass().getResourceAsStream("/recources/database.db").toString());
    } catch (SQLException ex) {
        Logger.getLogger(Lagerverwaltung.class.getName()).log(Level.SEVERE, null, ex);
    }

}

Just changed how the file is being accessed. Hope it works. Do tell the result.

ProGamer
  • 420
  • 5
  • 16
  • "this.getClass().getResourceAsStream("/recources/database.db").toString()" gives an error: "non-static variable this cannot be referenced from a static context". I also added it to the sound class and there it gives a NullPointerException when run. –  Jun 15 '20 at 13:13
  • Wait it gives the NullPointer when I run clip.stop() in the sound class and not while loading or when doing clip.start(). If I change the url to a wrong url it gives it while loading the file, so the loading functions but the clip is not working anymore. –  Jun 15 '20 at 13:25
  • UPDATE: I got the sound to work with this solution: [Solution](https://stackoverflow.com/questions/5529754/java-io-ioexception-mark-reset-not-supported) –  Jun 15 '20 at 14:06
  • 1
    Ok Nice you got the solution :) – ProGamer Jun 15 '20 at 14:24