Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Files in exported jar not being accessed?

    Hi all,

    I am making a game with some sound files in included. I have a "res" folder that is included in project path, and it works just fine when I compile and run in eclipse, but when I run my game from the exported jar file it does not have any sound. Any ideas as to why this is not working?

    Thanks!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Files in exported jar not being accessed?

    Are there any error messages when executing from the jar file? Are you sure the files are in the jar and that you have the correct path to them? What code are you using to access the files in the jar? They need to be treated as resources and accessed with thr getResource methods.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: Files in exported jar not being accessed?

    No there are not any error message. Yes, the files are in the jar and yes I have the correct path. And yes I am using getResource methods to get the files.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Files in exported jar not being accessed?

    If there are no errors and everything is located where it should be, why is there a problem?
    How are you executing the jar file so to see the console to know if there are errors?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: Files in exported jar not being accessed?

    I am not seeing the console from the jar. I am only seeing it from eclipse...

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Files in exported jar not being accessed?

    Run the jar from the command line (shell, whatever your system calls it), not by "double clicking" or anything like that.

    But, I can almost guarantee that you are using File references to access those sound files, but inside the jarfile that won't work. Google getResource/getResourceAsStream

  7. #7
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: Files in exported jar not being accessed?

    Java Code:
    AudioInputStream ais = AudioSystem.getAudioInputStream(getClass().getResourceAsStream(s));
    That is my code for getting the files. I am 100% sure that is getResourceAsStream();

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Files in exported jar not being accessed?

    Quote Originally Posted by pj6444 View Post
    Java Code:
    AudioInputStream ais = AudioSystem.getAudioInputStream(getClass().getResourceAsStream(s));
    That is my code for getting the files. I am 100% sure that is getResourceAsStream();
    Like I said
    Quote Originally Posted by masijade View Post
    almost guarantee
    with the stress, in this instance, on the almost as that is, 90% of the time, the error people asking this question make.

    Now that you've gotten all self-defensive and offended, maybe you can look at the first part of that post

    Quote Originally Posted by masijade View Post
    Run the jar from the command line (shell, whatever your system calls it), not by "double clicking" or anything like that.
    and tell us if any errors are actually produced when you execute that program, so that we can help you.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Files in exported jar not being accessed?

    To check if the path is correct, print out the URL returned by the getResource() method.

    Are there any empty catch(){} blocks?
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: Files in exported jar not being accessed?

    How can I print out the URL? No, I have e.printStackTrace() in all catch blocks

  11. #11
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: Files in exported jar not being accessed?

    Sorry.

    I have no errors when I run from the command line. Is there a special way I should be running the jar to have errors print?

    I am in cmd and what I am doing is:
    C:\Users\User\Desktop>Shooter.jar

    I have no errors coming up

  12. #12
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Files in exported jar not being accessed?

    Post some of your code. I.E. What is s?

    Use System.out.println to print both the value of s as well as a call to getResource (not getResourceAsStream) to see the created url.

  13. #13
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Files in exported jar not being accessed?

    Quote Originally Posted by pj6444 View Post
    Sorry.

    I have no errors when I run from the command line. Is there a special way I should be running the jar to have errors print?

    I am in cmd and what I am doing is:
    C:\Users\User\Desktop>Shooter.jar

    I have no errors coming up
    That should produce something like "command not found" as you do not execute jars that way. Do "java -jar Shooter.jar".

  14. #14
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: Files in exported jar not being accessed?

    Java Code:
    import javax.sound.sampled.AudioFormat;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.Clip;
    
    public class AudioPlayer {
    
    	private Clip clip;
    	
    	public AudioPlayer(String s) {
    
    		try {
    			AudioInputStream ais = AudioSystem.getAudioInputStream(getClass().getResourceAsStream(s));
    
    			AudioFormat baseFormat = ais.getFormat();
    			AudioFormat decodeFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16,baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
    		
    			AudioInputStream dais = AudioSystem.getAudioInputStream(decodeFormat, ais);
    			clip = AudioSystem.getClip();
    			clip.open(dais);
    		} catch(Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    	public void play() {
    		if(clip == null) return;
    		stop();
    		clip.setFramePosition(0);
    		clip.start();
    	}
    	
    	private void stop() {
    		if(clip.isRunning()) clip.stop();
    	}
    	
    	private void close() {
    		stop();
    		clip.close();
    	}
    	
    }
    Here is the code for the AudioPlayer

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Files in exported jar not being accessed?

    What is shown on the console when you use the commandline to execute the jar file?
    Did you add a print out of the URL returned by getResource()?
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Files in exported jar not being accessed?

    That looks okay. As mentioned add a System.out.println call for s as well as for the returned value of a call to getResource (not getResourceAsStream).

  17. #17
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: Files in exported jar not being accessed?

    So, here is what the console printed.

    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation. All rights reserved.

    C:\Users\Patrick>cd C:\Uses\Patrick\Desktop
    The system cannot find the path specified.

    C:\Users\Patrick>cd C:\Users\Patrick\Desktop

    C:\Users\Patrick\Desktop>java -jar Shooter.jar
    java.io.IOException: mark/reset not supported
    at java.util.zip.InflaterInputStream.reset(Unknown Source)
    at java.io.FilterInputStream.reset(Unknown Source)
    at com.sun.media.sound.SoftMidiAudioFileReader.getAud ioInputStream(Unkno
    wn Source)
    at javax.sound.sampled.AudioSystem.getAudioInputStrea m(Unknown Source)
    at com.patrickfeltes.sound.AudioPlayer.<init>(AudioPl ayer.java:15)
    at com.patrickfeltes.entities.Player.<init>(Player.ja va:34)
    at com.patrickfeltes.gamestate.Wave1State.init(Wave1S tate.java:48)
    at com.patrickfeltes.gamestate.Wave1State.<init>(Wave 1State.java:42)
    at com.patrickfeltes.gamestate.GameStateManager.<init >(GameStateManager.
    java:26)
    at com.patrickfeltes.main.GamePanel.init(GamePanel.ja va:62)
    at com.patrickfeltes.main.GamePanel.run(GamePanel.jav a:67)
    at java.lang.Thread.run(Unknown Source)
    java.io.IOException: mark/reset not supported
    at java.util.zip.InflaterInputStream.reset(Unknown Source)
    at java.io.FilterInputStream.reset(Unknown Source)
    at com.sun.media.sound.SoftMidiAudioFileReader.getAud ioInputStream(Unkno
    wn Source)
    at javax.sound.sampled.AudioSystem.getAudioInputStrea m(Unknown Source)
    at com.patrickfeltes.sound.AudioPlayer.<init>(AudioPl ayer.java:15)
    at com.patrickfeltes.gamestate.Wave1State.init(Wave1S tate.java:60)
    at com.patrickfeltes.gamestate.Wave1State.<init>(Wave 1State.java:42)
    at com.patrickfeltes.gamestate.GameStateManager.<init >(GameStateManager.
    java:26)
    at com.patrickfeltes.main.GamePanel.init(GamePanel.ja va:62)
    at com.patrickfeltes.main.GamePanel.run(GamePanel.jav a:67)
    at java.lang.Thread.run(Unknown Source)
    Hello World
    java.io.IOException: mark/reset not supported
    at java.util.zip.InflaterInputStream.reset(Unknown Source)
    at java.io.FilterInputStream.reset(Unknown Source)
    at com.sun.media.sound.SoftMidiAudioFileReader.getAud ioInputStream(Unkno
    wn Source)
    at javax.sound.sampled.AudioSystem.getAudioInputStrea m(Unknown Source)
    at com.patrickfeltes.sound.AudioPlayer.<init>(AudioPl ayer.java:15)
    at com.patrickfeltes.entities.Player.<init>(Player.ja va:34)
    at com.patrickfeltes.gamestate.Wave1State.init(Wave1S tate.java:48)
    at com.patrickfeltes.gamestate.GameStateManager.setSt ate(GameStateManage
    r.java:66)
    at com.patrickfeltes.gamestate.MenuState.selectOption (MenuState.java:41)

    at com.patrickfeltes.gamestate.MenuState.keyPressed(M enuState.java:66)
    at com.patrickfeltes.gamestate.GameStateManager.keyPr essed(GameStateMana
    ger.java:43)
    at com.patrickfeltes.main.GamePanel.keyPressed(GamePa nel.java:113)
    at java.awt.Component.processKeyEvent(Unknown Source)
    at javax.swing.JComponent.processKeyEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.KeyboardFocusManager.redispatchEvent(Unkn own Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEv ent(Unknown Source)

    at java.awt.DefaultKeyboardFocusManager.preDispatchKe yEvent(Unknown Sour
    ce)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAsse rtions(Unknown Sour
    ce)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent (Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Sour
    ce)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Sour
    ce)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Sour
    ce)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
    java.io.IOException: mark/reset not supported
    at java.util.zip.InflaterInputStream.reset(Unknown Source)
    at java.io.FilterInputStream.reset(Unknown Source)
    at com.sun.media.sound.SoftMidiAudioFileReader.getAud ioInputStream(Unkno
    wn Source)
    at javax.sound.sampled.AudioSystem.getAudioInputStrea m(Unknown Source)
    at com.patrickfeltes.sound.AudioPlayer.<init>(AudioPl ayer.java:15)
    at com.patrickfeltes.gamestate.Wave1State.init(Wave1S tate.java:60)
    at com.patrickfeltes.gamestate.GameStateManager.setSt ate(GameStateManage
    r.java:66)
    at com.patrickfeltes.gamestate.MenuState.selectOption (MenuState.java:41)

    at com.patrickfeltes.gamestate.MenuState.keyPressed(M enuState.java:66)
    at com.patrickfeltes.gamestate.GameStateManager.keyPr essed(GameStateMana
    ger.java:43)
    at com.patrickfeltes.main.GamePanel.keyPressed(GamePa nel.java:113)
    at java.awt.Component.processKeyEvent(Unknown Source)
    at javax.swing.JComponent.processKeyEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.KeyboardFocusManager.redispatchEvent(Unkn own Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEv ent(Unknown Source)

    at java.awt.DefaultKeyboardFocusManager.preDispatchKe yEvent(Unknown Sour
    ce)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAsse rtions(Unknown Sour
    ce)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent (Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Sour
    ce)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Sour
    ce)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Sour
    ce)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
    Hello World

    C:\Users\Patrick\Desktop>

  18. #18
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: Files in exported jar not being accessed?

    This is what the URL comes out as:

    javax.sound.sampled.AudioInputStream@75f172aa
    javax.sound.sampled.AudioInputStream@4da87e3f

  19. #19
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Files in exported jar not being accessed?

    Okay, that looks simply as though AudioInputStream doesn't work when wrapping a stream it receives out of a jarfile (that reference to zip.InflaterInputStream in the stack trace as a jarfile is nothing but a zipfile with a different file ending).

    Two choices, when the program starts it creates a tmp directory and reads the audio files out of the jar and into files in that tmp directory, then play the sounds by loading those files, or, if there are not alot of files and they are small, then read those files into a byte array for each file and create a ByteArrayInputStream from each of those arrays and use those InputStreams in the getAudioInputStream call (this second option may, or may not, work).
    Last edited by masijade; 11-14-2013 at 02:26 PM.

  20. #20
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Files in exported jar not being accessed?

    Quote Originally Posted by pj6444 View Post
    This is what the URL comes out as:

    javax.sound.sampled.AudioInputStream@75f172aa
    javax.sound.sampled.AudioInputStream@4da87e3f
    Uhm, that looks like the result of a call to getResourceAsStream, not getResource.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 06-21-2013, 06:21 PM
  2. Question about using icons in exported jar
    By dc495 in forum Advanced Java
    Replies: 0
    Last Post: 11-08-2012, 04:15 PM
  3. Replies: 0
    Last Post: 04-10-2012, 02:21 PM
  4. Reference an array so it can be accessed
    By DGates in forum New To Java
    Replies: 5
    Last Post: 02-06-2012, 09:12 PM
  5. Probelms with the ConnectorJ and exported JAR
    By Smokin' Caterpillar in forum JDBC
    Replies: 5
    Last Post: 10-18-2010, 10:26 AM

Posting Permissions

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