Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Compiled .jar won't run when using .wav-files.

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Compiled .jar won't run when using .wav-files.

    I have created a simple GUI that loops a .wav-file when a button is clicked. This works perfectly when run in Eclipse. However, when compiling it in a runnable .jar, it does not run. No errors thrown, it just doesn't run. Here is the full code:

    import java.awt.EventQueue;
     
    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.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.io.File;
    import java.io.IOException;
     
     
    public class Lydtest extends JFrame {
     
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	private JPanel contentPane;
     
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					Lydtest frame = new Lydtest();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
    	/**
    	 * Create the frame.
    	 * @throws IOException 
    	 * @throws UnsupportedAudioFileException 
    	 * @throws LineUnavailableException 
    	 */
    	public Lydtest() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 114, 72);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
     
    		File lydklipp = new File("pessent.wav");
    		AudioInputStream audioIn = AudioSystem.getAudioInputStream(lydklipp);
    		final Clip clip = AudioSystem.getClip();
    		clip.open(audioIn);
     
     
    		JButton btnTestLyd = new JButton("Pezzent!");
    		btnTestLyd.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				clip.loop(getDefaultCloseOperation());
    			}
    		});
    		btnTestLyd.setBounds(10, 11, 91, 23);
    		contentPane.add(btnTestLyd);
    	}
    }


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Compiled .jar won't run when using .wav-files.


  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Compiled .jar won't run when using .wav-files.

    Thank you! Would you care to be more specific, though? Am I simply supposed to use a URL-request instead? I am new to mixing sound and images with GUI's, so I'd appreciate a step by step guide as to how to do this. I do not understand how I am supposed to put the .wav-file inside the .jar.

  4. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Compiled .jar won't run when using .wav-files.

    Export it together with your class files and use classGetResourceAsStream to obtain an InputStream. This InputStream is the parameter for the AudioSystem.getAudioInputStream()

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Compiled .jar won't run when using .wav-files.

    Alright, so here is what I've changed:

    InputStream lydklipp = 
    		        this.getClass().getClassLoader().getResourceAsStream("pessent.wav");
    		AudioInputStream audioIn = AudioSystem.getAudioInputStream(lydklipp);
    		final Clip clip = AudioSystem.getClip();
    		clip.open(audioIn);

    Still works perfectly in Eclipse, but the .jar won't run.

  6. #6
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Compiled .jar won't run when using .wav-files.

    After doing some research, I see that many people have overcome this problem by creating a seperate folder which they call something like "res" or "resources", make it a sourcefolder, and create another folder named "sound" and add their files there. I do not seem to get this to work.

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Compiled .jar won't run when using .wav-files.

    Quote Originally Posted by EatMyBible View Post
    it just doesn't run.
    I do not seem to get this to work
    Add some printlns to the code to see how far the code does get. If there are no compile errors and no runtime errors, it did something somewhere correctly.
    Figure out where things go wrong, or at least try to localize the problem.
    Then tell us what does it do, where do things seem to go unexpected, and what would you like to happen instead, and ask some question about what you do not understand.

Similar Threads

  1. Program won't run help!
    By iridebmxnj in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 26th, 2012, 09:00 PM
  2. [SOLVED] files size for files inside the JAR file
    By Natherul in forum Java Theory & Questions
    Replies: 3
    Last Post: February 9th, 2012, 03:17 PM
  3. [SOLVED] .jar file won't execute
    By pajfilms in forum What's Wrong With My Code?
    Replies: 27
    Last Post: August 3rd, 2011, 07:43 PM
  4. How to create or edit .ser files in Jar files
    By xbill in forum Java IDEs
    Replies: 1
    Last Post: May 18th, 2011, 05:15 AM
  5. [SOLVED] Compiled .jar won't work (Error-sound)
    By Fermen in forum Object Oriented Programming
    Replies: 2
    Last Post: March 31st, 2011, 05:24 PM