Setup Launch4j to create exe file include photos

2.9k views Asked by At

I'm using Launch4j to create a exe file for my code. The exe file created successfully but because I use in my code some images into GUI, the exe file doesn't work. How can I insert my images into the exe file? Thanx

1

There are 1 answers

2
Moritz Petersen On BEST ANSWER

My projects usually have a folder structure like this:

/src
    /main
         /java
         /resources
/test
    ...

(because I use Maven; Launch4J has a good Maven plugin, btw.).

I put all images, that need to be available for the application, into the /resources folder. During the build process, images are copied into the output folder, where all the compiled .class files are. The images are now in the classpath.

To access resources (i.e. images) in your classpath, you can use the classloader, such as:

  • Resources in the root directory (src/main/resources/image.jpg) can be accessed through the System ClassLoader:

    ClassLoader.getSystemResource("image.jpg");
    ClassLoader.getSystemResourceAsStream("image.jpg");
    
  • Resources in other packages (e.g. for com.example.MyClass the resource should be at src/main/resources/com/example/image.jpg) can be accessed through the class itself:

    MyClass.class.getClassLoader().getResource("image.jpg");
    

Launch4J never complains about that and uses the images without any problem, even in .exe files.