Eclipse exported Runnable JAR not showing images

后端 未结 8 1693
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 16:46

My images will not load when running a JAR file exported from Eclipse.

I have the images in a resources class package. I\'ve tried a images source folder as well wit

8条回答
  •  温柔的废话
    2020-11-22 17:00

    For creating a runnable JAR file from Eclipse we may refer to the article "Creating Runnable Jars in Eclipse (or other IDE...or by hand):" (https://www.cefns.nau.edu/~edo/Classes/CS477_WWW/Docs/RunnableJarsinEclipse.html), it mentioned that we need do four things as

    1. Make sure create a package for our code, not just create a project in Eclipse
    2. Create the sub-pakage (subfolder) for our resource files under the main package of our code (note that the sub-package is under main package, is not only in the project)
    3. get all file references from getResource() (getting the URL reference)
    4. Export of files as a runnable JAR in Eclipse (File -> Export... -> select Runnable JAR files -> next -> ...)

    But for image file in the example code of above article it only creates the ImageIcon, it does not create the SWT Image, and there are many questions in the Internet for how to get SWT Image from URL or how to convert ImageIcon to SWT Image, below is the example code for getting the SWT Image from URL,

    Image imgSWT=null;  // Image class is the SWT Image class
    ImageDescriptor imgDesc=null;
    java.net.URL imgURL = YourClassName.class.getResource("path/image_filename");
    
    if (imgURL != null) {
        imgDesc = ImageDescriptor.createFromURL(imgURL);
        imgSWT = imgDesc.createImage();
    }
    

提交回复
热议问题