Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Loading Local Image for Paint


    Opoz

    Recommended Posts

    Okay so I have spent about 15 hours trying to figure this out. The issue is I am trying to load an image in the jar and set it to a variable.

    I HAVE looked at other posts and I think I have tried everything I could find but nothing seems to work.

    I am not asking how to FIND the image as I can get the URL of the image, but then ImageIO.read(URL) is not throwing any exception, but returning null. The image is a .png which I have heard is compatible with ImageIO.read().

    Any help is appreciated. Thank you!

    My Project:

            Project
            ->src
            -->package
            --->Main.java
            --->paint.png

    My Code:

    In my onStart() method:
          

    (mainPaint is a private Image)

    mainPaint = getImage("paint.png");
    

    The method:

       

    private Image getImage(String fileName) {
    
        URL url = getClass().getResource(fileName);
        BufferedImage image = null;
            
        log(url.toString()); // To make sure I have the correct file
        // returning jar:file:/C:/Users/Me/DreamBot/Scripts/MyJar.jar!/package/paint.png
        log(url.getPath());
        // returning file:/C:/Users/Me/DreamBot/Scripts/MyJar.jar!/package/paint.png
            
        try {
            image = ImageIO.read(url);
        } catch (IOException e1) {
            log("Error converting url to image.");
            // This is not happening
        }
        
        if (image == null) {
            log("Image is null.");
            // This is happening
        }
            
        return image;
    }

    Is the URL invalid? Am I just missing something? I'm just trying to save the local image in the jar as an Image object, I feel like this is way too difficult for what I am trying to do.

    Link to comment
    Share on other sites

    You should add another "/" in front of "/paint.png", if that doesn't work, Try going back a few files...."/package/paint.png"

    Like I said it isn't having issues finding the URL. It is finding the location of the image but can't read it. I also just tried:

            Image image = Toolkit.getDefaultToolkit().getImage(getClass().getResource(fileName));
            if(image == null) {
                log("Image is null");
            }
            log("Height: " + image.getHeight(null));
            log("Width: " + image.getWidth(null));
            
            BufferedImage bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    
            // Draw the image on to the buffered image
            Graphics2D bGr = bimage.createGraphics();
            bGr.drawImage(image, 0, 0, null);
            bGr.dispose();
    
            // Return the buffered image
            return bimage;

    and getHeight and getWidth are returning -1. If I open the exported jar and open the image it works fine and has no issues so the image is a working image.

    Link to comment
    Share on other sites

    does it work if you run it not in a jar ?

    Using the first method it returns the proper width and height if I run it without exporting it.

     

    Using the second method it still returns width and height as -1 if I run it without exporting it.

     

    I would assume that means that the URL it is returning when it is in the jar is invalid?

     

    This is the toString() of the URL when ran in eclipse: file:/C:/Users/Opoz/git/repo/project/bin/package/paint.png

    This is the toString() of the URL when ran after exporting: jar:file:/C:/Users/Opoz/DreamBot/Scripts/MyJar.jar!/package/paint.png

    This is the getPath() of the URL when ran after exporting: file:/C:/Users/Opoz/DreamBot/Scripts/MyJar.jar!/package/paint.png

    Link to comment
    Share on other sites

    It looks like you only ask for the filename in your method, i haven't spent time reading all the code, but doesn't your method require the full pathname?

    Link to comment
    Share on other sites

    It looks like you only ask for the filename in your method, i haven't spent time reading all the code, but doesn't your method require the full pathname?

    It uses the filename to find the location of the file and makes that a URL object. For some reason the URL object can be read using ImageIO.read(URL) fine in eclipse, but returns null once I export it.

    Link to comment
    Share on other sites

    It uses the filename to find the location of the file and makes that a URL object. For some reason the URL object can be read using ImageIO.read(URL) fine in eclipse, but returns null once I export it.

    Ahh i see. I know what this problem is: have had this with a PDF file i wanted to open.

    But my problem was that the pdf file was inside the jar file, and i fixed it using the bufferreader method. Your file is outside the jar file.

     

    Maybe it's because of admin rights, blocking the request from the jar file? Try opening the jar file in administrator mode??

    Link to comment
    Share on other sites

    Jar files store the data in a different location, since it's all packaged into one thing. I had this issue before also, can't exactly remember what I did to fix though.....

    Link to comment
    Share on other sites

    Jar files store the data in a different location, since it's all packaged into one thing. I had this issue before also, can't exactly remember what I did to fix though.....

    Doesn't matter since he's requesting the file location of another file :P

    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.