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 4 of 4

Thread: Exporting Runnable JAR File

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Exporting Runnable JAR File

    Running JDK 1.6.0.29

    Hello fellow JD's. I've bee programming in Java for quite some time now but have never created a Runnable JAR file (never had to). Anyways, I have created a simple GUI. It runs as expect in Eclipse, but when I Export to Runnable JAR file, my jpg file and wav file are not included within the JAR file and not sure why. They are both stored within my Java Project. Here's the code to my simple program, if needed :

    [
    package BSPackage;
     
    import java.applet.Applet;
    import java.applet.AudioClip;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
     
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
     
     
    public class BullshitButton extends JFrame implements ActionListener
    {
    	//Create AudioClip object to play bullshit remarks!
    	private AudioClip myAudioPlayer;
    	private JPanel bullshitPanel;
    	private JFrame bullshitFrame;
    	private JButton bullshitButton;
    	private JLabel bullshitButtonPic;
    	private String fileName;
     
    	/**
    	 * @param args
    	 */
     
    	public static void main(String[] args) 
    	{		
    		BullshitButton myBullshitButtonApp = new BullshitButton();
    	}
     
    	public BullshitButton()
     
    	{
    		//Create JPanel
    		bullshitPanel = new JPanel();
     
    		//Create JFrame
    		bullshitFrame = new JFrame("Bullshit Button");
     
    		//Create JButton
    		bullshitButton = new JButton("Click for Bullshit!");
     
    		//Create Image object
    		bullshitButtonPic = new JLabel(new ImageIcon("bullshit button.jpg"));
     
    		//add JPanel to Frame
    		bullshitFrame.add(bullshitPanel);
     
    		//add bullshit button pic to background
    		bullshitPanel.add(bullshitButtonPic);
     
    		//add ActionListener to button and then add to JPanel
    		bullshitButton.addActionListener(this);
    		bullshitPanel.add(bullshitButton);
     
    		//Close running process by clicking the 'X' in the upper left corner
    		bullshitFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		//Set JFrame size
    		bullshitFrame.setSize(265, 350);
     
    		bullshitFrame.setVisible(true);
     
    	}
     
    	@Override
    	public void actionPerformed(ActionEvent arg0) 
    	{
    		playSoundFile();
    	}
     
     
    	public void playSoundFile()
    	{
    		int determineSoundClip = 1;
    		switch(determineSoundClip)
    		{
    			case 1:	fileName = "This is Absolute Bullshit.wav";
    					break;
    			case 2: fileName = "";
    					break;
    			case 3: fileName = "";
    					break;
    			case 4: fileName = "";
    					break;
    			case 5: fileName = "";
    					break;
    			default: determineSoundClip = 1;
    				     break;
    		}
    		determineSoundClip++;
     
    		try
    		{
    			File file = new File(fileName);
    			if(file.exists())
    			{
    				myAudioPlayer = Applet.newAudioClip(new File(fileName).toURL());
     
    			}
    			else
    				throw new RuntimeException("Sound: " + fileName + " not found");
    		}
    		catch(Exception e)
    		{
    			throw new RuntimeException("Sound: Bad Url: " + e);
    		}
    		myAudioPlayer.play();
    	}
     
    }

    Here's what my GUI looks like when executed in Eclipse:
    bullshitGUI.JPG

    Then when executed with the exported Runnable Jar file, the bullshit button picture is gone and of course by clicking the button, no sound is being played.

    Any thoughts or suggestions?


  2. #2
    Junior Member
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exporting Runnable JAR File

    After a couple of hours, I fixed my problem. I didn't specify the exact filepath to my jpg and wav files on my HDD. It can't reference the file path to my Project Explorer.

    In other words this should be changed:

    //Create Image object
    bullshitButtonPic = new JLabel(new ImageIcon("bullshit button.jpg"));

    Changed to:
    //Create Image object
    bullshitButtonPic = new JLabel(new ImageIcon("C:/../bullshit button.jpg"));

    If others wanted to run this, they'd need my wav/jpg file stored on their machine on their 'C' drive. I'm not sure how to specify the path for the JAR file. Maybe in the manifest file?

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exporting Runnable JAR File

    I didn't specify the exact filepath to my jpg and wav files on my HDD.
    A better solution to the problem would be to include ALL the files in the jar file and use the classloader's getResource method to get to the "file" in the jar file. That makes the jar portable by itself with NO dependencies on external files.
    If others wanted to run this, they'd need
    This problem goes away by using resources instead of HDD files.

  4. The Following User Says Thank You to Norm For This Useful Post:

    KevinWorkman (January 26th, 2012)

  5. #4
    Junior Member
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exporting Runnable JAR File

    Very intuitive. Thanks Norm.

Similar Threads

  1. creating runnable JAR file
    By olimpicco in forum Java IDEs
    Replies: 25
    Last Post: January 11th, 2012, 09:15 AM
  2. Exporting to an Excutable Jar
    By KeyLay in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 28th, 2011, 07:21 PM
  3. Exporting Data to Excel Spreadsheet
    By PineAppleKing in forum Java Theory & Questions
    Replies: 5
    Last Post: May 19th, 2011, 03:03 PM
  4. Replies: 1
    Last Post: April 21st, 2011, 09:59 AM
  5. Image location on a Runnable JAR file?
    By DarrenReeder in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2010, 07:59 AM