0

I have a jar file that's running, but it needs an exe and a couple dlls with it to work.

To make it convenient for the user, I want to package the folder with the exe and the 2 dlls in the jar, and have it extract it when the jar is ran.

I've read answers like this one, and this one, but I still don't understand how to apply that code to what I need. I understand that a jar file is essentially a zip, but I don't know how I can get the path to the zip, and extract the folder I need from it.

I tried using the code posted here to just extract the exe for a start, but it looks like it's trying to extract the exe from the class (if that makes any sense?)

Does anyone have a code snipped they could share to show how to extract a folder with a certain name from the currently executing jar?

If you guys could help me out I would really appreciate it!

Community
  • 1
  • 1
Keith M
  • 1,199
  • 2
  • 18
  • 38

1 Answers1

2

If you want to access the resources from a JAR that is on the classpath at runtime, you simply access the resources from the classpath. So no need to read it via the JAR API. Therefore your first link points to a valid solution.

It may be tricky to use the appropriate classloader. If your resource is in the same folder as a Java class file, this link may help you: How to access resources in JAR file?

URL url = getClass().getResource("path/to/img.png");

or

URL url = MyClass.class.getResource("path/to/img.png");

I think you could access the two files separately and store them to a file. There are also methods to open a stream to copy. Here is a similar question: getResourceAsStream returns null

Community
  • 1
  • 1
Robert Reiner
  • 531
  • 3
  • 9