Moving Files Within Jar

Started by
3 comments, last by null; 7 years, 9 months ago

I've been working on a little program recently (details aren't exactly very important at the moment), and I essentially copy a DLL file to a certain location.

I finished up the program, and it worked pretty much perfectly. Recently, I wanted to make it more compact so it could be a standalone jar without requiring a folder with assets (i.e the image background, icon, DLL, etc.).

I figured out that I could get the image as a resource from the actual jar file/class - that eliminates the need for the image assets in a separate folder.

Next, I wanted to store the DLL within the jar file so I could copy it to another location. I've tried everything, honestly. I just can't figure out how I would go about copying the DLL file from within the actual jar file to an external location.

Any help is welcome. I'll try everything.

Thanks to whomever answers!

Advertisement

Do you have to do this via code? Aren't JAR files effectively zip archives or some kind? The only thing I had to hand to test was Minecraft (exe) and I can quite happily open it up as if it is a zip archive. You could potentially do this via code and extract the desired dll to a given location.

I don't know how similar this is but when I build my Android APK it creates 2 different APKs, one for ARM and one for x86. I need to copy the x86 parts and merge them into a single APK to publish. I use a bat script (I guess it's called a script but I'm not sure of the terminology) to do this. It extracts all the files I need then re-archives them all into a single APK. I'm sure you could do something similar but obviously using an external tool (7z in my case) might not be the best option if you are doing this on a user's machine as they may not have it installed.

You could possibly do this during installation if you have such a step.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

Next, I wanted to store the DLL within the jar file so I could copy it to another location. I've tried everything, honestly. I just can't figure out how I would go about copying the DLL file from within the actual jar file to an external location.


I don't really see the problem. It's been a long while since I did something with Java but if I remember correctly you could trivially get an URL to any file in the .jar you loaded yourself from as an URL and use that URL to open a binary stream. Then just open a binary stream in the location where you want the DLL for writing and copy the content over. A far more interesting problem is going to be picking a sensible location to put the DLL and getting Java and/or the OS to load it there.

Well then... I figured it out.

Apparently while I was using FileOutputStream, I gave it an empty directory. I was supposed to add the result of the copy I wanted at the end.

In the beginning, it was something like:

C:\bla\bla\FolderWhereIWantTheDLL

now it's:

C:\bla\bla\FolderWhereIWantTheDLL\dllName.dll

Do you have to do this via code? Aren't JAR files effectively zip archives or some kind? The only thing I had to hand to test was Minecraft (exe) and I can quite happily open it up as if it is a zip archive. You could potentially do this via code and extract the desired dll to a given location.

I don't know how similar this is but when I build my Android APK it creates 2 different APKs, one for ARM and one for x86. I need to copy the x86 parts and merge them into a single APK to publish. I use a bat script (I guess it's called a script but I'm not sure of the terminology) to do this. It extracts all the files I need then re-archives them all into a single APK. I'm sure you could do something similar but obviously using an external tool (7z in my case) might not be the best option if you are doing this on a user's machine as they may not have it installed.

You could possibly do this during installation if you have such a step.

I didn't want to use any external scripts and whatnot to accomplish what I needed. I eventually ended up fixing it, so yeah!

This topic is closed to new replies.

Advertisement