Results 1 to 17 of 17
Like Tree2Likes
  • 2 Post By Tolls

Thread: Including Recources into a runnable JAR using eclipse

  1. #1
    Lucas_F98 is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Question 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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,778
    Blog Entries
    7
    Rep Power
    21

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Lucas_F98 is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: Including Recources into a runnable JAR using eclipse

    Quote Originally Posted by JosAH View Post
    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.
    Whilst that might work a batch file is needed to launch it. What i am looking for is a way to make a runnable JAR File. ie. double click to run.

    Thanks,
    Lucas

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default 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.

  5. #5
    Lucas_F98 is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: Including Recources into a runnable JAR using eclipse

    Quote Originally Posted by Tolls View Post
    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.
    CODE:

    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();
                }
        }
    }
    i want to export this as a Runnable JAR File so how would i include the "getResourceAsStream" thingo you said.

    Sorry for me not knowing much,

    Lucas
    Last edited by Lucas_F98; 01-11-2012 at 12:57 PM.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,778
    Blog Entries
    7
    Rep Power
    21

    Default Re: Including Recources into a runnable JAR using eclipse

    Quote Originally Posted by Lucas_F98 View Post
    Whilst that might work a batch file is needed to launch it. What i am looking for is a way to make a runnable JAR File. ie. double click to run.
    That is one of the following steps of the wizard: it asks you to specify a file to be used as the 'manifest' entry; that entry specifies the Main-Class in your .jar file. Read the relevant documentation for details.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Lucas_F98 is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: Including Recources into a runnable JAR using eclipse

    Quote Originally Posted by JosAH View Post
    That is one of the following steps of the wizard: it asks you to specify a file to be used as the 'manifest' entry; that entry specifies the Main-Class in your .jar file. Read the relevant documentation for details.

    kind regards,

    Jos
    Cool That works with a double click, but the audio does not play. Tolls Said i will need "getResourceAsStream" how would i do that

    cheers,
    Lucas

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Including Recources into a runnable JAR using eclipse

    Please use code tags when posting code:
    Java Code:
    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("src/SEWS.wav").getAbsoluteFile());
    That is getting a file from the file system, which will not work with a resource in the jar.
    There is a method called getResourceAsStream() that handles that problem:
    Java Code:
    InputStream is = <whatever class you're in>.class.getResourceAsStream("<path>");
    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.

  9. #9
    Lucas_F98 is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: Including Recources into a runnable JAR using eclipse

    So do i put

    Java Code:
    InputStream is = <whatever class you're in>.class.getResourceAsStream("<path>");
    inside

    Java Code:
    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("src/SEWS.wav").getAbsoluteFile());
    like:

    Java Code:
    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("InputStream is = <whatever class you're in>.class.getResourceAsStream("<path>")").getAbsoluteFile());
    or
    Java Code:
    InputStream is = <whatever class you're in>.class.getResourceAsStream("<path>");
    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("is").getAbsoluteFile());
    (would "is" need quotes?)

    Cheers

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Including Recources into a runnable JAR using eclipse

    It's an InputStream.
    getAudioInputStream() takes an InputStream.
    Put it straight into there.

  11. #11
    Lucas_F98 is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default 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();
    		    }
    	}
    }

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Including Recources into a runnable JAR using eclipse

    Look at the API for (one version of) the method you are using.

  13. #13
    Lucas_F98 is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default 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
    Java Code:
     AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(is).getAbsoluteFile());
    Cheers, much appreciated,

    Lucas

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default 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.
    Fubarable and Lucas_F98 like this.

  15. #15
    Lucas_F98 is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default 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

    Java 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();
    		    }
    	}
    
    }
    It says that "this" can not be used in a static context so i was wondering what i should replace it with

    my wav file is "SEWS.wav" in the pic Including Recources into a runnable JAR using eclipse-capture.png

    so what would i use to reference to it would it be "/src/SEWS.wav" or what, i have no idea

    Cheers, and Sorry,
    Lucas
    Last edited by Lucas_F98; 01-13-2012 at 06:17 AM. Reason: missing information

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default 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.

  17. #17
    Lucas_F98 is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: Including Recources into a runnable JAR using eclipse

    Cheers,

    All fixed, Thanks Tolls And JosAH

    Lucas

Similar Threads

  1. Including image files in runnable jar
    By bleah in forum Eclipse
    Replies: 5
    Last Post: 06-18-2011, 02:54 PM
  2. Including Perl in java
    By swati.jyoti in forum New To Java
    Replies: 1
    Last Post: 05-13-2010, 05:31 PM
  3. Including external file in JSP
    By ulix83 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-10-2010, 11:31 AM
  4. Sum of all even numbers up to but not including 100
    By bigpips305 in forum New To Java
    Replies: 41
    Last Post: 02-09-2010, 04:16 PM
  5. Including JAR in applications
    By bugger in forum New To Java
    Replies: 0
    Last Post: 01-11-2008, 10:36 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •