0

I am trying to make a basic music player using things that I have recently learned, (I am new to java). Whenever I run my code in Eclipse, it works perfectly fine. All the buttons have their icons, and everything works perfectly.

However, when I export the project into an executable jar file, it seems as if the file cannot access the icons.

I'm not using a direct path, I'm using "src/musicPlayer/restartButton.png"

I'm using IconImages to create the icons that the JButtons use, and if you want to criticize me for using Swing, it's the only thing I know how to create a GUI with, so please don't. If you need to see the code, it's right here:

package musicPlayer;

import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class MusicPlayer extends JFrame {

    String path = "";
    String song = "";
    boolean pathEntered = false;
    boolean isPlaying = false;
    boolean isLooping = false;
    Clip music = AudioSystem.getClip();
    JFrame player = new JFrame();
    JButton playOrPauseButton = new JButton();
    JButton rewindButton = new JButton();
    JButton fastForwardButton = new JButton();
    JButton enterSongButton = new JButton();
    JButton restartButton = new JButton();
    JButton loopButton = new JButton();
    JLabel title = new JLabel();
    JLabel songPlaying = new JLabel();
    JLabel looping = new JLabel();
    JTextField enterSong = new JTextField();
    ImageIcon playPauseIcon = new ImageIcon("src/musicPlayer/playPauseButton.png");
    ImageIcon rewindIcon = new ImageIcon("src/musicPlayer/rewindButton.png");
    ImageIcon fastForwardIcon = new ImageIcon("src/musicPlayer/fastForwardButton.png");
    ImageIcon restartIcon = new ImageIcon("src/musicPlayer/restartButton.png");
    ImageIcon loopIcon = new ImageIcon("src/musicPlayer/loopButton.png");
    ImageIcon playerIcon = new ImageIcon("src/musicPlayer/musicPlayerIcon.png");
    
    public MusicPlayer () throws LineUnavailableException, IOException, UnsupportedAudioFileException {
        
        
        player.setSize(500, 500);
        player.setBackground(new Color(10, 60, 100));
        player.setVisible(true);
        player.setTitle("Music Player");
        player.setLayout(null);
        player.setResizable(false);
        player.setIconImage(playerIcon.getImage());
        player.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        
        title.setText("Music Player");
        title.setBounds(185, 0, 150, 150);
        title.setFont(new Font("Serif", Font.BOLD, 20));
        title.setVisible(true);
        
        
        playOrPauseButton.setIcon(playPauseIcon);
        playOrPauseButton.setBounds(200, 200, 75, 75);
        playOrPauseButton.addActionListener(e -> playPause());
        playOrPauseButton.setBackground(Color.LIGHT_GRAY);
        
        
        rewindButton.setIcon(rewindIcon);
        rewindButton.setBounds(110, 200, 75, 75);
        rewindButton.addActionListener(e -> rewind());
        rewindButton.setBackground(Color.LIGHT_GRAY);
        
        
        fastForwardButton.setIcon(fastForwardIcon);
        fastForwardButton.setBounds(290, 200, 75, 75);
        fastForwardButton.addActionListener(e -> fastForward());
        fastForwardButton.setBackground(Color.LIGHT_GRAY);
        
        
        restartButton.setIcon(restartIcon);
        restartButton.setBounds(20, 200, 75, 75);
        restartButton.addActionListener(e -> restart());
        restartButton.setBackground(Color.LIGHT_GRAY);
        
        
        loopButton.setIcon(loopIcon);
        loopButton.setBounds(380, 200, 75, 75);
        loopButton.addActionListener(e -> loop());
        loopButton.setBackground(Color.LIGHT_GRAY);
        
        
        enterSongButton.setText("Enter Path");
        enterSongButton.setBounds(190, 130, 100, 30);
        enterSongButton.addActionListener(e -> {
            try {
                enterSong();
            } catch (UnsupportedAudioFileException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            } catch (LineUnavailableException e1) {
                e1.printStackTrace();
            }
        });
        enterSongButton.setBackground(Color.LIGHT_GRAY);
        
        
        enterSong.setBounds(200, 100, 80, 20);
        enterSong.setToolTipText("Enter path here");
        enterSong.setVisible(true);
        
        
        looping.setBounds(204, 430, 80, 20);
        looping.setText("Looping off");
        looping.setVisible(true);
        
        
        player.add(playOrPauseButton);
        player.add(rewindButton);
        player.add(fastForwardButton);
        player.add(restartButton);
        player.add(loopButton);
        player.add(enterSongButton);
        player.add(title);
        player.add(enterSong);
        player.add(songPlaying);
        player.add(looping);
    }
    
    
    void playPause () {
        if (isPlaying) {
            music.stop();
            isPlaying = false;
        } else {
            music.start();
            isPlaying = true;
            songPlaying.setVisible(true);
        }
    }
    
    
    void rewind () {
        
        music.setMicrosecondPosition(music.getMicrosecondPosition() - 5000000);
        
    }
    
    
    void fastForward () {
        
        music.setMicrosecondPosition(music.getMicrosecondPosition() + 5000000);
        
    }
    
    
    void enterSong () throws UnsupportedAudioFileException, IOException, LineUnavailableException {
        
        if (!pathEntered) {
            path = enterSong.getText();
            enterSong.setToolTipText("Enter song here");
            enterSongButton.setText("Enter Song");
            enterSong.setText("");
            pathEntered = true;
        } else {
            song = enterSong.getText();
            enterSong.setText("");
            String pathAndSong = path + "\\" + song;
            File file = new File(pathAndSong);
            AudioInputStream audioStream = AudioSystem.getAudioInputStream(file);
            music.open(audioStream);
            
            songPlaying.setText("Now playing: " + song);
            songPlaying.setBounds(224 - song.length() * 2, 0, 400, 20);
            songPlaying.setFont(new Font("Serif", Font.BOLD, 10));
        }
        
    }
    
    
    void restart () {
        
        music.setMicrosecondPosition(0);
        
    }
    
    void loop () {
        
        if (!isLooping) {
            music.loop(2000000000);
            isLooping = true;
            looping.setText("Looping on");
        } else {
            music.loop(0);
            isLooping = false;
            looping.setText("Looping off");
        }
        
    }
}
  • How does it fail? Generally, there will be an exception, the message of which will tell you what the problem is. – passer-by Jan 29 '22 at 17:35
  • Does this answer your question ? https://stackoverflow.com/questions/941754/how-to-get-a-path-to-a-resource-in-a-java-jar-file – Shanavas M Jan 29 '22 at 17:36
  • Does this answer your question? [How to get a path to a resource in a Java JAR file](https://stackoverflow.com/questions/941754/how-to-get-a-path-to-a-resource-in-a-java-jar-file) – Shanavas M Jan 29 '22 at 17:37
  • 2
    Print out the contents of the JAR file that Eclipse created for you. Refer to the [jar](https://docs.oracle.com/en/java/javase/17/docs/specs/man/jar.html) command. You will see that the path to file `restartButton.png` is **not** `src/musicPlayer`. – Abra Jan 29 '22 at 18:42
  • @passer-by There is no exception. Do I need to have extra code to detect the exception, or will it just fail? – BattleGuy03 Jan 29 '22 at 21:01
  • A .jar file is a zip file. You cannot use File to refer to its contents. And a relative file name wouldn’t work anyway, since the [working directory](https://en.wikipedia.org/wiki/Working_directory) of the Java process is in no way guaranteed to be the location of the program. Use [Class.getResource](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Class.html#getResource(java.lang.String)) to read your images. – VGR Jan 30 '22 at 01:34
  • Maybe [How to Use Icons](https://docs.oracle.com/javase/tutorial/uiswing/components/icon.html) will help. – Abra Jan 30 '22 at 05:07

0 Answers0