0

Question:
I have a .jar file and 2 folders containing assets. I would like them to all be packaged into a single .exe file. I have already tried Launch4j but this does not allow the adding of assets into the .exe.

Question 2:
I define the file paths in my program using assets/audio/ + assetName. Will this need to be changed by following your method, and if so how?

This question has been asked for other languages here and here but the answers on those questions don't help.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Peake
  • 85
  • 1
  • 6
  • By assets you mean resources, correct? How about bundling them directly into to jar itself? Then, accessing them is a little different. You'll have to call `MyClass.class.getResource()` or `MyClass.class.getResourceAsStream()`. – Mordechai Feb 14 '17 at 02:38
  • @MouseEvent I have just gone through my project and followed your suggestion, my bundling looks like [this](https://gyazo.com/1d2a6055946d7327a50fd77bf47689d4) and I call the resources like [this](https://gyazo.com/b20f4e3f6317f1866188fa631a108346). All works well when run from Eclipse, though when exported as a runnable .JAR. The program appears to not be able to reference the resources, could you help me with this? If you need more information, I'd be glad to supply – Peake Feb 14 '17 at 04:11

1 Answers1

1

You can put assets inside your jar.

For example, if you have a file called Test.txt and you want to print the file content to the console

The "Test.txt" file is placed into a package called assets at the same level of the root package

The project structure will look like this

package net.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Test {
    public static void main(String[]args) {
        BufferedReader br = null;

        try {
            InputStream is = Test.class.getResourceAsStream("/assets/Test.txt");

            if(is != null) {
                br = new BufferedReader(new InputStreamReader(is)); 

                //Print the content of Test.txt
                String line;
                while((line = br.readLine()) != null) {
                    System.out.println(line); 
                }
            } else {
                //Resource not found
                System.out.println("Resource unavailable!");
            }

        } catch (IOException e) {
            e.printStackTrace(); //Log IO Error
        } finally {
            if(br != null) {
                try {
                    br.close();
                } catch (IOException e) {}
            }
        }
    }
}

You cannot access .jar resources as you normally do with external files by using a File object.

If your assets are into the jar, you can then use Launch4j (or alternative) to get an .exe.

Flood2d
  • 1,338
  • 8
  • 9
  • I have just gone through my project and followed your suggestion, my bundling looks like this and I call the resources like this. All works well when run from Eclipse, though when exported as a runnable .JAR. The program appears to not be able to reference the resources, could you help me with this? If you need more information, I'd be glad to supply – Peake Feb 18 '17 at 22:45