简体   繁体   中英

java.lang.IllegalArgumentException: input == null! at javax.imageio.ImageIO.read(Unknown Source)

I am trying to set the icon image like

private void formWindowActivated(java.awt.event.WindowEvent evt) {                                     
  try {

        Image img=ImageIO.read(getClass().getResource("images/logo.png"));
        setIconImage(img);
    } catch (IOException ex) {
        System.out.println(ex.getMessage());
    }
}   

The code seems to work perfectly after compilation on NetBeans but I get the IllegalArgumentException if I run the jar file from cmd.I don't seem to understand how the image can be visible in first case and not on the other one.

This will happen under Windows (case insensitive file system) where loading may succeed for say Images/Logo.PNG on the unpacked class path, and will fail when starting from the jar (zip Archive), where paths are case-sensitive.

Correcting the case of a file may be cumbersome (version control and such), hence a rename might be more useful.

Remarks:

If the (actual!) class is in package xyz , the directory path in the jar is x/y/z/images/logo.png .

Hence alternatives to consider are non-child class reference and absolute path:

    Image img=ImageIO.read(Xyz.class.getResource("images/logo.png"));
    Image img=ImageIO.read(getClass().getResource("/u/v/w/images/logo.png"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM