Results 1 to 17 of 17
- 01-11-2012, 09:38 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 9
- Rep Power
- 0
Including Recources into a runnable JAR using eclipse
OK, so i have a .wav audio file and i am using eclipse. I have imported it into the src folder, and am calling on it in the code using "src/blah.wav".
It works when i run it in eclipse but if i export is a s a runnable JAR and try to use it it doesn't.
Is there something i have to do to make it get included in the jar file, i selected "package required libraries into generated JAR" if that helps.
i am new to java so any help would be appreciated
- 01-11-2012, 10:43 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Re: Including Recources into a runnable JAR using eclipse
Select the project you want to export, right mouse click and select Export ... > Jar file; deselect everything in the right pane (you don't want Eclipse's crap in your .jar file) and select your resources in your left pane (including all the packages with the code in it). Follow the other steps in the wizard and you're in business.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-11-2012, 11:06 AM #3
Member
- Join Date
- Jan 2012
- Posts
- 9
- Rep Power
- 0
- 01-11-2012, 11:07 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Including Recources into a runnable JAR using eclipse
Also you'll probably have to change your code as the .wav will no longer be a file on the file system, but a resource in your jar.
SO you'll need getResourceAsStream (or similar) to access it.
- 01-11-2012, 11:12 AM #5
Member
- Join Date
- Jan 2012
- Posts
- 9
- Rep Power
- 0
Re: Including Recources into a runnable JAR using eclipse
CODE:
i want to export this as a Runnable JAR File so how would i include the "getResourceAsStream" thingo you said.Java Code:import java.io.File; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; public class sound { public static void warningsound() { try{ AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("src/SEWS.wav").getAbsoluteFile()); Clip clip = AudioSystem.getClip(); clip.open(audioInputStream); clip.start(); }catch(Exception ex){ ex.printStackTrace(); } } }
Sorry for me not knowing much,
LucasLast edited by Lucas_F98; 01-11-2012 at 11:57 AM.
- 01-11-2012, 11:12 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Re: Including Recources into a runnable JAR using eclipse
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-11-2012, 11:19 AM #7
Member
- Join Date
- Jan 2012
- Posts
- 9
- Rep Power
- 0
- 01-11-2012, 11:54 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Including Recources into a runnable JAR using eclipse
Please use code tags when posting code:
That is getting a file from the file system, which will not work with a resource in the jar.Java Code:AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("src/SEWS.wav").getAbsoluteFile());
There is a method called getResourceAsStream() that handles that problem:
Exactly what path you need you'll have to muck about with, as I can never remember. I think "/SEWS.wav" will look for the file at the top of the jar.Java Code:InputStream is = <whatever class you're in>.class.getResourceAsStream("<path>");
- 01-11-2012, 12:04 PM #9
Member
- Join Date
- Jan 2012
- Posts
- 9
- Rep Power
- 0
Re: Including Recources into a runnable JAR using eclipse
So do i put
insideJava Code:InputStream is = <whatever class you're in>.class.getResourceAsStream("<path>");
like:Java Code:AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("src/SEWS.wav").getAbsoluteFile());
orJava Code:AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("InputStream is = <whatever class you're in>.class.getResourceAsStream("<path>")").getAbsoluteFile());
(would "is" need quotes?)Java Code:InputStream is = <whatever class you're in>.class.getResourceAsStream("<path>"); AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("is").getAbsoluteFile());
Cheers
- 01-11-2012, 01:02 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Including Recources into a runnable JAR using eclipse
It's an InputStream.
getAudioInputStream() takes an InputStream.
Put it straight into there.
- 01-12-2012, 12:24 AM #11
Member
- Join Date
- Jan 2012
- Posts
- 9
- Rep Power
- 0
Re: Including Recources into a runnable JAR using eclipse
OK That just went over my head, here is my code can you fix it up, because i am as confused as a legless spider at the moment.
Thanks,
Lucas
Java Code:package fire; import java.io.File; import java.io.InputStream; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; public class sound { InputStream is = sound.class.getResourceAsStream("/SEWS.wav"); public static void warningsound() { try{ AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(is).getAbsoluteFile()); Clip clip = AudioSystem.getClip(); clip.open(audioInputStream); clip.start(); }catch(Exception ex){ ex.printStackTrace(); } } }
- 01-12-2012, 10:19 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Including Recources into a runnable JAR using eclipse
Look at the API for (one version of) the method you are using.
- 01-12-2012, 10:37 AM #13
Member
- Join Date
- Jan 2012
- Posts
- 9
- Rep Power
- 0
Re: Including Recources into a runnable JAR using eclipse
Thanks Tolls,
I am having a real mental blank at the moment and cant seem to figure it out could you please just tell me what i should replace this line with
Cheers, much appreciated,Java Code:AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(is).getAbsoluteFile());
Lucas
- 01-12-2012, 12:30 PM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Including Recources into a runnable JAR using eclipse
1. Don't PM people with your urgency. Most of us here have jobs to do and only come on when we have a window of opportunity.
2. Learn to read the API. I gave a link to the specific bit that you need to use. If you can't understand it then you probably need to take a step back from messing with audio and learn how methods are called in Java.
- 01-13-2012, 04:56 AM #15
Member
- Join Date
- Jan 2012
- Posts
- 9
- Rep Power
- 0
Re: Including Recources into a runnable JAR using eclipse
Hello Tolls,
I am sincerely sorry about doing that and i will never do it again,
And i did read the API but as i am a beginner i did not understand it, from what i believe from reading the API is that i should use this code
It says that "this" can not be used in a static context so i was wondering what i should replace it withJava Code:package fire; import java.io.InputStream; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; public class sound { public static void warningsound() { try{ InputStream is = this.class.getResourceAsStream("SEWS.wav"); //"this" is in this line AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(is); Clip clip = AudioSystem.getClip(); clip.open(audioInputStream); clip.start(); }catch(Exception ex){ ex.printStackTrace(); } } }
my wav file is "SEWS.wav" in the pic
so what would i use to reference to it would it be "/src/SEWS.wav" or what, i have no idea
Cheers, and Sorry,
LucasLast edited by Lucas_F98; 01-13-2012 at 05:17 AM. Reason: missing information
- 01-13-2012, 10:10 AM #16
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Including Recources into a runnable JAR using eclipse
As per my post: "whatever class you're in".
In your case sound (which should be Sound by the Java naming standards).
"this" references the current object, which does not exist in a static method, hence the error.
- 01-13-2012, 11:16 AM #17
Member
- Join Date
- Jan 2012
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Including image files in runnable jar
By bleah in forum EclipseReplies: 5Last Post: 06-18-2011, 01:54 PM -
Including Perl in java
By swati.jyoti in forum New To JavaReplies: 1Last Post: 05-13-2010, 04:31 PM -
Including external file in JSP
By ulix83 in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 05-10-2010, 10:31 AM -
Sum of all even numbers up to but not including 100
By bigpips305 in forum New To JavaReplies: 41Last Post: 02-09-2010, 03:16 PM -
Including JAR in applications
By bugger in forum New To JavaReplies: 0Last Post: 01-11-2008, 09:36 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks