Jump to content

How to make Jar file using sounds

- - - - -

  • Please log in to reply
18 replies to this topic

#1
Serialcek

Serialcek

    Learning Programmer

  • Members
  • PipPipPip
  • 72 posts
Hi everyone.
I want to make a jar file with Eclipse. But I have some problems if I want to use sounds in this my application.

For example this is my method for some sound:



	public void click() {


		InputStream in = null;

		try {

			in = new FileInputStream("won.wav");

		} catch (FileNotFoundException e) {

			e.printStackTrace();

		}


		AudioStream as = null;

		try {

			as = new AudioStream(in);

		} catch (IOException e) {

			e.printStackTrace();

		}


		AudioPlayer.player.start(as);


	


I have this file won.wav in the same folder as the Project. If I run it from eclipse everything works. Should I put this file somewhere else if I want to do a jar file. Or am I missing some radio button to check when I'm exporting jar file?

Thx for help :)

#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
I've tried doing this before, except with image files for my software engineering class. It's really a pain in the ar$e, imo.

I had to right click on my project folder then
New>Source Folder>bin
I chose the bin folder and clicked ok.
It should add another package/folder looking image in your folder structure.

Then, I had to manually add the image inside of the bin folder. In my case, I just didn't want to drop them right into the bin folder, so I had another folder inside of bin called images.
Note*** You have to add the files into both folders, bin and images. (In my case)

To load the image, you'll have to use the this.getClass().getResource("filename.wav"); method.

Here is the very simple code I used as an example:

public class JarRunner {

	public static void main(String[] args) {

		// TODO Auto-generated method stub

		JFrame frame = new JFrame();

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		frame.setContentPane( new MyJPanel() );

		frame.pack();

		frame.setVisible(true);

	}

}


import java.awt.Dimension;

import javax.swing.ImageIcon;

import javax.swing.JLabel;

import javax.swing.JPanel;


public class MyJPanel extends JPanel {

	public MyJPanel() {

		ImageIcon imageIcon = new ImageIcon(getClass().getResource("exampleImage.jpg"));

		JLabel label = new JLabel( imageIcon );

		this.add(label);

	}

	

	public Dimension getPreferredSize() {

		return new Dimension( 200, 200 );

	}

}


Here is the screenshot of the folder/file structure and bin folder.

Attached File  jarTest.png   142.11K   70 downloads

Hopefully it works the same for audio files.

#3
Serialcek

Serialcek

    Learning Programmer

  • Members
  • PipPipPip
  • 72 posts
Hey thx but if I understand you and if I have done everything right it still doesn't work :O

Now my code looks like this:


	public void click() {


		  InputStream in = 

			     getClass().getResourceAsStream("won.wav");

		  

		AudioStream as = null;

		try {

			as = new AudioStream(in);

		} catch (IOException e) {

			e.printStackTrace();

		}


		AudioPlayer.player.start(as);


	}



And as you sad I added a source folder bin to the project and it apper in eclipse under src.
And I had alredy the wave files into this folder. So I then created a folder named Sound in bin and put in the wav file too.. BTW I don't understand why it should help if I create another file into the bin file. :P
Anyway if I try to export it as jar file it stil doesn't work :(

#4
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Were you sure to tick "be sure to package required libraries into jar" ?
I tried tons of different ways to try and package my images into the jar.
Were you sure to refresh your folder structure in Eclipse? It didn't work before I refreshed the folder structure.

Also check out a similar problem on another forum:
Eclipse Community Forums

Eclipse seems really picky about these things.

#5
Serialcek

Serialcek

    Learning Programmer

  • Members
  • PipPipPip
  • 72 posts
Hey yes when I refreshed the the file it works.. thx a lot..
But now I have one more problem.. Now I know how to import sound and images.. but i have a problem with fonts :/
I find on the net something but it's not working or I don't know how to do it:


	 String fontFileName = "Chicken Butt.ttf";

		    InputStream is = this.getClass().getResourceAsStream(fontFileName);	    

		    try {

		        Font font = Font.createFont(Font.TRUETYPE_FONT, is);


		       return font.deriveFont(42);   // Here it doesn't return font


		   } catch (IOException ioe);

		   } catch (FontFormatException ffe);

		   }


		

			 jLabel1.setFont(font);  // font is underlined here



#6
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Have you tried getResource() for a font instead of getResourceAsStream()?

#7
Serialcek

Serialcek

    Learning Programmer

  • Members
  • PipPipPip
  • 72 posts
Yes I triesd and it looks like it doesn't work for fonts...
Actually I found a lot on the internet but nothing is working for me. For example I found this :


 File f = new File("your.ttf");

    FileInputStream in = new FileInputStream(f);

    Font dynamicFont = Font.createFont(Font.TRUETYPE_FONT, in);

    Font dynamicFont32Pt = dynamicFont.deriveFont(32f);



This should work but I get an error because I dont have FileNotFoundException IOException or try/catch problem.
But when I start to use this I get even more errors :/
Is there any way to just disable this errors?

#8
Serialcek

Serialcek

    Learning Programmer

  • Members
  • PipPipPip
  • 72 posts
NeverMind.. I found something that works.. but when I export the file as jar it doesn't even start from another PC.


 File f = new File("Chicken Butt.ttf");

		    FileInputStream in = null;

			try {

				in = new FileInputStream(f);

			} catch (FileNotFoundException e1) {

			}

		    Font dynamicFont = null;

			try {

				dynamicFont = Font.createFont(Font.TRUETYPE_FONT, in);

			} catch (FontFormatException e) {

			} catch (IOException e) {

			}

		    Font dynamicFont32Pt = dynamicFont.deriveFont(42f);


And I deleted the font file from the windows/system/font file to make sure that it works like I want.
Anyone know why my jar file doesn't wanna start now from another PC.

#9
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
print file.getRealPath();
Then you know where it gets the font from. And that location propably doesn't exist at another pc.

#10
Serialcek

Serialcek

    Learning Programmer

  • Members
  • PipPipPip
  • 72 posts
Actually I used f.getAbsoluteFile();
And it returned C:\workspace\matka\Chicken Butt.ttf

And yes you we're right. Does any body know what I should do that it will works fine?

#11
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Well, to get stuff out of a jar
getClass().getResource(fileName) is the way to go.
So the only reason it's not working, is if it's not in there.
Open your jar with a zip program (winrar/7zip/whatever) and see if it's inside it.

#12
Serialcek

Serialcek

    Learning Programmer

  • Members
  • PipPipPip
  • 72 posts
Yes I used getClass().getResource() for get the image and sounds.
But I don't know actually how to use it to get font.
I tried something but didn't work :/




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users