Page 1 of 2 12 LastLast
Results 1 to 20 of 32
  1. #1
    rdjava's Avatar
    rdjava is offline Member
    Join Date
    Jan 2011
    Posts
    38
    Rep Power
    0

    Angry InputStream/Jar Problems/File IO Problems

    Alright, so i give up, i've tried almost everything i know i can do here. I created a game engine, the game engine is all packaged into a Jar file, resources, everything. When i run the program my exception logger catches the following error below. For some reason nothing loads into the program. I'm attempting to load images, audio, and text. The program runs and my exception logger logs all the exceptions and says the program is safe to run. Though i dont have audio or graphics going.

    Exception:
    FileNotFoundException

    now i know what that means, but i dont understand why it cant find the file. I made sure that paths are ALL correct, here is some code as to how i am approaching this.

    To load an Image i do:
    String fileName = "testgame/resources/images/image.bmp";
    InputStream is = new BufferedInputStream(new FileInputStream(fileName));
    image = ImageIO.read(is);
    To load audio i do:

    AudioInputStream stream;
    File fileName = "testgame/resources/audio/audio.wav";
    songTrack = new File(fileName);

    for (int i = 0; i < clipSize; i++) {
    stream = AudioSystem.getAudioInputStream(songTrack);
    clip[i] = AudioSystem.getClip();
    clip[i].open(stream);
    }
    return true;
    To load text i do:

    File file = new File("testgame/resources/level/level.lvl");
    Scanner fileScanner = new Scanner(file);

    So i dont know whats going on, the only exception thrown is the
    file not found exception for EVERY file, image, audio, and text.

    The Main class file is in a folder in the JAR called Clash20 and the
    folder testgame is in a folder within the jar called clash20 where the Main class file is.
    So i really dont know whats going on. Please help. If you have any more
    info you need please ask.:confused:
    Last edited by rdjava; 01-06-2011 at 11:29 AM.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,096
    Rep Power
    20

    Default

    Where are those files located?#
    In the jar file?
    If so then you need to use getResourceAsStream to get the InputStream, otherwise Java will look for the files on the file system and not in the jar.
    Last edited by Tolls; 01-05-2011 at 10:54 AM. Reason: Add a link

  3. #3
    rdjava's Avatar
    rdjava is offline Member
    Join Date
    Jan 2011
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Where are those files located?#
    In the jar file?
    If so then you need to use getResourceAsStream to get the InputStream, otherwise Java will look for the files on the file system and not in the jar.
    I've already tried that approach. not even that works. Can you elaberate on how to use the getResourceAsStream? cause i think ive been using it wrong. and yes they are all in the jar

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,096
    Rep Power
    20

    Default

    I can never remember if you want a slash at the start or not for the file path (one is relative to the class, the other is from the root).
    I always have to test it out.
    But other than that it should simply be the path to the file.

  5. #5
    rdjava's Avatar
    rdjava is offline Member
    Join Date
    Jan 2011
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by rdjava View Post
    I've already tried that approach. not even that works. Can you elaberate on how to use the getResourceAsStream? cause i think ive been using it wrong. and yes they are all in the jar
    Also i tried this

    InputStream is = getClass().getResourceAsStream(imageFile);
    image = ImageIO.read(is);
    says its null

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,096
    Rep Power
    20

    Default

    That simply means the path you are giving to it is wrong.
    What path are you using?
    What does the structure of your jar look like?
    Have you tried sticking a slash at the start?

  7. #7
    rdjava's Avatar
    rdjava is offline Member
    Join Date
    Jan 2011
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    I can never remember if you want a slash at the start or not for the file path (one is relative to the class, the other is from the root).
    I always have to test it out.
    But other than that it should simply be the path to the file.
    neither one seems to work:(

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

    Default

    What is the package of the class calling that and what is the exact value of "imageFile", and where does the actual file reside in relation to the class making the call.

  9. #9
    rdjava's Avatar
    rdjava is offline Member
    Join Date
    Jan 2011
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    What is the package of the class calling that and what is the exact value of "imageFile", and where does the actual file reside in relation to the class making the call.
    package is called clash20
    the exact value is always different since there are a few dozen images being loaded, the code is within a method, the method is

    private void uploadImage(String imageFile) {

    try {
    InputStream is = new BufferedInputStream(new FileInputStream(imageFile));
    image = ImageIO.read(is);
    } catch (IOException ioe) {
    ExceptionLog.logException(ioe, true);
    } catch (IllegalArgumentException iae){
    ExceptionLog.logException(iae, true);
    }

    }
    the file(s) is/are located , "clash20/testgame/resources/images/"
    the main class is located at "clash20/"
    Last edited by rdjava; 01-05-2011 at 11:51 AM.

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

    Default

    Then, hoepfully, the "mainclass" is doing the loading and the value of "imageFile" is "testgame/resources/images/whatever" or that the value of "imageFile" is "/clash20/testgame/resources/images/whatever" (assuming that clash20 is not, itself, located within some other package, but, rather, is the "top-level" package).

  11. #11
    rdjava's Avatar
    rdjava is offline Member
    Join Date
    Jan 2011
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    Then, hoepfully, the "mainclass" is doing the loading and the value of "imageFile" is "testgame/resources/images/whatever" or that the value of "imageFile" is "/clash20/testgame/resources/images/whatever" (assuming that clash20 is not, itself, located within some other package, but, rather, is the "top-level" package).
    Ok, but for some odd reason nothing seems to be working, it either says the object loaded using the getResourceAsStream is null or the file not found exception is thrown.

  12. #12
    rdjava's Avatar
    rdjava is offline Member
    Join Date
    Jan 2011
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    Then, hoepfully, the "mainclass" is doing the loading and the value of "imageFile" is "testgame/resources/images/whatever" or that the value of "imageFile" is "/clash20/testgame/resources/images/whatever" (assuming that clash20 is not, itself, located within some other package, but, rather, is the "top-level" package).
    I also tried doing this:

    InputStream is = ImageCache.class.getResourceAsStream(imageFile);
    image = ImageIO.read(is);
    but my exception logger gives me this error

    java.lang.IllegalArgumentException: input == null!

    I do know that when the path doesnt exist it returns null. then complains about it with the Illegal Argument Exception. So i dont know what else to do. Its really getting irritating that something so simple isnt working

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

    Default

    Once again, what class is loading (and don't use another's class reference to do it use that classes class reference), what is it's full package, what is the full path to the image file, what is the complete value of the imageFile variable and, what, exactly, does your code look like in that position (as fnfe will only be thrown using on of the "file" methods, which you shouldn't be).

  14. #14
    rdjava's Avatar
    rdjava is offline Member
    Join Date
    Jan 2011
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    Once again, what class is loading (and don't use another's class reference to do it use that classes class reference), what is it's full package, what is the full path to the image file, what is the complete value of the imageFile variable and, what, exactly, does your code look like in that position (as fnfe will only be thrown using on of the "file" methods, which you shouldn't be).
    ok well i did this, the images load up in the IDE (netbeans) and again the package is clash20. the full path of the image file is "testgame/resources/images/image.bmb" i already tried adding the "/" at the begining, that doesnt work either. but i do have a question, the class is an inner class of GameEngine, the .class file is called GameEngine$ImageCache.class
    here is the full class of ImageCache

    public class ImageCache {

    private BufferedImage image;

    public ImageCache(String dir) {
    //File imageFile = new File(dir);
    uploadImage(dir);
    }

    public ImageCache(String dir, Color color) {
    //File imageFile = new File(dir);
    uploadImage(dir, color);
    }

    public ImageCache(String dir, Color color, float transparency) {
    //File imageFile = new File(dir);
    uploadImage(dir, color, transparency);
    }

    public ImageCache(BufferedImage img){
    image = img;
    }

    public ImageCache(BufferedImage img, Color color){
    image = mbt(img, color);
    }

    public ImageCache(BufferedImage img, Color color, float transparency){
    image = mbt(img, color);
    image = ti(img, transparency);
    }


    private void uploadImage(String imageFile) {
    // upload image
    try {
    InputStream is = getClass().getResourceAsStream(imageFile);
    image = ImageIO.read(is);
    } catch (Exception ioe) {
    ExceptionLog.logException(ioe, true);
    }

    }

    private void uploadImage(String imageFile, Color color) {
    // upload image and make the image's background transparent
    try {
    InputStream is = getClass().getResourceAsStream(imageFile);
    image = ImageIO.read(is);
    image = mbt(image, color);
    } catch (Exception ioe) {
    ExceptionLog.logException(ioe, true);
    }

    }

    private void uploadImage(String imageFile, Color color, float trans) {
    // upload image and make the image's background transparent and
    // translucent
    try {
    InputStream is = getClass().getResourceAsStream(imageFile);
    image = ImageIO.read(is);
    image = mbt(image, color);
    image = ti(image, trans);
    } catch (Exception ioe) {
    ExceptionLog.logException(ioe, true);
    }


    }
    }
    Again this works in the IDE of Netbeans, the program runs perfectly, but not when i execute the Jar

  15. #15
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    It sounds like the image file is not where you expect it to be in the jar file (or maybe is not in the jar file at all). Can you manually dearchive the jar file and see what the path is to the image file?

    Java Code:
    jar tf {your jar file name}.jar
    My bet is that the image file is not getting included in the jar file at all, and you probably need to modify your netbeans build to make sure it's in there.

  16. #16
    ClickerMonkey is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    Here's what I use in my mini-game engine:

    Java Code:
    	// The source directory to currently load
    	// resources from.
    	private String resources = "";
    	
    	/**
    	 * Sets the directory containing all the resources to load.
    	 * 
    	 * @param directory => The directory containing resources.
    	 */
    	public void setResourceDirectory(String directory)
    	{
    		resources = directory;
    	}
    	
    	/**
    	 * Loads a clip from the resource directory.
    	 * 
    	 * @param filename => The name of the file in the resource directory.
    	 */
    	public AudioClip loadClip(String filename) throws Exception
    	{
    		URL url = getClass().getClassLoader().getResource(resources + filename);
    		
    		if (url == null)
    			throw new IOException("Resource " + filename + " could not be found!");
    		
    		return Applet.newAudioClip(url);
    	}
    	
    	/**
    	 * Loads a font from the resource directory.
    	 * 
    	 * @param filename => The name of the file in the resource directory.
    	 * @param size => The size in pixels of the font to load.
    	 */
    	public Font loadFont(String filename, float size) throws Exception
    	{
    		URL url = getClass().getClassLoader().getResource(resources + filename);
    		
    		if (url == null)
    			throw new IOException("Resource " + filename + " could not be found!");
    			
    		InputStream stream = url.openStream();
    		Font font = Font.createFont(Font.TRUETYPE_FONT, stream);
    		stream.close();
    		return font.deriveFont(size);
    	}
    	
    	/**
    	 * Loads an image from the resource directory.
    	 * 
    	 * @param filename => The name of the file in the resource directory.
    	 */
    	public BufferedImage loadImage(String filename) throws Exception
    	{
    		URL url = getClass().getClassLoader().getResource(resources + filename);
    		
    		if (url == null)
    			throw new IOException("Resource " + filename + " could not be found!");
    		
    		InputStream stream = url.openStream();
    		BufferedImage image = ImageIO.read(stream);
    		stream.close();
    		return image;
    	}
    	
    	/**
    	 * Loads a file from the resource directory.
    	 * 
    	 * @param filename => The name of the file in the resource directory.
    	 */
    	public File loadFile(String filename) throws Exception
    	{
    		URL url = getClass().getClassLoader().getResource(resources + filename);
    		
    		if (url == null)
    			throw new IOException("Resource " + filename + " could not be found!");
    		
    		return new File(url.getFile());
    	}
    	
    	/**
    	 * Loads a stream from the resource directory.
    	 * 
    	 * @param filename => The name of the file in the resource directory.
    	 */
    	public InputStream loadString(String filename) throws Exception
    	{
    		URL url = getClass().getClassLoader().getResource(resources + filename);
    		
    		if (url == null)
    			throw new IOException("Resource " + filename + " could not be found!");
    		
    		return url.openStream();
    	}
    Then do something like:

    Loader loader = new Loader()
    loader.setResourceDirectory("testgame/resources/images/whatever");
    BufferedImage image = loader.loadImage("meow.png");

    where the resource directory starts at the root directory in the project (not the project dir itself, the next level in)

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

    Default

    Quote Originally Posted by toadaly View Post
    It sounds like the image file is not where you expect it to be in the jar file (or maybe is not in the jar file at all).
    Either that, or as I have been hinting at, the variable only contains the name of the image and not the complete path.

  18. #18
    rdjava's Avatar
    rdjava is offline Member
    Join Date
    Jan 2011
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by toadaly View Post
    It sounds like the image file is not where you expect it to be in the jar file (or maybe is not in the jar file at all). Can you manually dearchive the jar file and see what the path is to the image file?

    Java Code:
    jar tf {your jar file name}.jar
    My bet is that the image file is not getting included in the jar file at all, and you probably need to modify your netbeans build to make sure it's in there.
    nope, its there, i opened the jar with winzip. the path is correct and the image does exist

  19. #19
    rdjava's Avatar
    rdjava is offline Member
    Join Date
    Jan 2011
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by ClickerMonkey View Post
    Here's what I use in my mini-game engine:

    Java Code:
    	// The source directory to currently load
    	// resources from.
    	private String resources = "";
    	
    	/**
    	 * Sets the directory containing all the resources to load.
    	 * 
    	 * @param directory => The directory containing resources.
    	 */
    	public void setResourceDirectory(String directory)
    	{
    		resources = directory;
    	}
    	
    	/**
    	 * Loads a clip from the resource directory.
    	 * 
    	 * @param filename => The name of the file in the resource directory.
    	 */
    	public AudioClip loadClip(String filename) throws Exception
    	{
    		URL url = getClass().getClassLoader().getResource(resources + filename);
    		
    		if (url == null)
    			throw new IOException("Resource " + filename + " could not be found!");
    		
    		return Applet.newAudioClip(url);
    	}
    	
    	/**
    	 * Loads a font from the resource directory.
    	 * 
    	 * @param filename => The name of the file in the resource directory.
    	 * @param size => The size in pixels of the font to load.
    	 */
    	public Font loadFont(String filename, float size) throws Exception
    	{
    		URL url = getClass().getClassLoader().getResource(resources + filename);
    		
    		if (url == null)
    			throw new IOException("Resource " + filename + " could not be found!");
    			
    		InputStream stream = url.openStream();
    		Font font = Font.createFont(Font.TRUETYPE_FONT, stream);
    		stream.close();
    		return font.deriveFont(size);
    	}
    	
    	/**
    	 * Loads an image from the resource directory.
    	 * 
    	 * @param filename => The name of the file in the resource directory.
    	 */
    	public BufferedImage loadImage(String filename) throws Exception
    	{
    		URL url = getClass().getClassLoader().getResource(resources + filename);
    		
    		if (url == null)
    			throw new IOException("Resource " + filename + " could not be found!");
    		
    		InputStream stream = url.openStream();
    		BufferedImage image = ImageIO.read(stream);
    		stream.close();
    		return image;
    	}
    	
    	/**
    	 * Loads a file from the resource directory.
    	 * 
    	 * @param filename => The name of the file in the resource directory.
    	 */
    	public File loadFile(String filename) throws Exception
    	{
    		URL url = getClass().getClassLoader().getResource(resources + filename);
    		
    		if (url == null)
    			throw new IOException("Resource " + filename + " could not be found!");
    		
    		return new File(url.getFile());
    	}
    	
    	/**
    	 * Loads a stream from the resource directory.
    	 * 
    	 * @param filename => The name of the file in the resource directory.
    	 */
    	public InputStream loadString(String filename) throws Exception
    	{
    		URL url = getClass().getClassLoader().getResource(resources + filename);
    		
    		if (url == null)
    			throw new IOException("Resource " + filename + " could not be found!");
    		
    		return url.openStream();
    	}
    Then do something like:

    Loader loader = new Loader()
    loader.setResourceDirectory("testgame/resources/images/whatever");
    BufferedImage image = loader.loadImage("meow.png");

    where the resource directory starts at the root directory in the project (not the project dir itself, the next level in)
    Is URL an actual URL? and im not using an Applet. does that make any difference?

  20. #20
    rdjava's Avatar
    rdjava is offline Member
    Join Date
    Jan 2011
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    Either that, or as I have been hinting at, the variable only contains the name of the image and not the complete path.
    No it has the full path. Im sure of it.

Page 1 of 2 12 LastLast

Similar Threads

  1. Array, I/O with a .txt file problems
    By basketball8533 in forum New To Java
    Replies: 4
    Last Post: 10-28-2010, 09:49 AM
  2. problems with txt file
    By blumdiggity in forum New To Java
    Replies: 1
    Last Post: 02-27-2010, 01:16 PM
  3. Jar file problems!
    By viper81 in forum Eclipse
    Replies: 3
    Last Post: 12-31-2008, 10:50 AM
  4. Problems with file dir
    By willemjav in forum Java Applets
    Replies: 0
    Last Post: 02-07-2008, 12:08 AM
  5. problems with the name of file
    By bbq in forum Java Servlet
    Replies: 0
    Last Post: 06-28-2007, 04:47 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
  •