简体   繁体   中英

Jar file does not (Image loading related issue)

I am working on a loading screen for a school project. In Netbeans the code runs fine, but when I create an executable jar the .jar file does not execute.

I think the issue is related to the background image Loading since when I remove this chuck of code from my project. The .jar execute but when I add it to load the backgound image the .jar file does not execute. Any help will be highly appreciated.

private void loadBackground(){
     try {
        backgroundImage = ImageIO.read(
               LoadingScreen.class.getResource("../resources/linen.png"));
    } catch (IOException ex) {
        Logger.getLogger(LoadingScreen.class.getName()).log(Level.SEVERE, null, ex);
    }
   Background.setIcon(new ImageIcon(backgroundImage));
}

According to Class#getResource(String)

Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:

If the name begins with a '/' ('\/'), then the absolute name of the resource is the portion of the name following the '/'.

Otherwise, the absolute name is of the following form: modified_package_name/name Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\.').

The ../ makes no sense because it would resolve to a resource name

com/yourclasspackage/../resources/linen.png

I recommend you always use absolute paths in the resource path, ie. paths starting with / . In your case,

LoadingScreen.class.getResource("/resources/linen.png"));

assuming /resources/linen.png is at the root of the classpath.

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