Results 1 to 4 of 4
  1. #1
    Bertieboy7 is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default FileInputStream with relative path in a JAR

    Ok, so somewhere in my rickroll application i've got a file which is rickroll.mp3 and is the argument for a fileinputstream.

    Now when i test my application in eclipse everything works fine. But when i export my source to a executable jar with eclipse the sound seems to be not loaded.

    I'm also using JLayer to play the mp3 file but that shouldn't matter really.

    So the problem here i think is that the path when compiled doesn't really point to my file anymore.

    Here is a pic of my project



    Here is a pic of the JAR file itself inside



    And here is the failing code in my MP3.java

    Java Code:
    public void play() {
    		try {
    			[COLOR="Red"][B]FileInputStream fis = new FileInputStream(
    					MP3.class.getResource("rickroll.mp3").getPath());[/B][/COLOR]
    			BufferedInputStream bis = new BufferedInputStream(fis);
    			player = new Player(bis);
    		} catch (Exception e) {
    			System.out.println("Problem playing file " + filename);
    			System.out.println(e);
    		}
    
    		// run in new thread to play in background
    		new Thread() {
    			public void run() {
    				try {
    					player.play();
    				} catch (Exception e) {
    					System.out.println(e);
    				}
    			}
    		}.start();
    
    	}

    I hope someone can help me out with what im doing wrong with the FileInputStream
    Last edited by Bertieboy7; 03-30-2011 at 06:28 PM.

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Hi, i'm also new to java so am not 100% sure but i suspect your problem is the .getPath(),
    I think this should be enough:
    Java Code:
    getClass().getResource(pathname)

  3. #3
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default

    Ozzyman has a point, but I've also had problems with this in a game that I've made. I've tried this too, and found that for some reason my audio will only work if it is in the same directory as the jar file, but not actually inside the jar file. This kinda sucks, but it's one solution, though a rick roll application where the victim had to download the rickroll.mp3 wouldn't be too effective I suppose..

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    Use getResourceAsStream to get a stream from a resource inside a jar file.

    db

Similar Threads

  1. Help needed to load relative path of file
    By friendz_forever in forum New To Java
    Replies: 8
    Last Post: 08-15-2011, 11:03 AM
  2. How to use a relative path for a file?
    By kipade in forum SWT / JFace
    Replies: 0
    Last Post: 02-28-2011, 07:37 AM
  3. Replies: 2
    Last Post: 10-14-2009, 06:47 AM
  4. how to set relative path in jsp?
    By makpandian in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-02-2009, 09:20 AM
  5. Obtaining relative path outside of executable JAR
    By tjsaker in forum Advanced Java
    Replies: 1
    Last Post: 11-14-2008, 07:48 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
  •