1

How would I be able to run music in a runnable jar file? My programs runs the music when I compile and run the code. But when I try making it into a runnable jar file through eclipse, it does not run the music for some reason.

My code

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

import sun.audio.*;
import java.io.*;

public class Birthday {

 static JFrame bDay = new JFrame();
 static JPanel panel = new JPanel(null);

 public static void main(String[] args) {

  fillPanel();

  bDay.add(panel);

  panel.setFocusable(true);

  bDay.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/
  bDay.setSize(325,250);
  bDay.setLocationRelativeTo(null); 
  bDay.setTitle("Happy Birthday"); 
  bDay.setResizable(true); 
  bDay.setVisible(true); 

 }

 public static void fillPanel(){

  Color fav = new Color(50,190, 189);
  fav = generatePastelColor(fav);
  panel.setBackground(fav);

  JLabel hb = new JLabel("HAPPY BIRTHDAY!!");
  hb.setFont(new Font("Verdana", 2,30));
  hb.setBorder(new LineBorder(Color.WHITE));
  panel.add(hb);
  hb.setBounds(8, 50, 295, 50);

  JButton button = new JButton("Click me");
  panel.add(button);
  button.setBounds(50, 150, 200, 50);
  button.addActionListener(new AL());
 }

 public static class AL implements ActionListener{
  public final void actionPerformed(ActionEvent e){
   music();
  }
 }

 public static Color generatePastelColor(Color mix) {
  int red = 50;
  int green = 190;
  int blue = 189;

  // mix the color
  if (mix != null) {
   red = (red + mix.getRed()) / 2;
   green = (green + mix.getGreen()) / 2;
   blue = (blue + mix.getBlue()) / 2;
  }
  Color color = new Color(red, green, blue);
  return color;
 }

 public static void music(){
  AudioPlayer mgp = AudioPlayer.player;
  AudioStream bgm;
  AudioData md;
  ContinuousAudioDataStream loop = null;

  try {
   bgm = new AudioStream(new FileInputStream(new File("music\\Birthday.wav")));
   md = bgm.getData();
   loop = new ContinuousAudioDataStream(md);
  } catch (IOException e) {
   e.printStackTrace();
  }
  mgp.start(loop);
 }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
ThisIsMe
  • 15
  • 3
  • maybe it doesn't find the audio file. – Stultuske Jun 22 '14 at 06:56
  • 1) By the time of deployment, those resources will likely become an [tag:embedded-resource]. That being the case, the resource must be accessed by `URL` instead of `File`. See the [info page](http://stackoverflow.com/tags/embedded-resource/info) for the tag, for a way to form an `URL`. ... – Andrew Thompson Jun 22 '14 at 07:10
  • ... 2) Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead [use layout managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html), or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Jun 22 '14 at 07:11

1 Answers1

3

File inside Jar file is no longer a File, so change it to read it as resource

bgm = new AudioStream(new FileInputStream(new File("music\\Birthday.wav")));

to

bgm = new AudioStream(Birthday.class.getResourceAsStream ("/music/Birthday.wav"));

assuming you have music/Birthday.wav at the root of classpath

jmj
  • 237,923
  • 42
  • 401
  • 438
  • So when I changed the code, Eclipse showed the error The constructor FileInputStream(InputStream) is undefined – ThisIsMe Jun 22 '14 at 17:08