Extract contents of Windows executable that is a wrapped jar file? - java

I've been trying to unwrap a jar file that is in a Windows executable. I tried to open it with 7Zip, but as I expected that didn't work, and going through the executable in a hex editor leads me to believe it was wrapped with exe4j. Is there any way to get the contents of the jar file?

So I figured out the solution, it turns out exe4j puts the jar file it needs into a user's temporary directory, like this. I'll leave this up for anyone else who may need to something like this.

Related

Ship and call a .exe from within a .jar [duplicate]

I have a huge JAR file, which I created with Maven Shade plugin.
I run this with java -jar foo.jar , and my UI opens. Now I want to execute *.exe file, which is also in that JAR file, how can I do this?
I tried putting the exe to my classpath and run it from there, but after trying I found out that classpath is actually the location, where my JAR is.
Any suggestions?
Found this thing here, but is this really the best solution? Seems like alot of work, I think I have different case here, because I can define the location of exe myself and the JAR is created by me.
run exe which is packaged inside jar
Why I need this?
I want to give user a single file executable, which he can run, but my program is using the *.exe. Should I put the exe next to my jar and there will be 2 files or there is solution for my requirements?
Copying the file to a temporary location and running it is the way to go. The answer you linked to does much more work that necessary, as you can get your exe file as an InputStream and copy it to a file with a utility like Apache Commons IO FileUtils.copy(in, out)
See How do I copy a text file from a jar into a file outside of the jar? for example.
It's not about the location, it's about the fact that you need to tell your OS to run the exe and, unfortunately, you can't do that by providing a location within a jar.

Converting Java URL to valid File path in Linux

Ok I'm developing in Linux using Eclipse a program that needs to read a text file. The idea is to have the JAR and text file on the same folder. So I'm getting the text file path like this:
Client.class.getClassLoader().getResource("Client.class");
This correctly returns the path and I append the file name and get the below path:
/home/marquinio/workspace/my_project/info.txt
Problem is when I export my project into an executable JAR file. The JAR can't read the file. I double checked and everything looks fine. The only problem I see is that now the path has some "file:" appended at the beginning like this:
file:/home/marquinio/workspace/my_project/info.txt
which is probably why I'm getting a "FileNotFoundException". The JAR and text file are both in the same folder.
Anyone knows how to fix this? Why would Java behave different between Eclipse and executing JAR in command prompt?
Is there a replacement to the "...getResource(...)" provided by Java without that "file:"?
NOTE: this JAR should also be compatible in Windows environment. Still need to test it.
Thanks in advance.
The resource you are referring to is not guaranteed to be a file on the file system. Why not use ClassLoader#getResourceAsStream()? Without looking into the details, my best guess is that the different behavior you are seeing is because a different classloader is being used in each case above.

Extract a JAR file, possibly with WINRAR on CL

I need to be able to extract jar files on the command line.
Piece of cake, you might say. Yes, except I need the extractor to rename same-named files.
To be specific, the Jar file has a file named:
classAX.class
and another named:
classax.class
in the same directory. I need it to extract them both, and preferably rename one:
classax (2).class
or something similar.
It must be able to do this without any user intervention.
Winrar has this capability, but when attempting to extract with unrar.exe, it says that the input file is not a valid RAR archive. (Though winrar has jar capabilities.)
Any way to force winrar to accept jars via command line, or perhaps another program?
Thanks,
~Kurt Nauck
You can develop your own programme in java, there is a Zip package that can deal with compressed files, an also you can deal with a rest of your needs
7Zip command line util accomplishes this perfectly. Thanks for the answers.

Putting bat file inside a jar file

I have a java class that uses a bat file to execute commands. However I developed it in Eclipse IDE. It works fine in there. But as I export it in a jar file, it fails to find the bat file that was included.(gives me an IOException)
The file structure in eclipse is as follows
:
Project1
---->src
------>com.myproj
-------->BatFileRead.java
----md.bat
----ul.bat
md.bat and ul.bat is same level as src directory. After jarring it src folder disappears.
Could someone help me with this.
Thanks
In order to execute the command, you'll have to extract the bat file afterwards. You can't run executables which are inside jar files. Basically you'll need to open the batch file entry in the jar file as an input stream, and copy the data to a FileOutputStream on disk. You won't be able to execute it until it's a proper standalone file on the file system.
If you're already trying to extract it, chances are you're using getResource or getResourceAsStream slightly incorrectly. This is easy to do, because it depends whether you're calling ClassLoader.getResourceAsStream or Class.getResourceAsStream. The first only ever uses absolute paths (implicitly) and the second can use either absolute or relative paths. For example, in your case you'd want:
BatFileRead.class.getResourceAsStream("/md.bat")
or
BatFileRead.class.getClassLoader().getResourceAsStream("md.bat")
Have you checked that the bat files are definitely ending up in the jar file? Just list the contents with
jar tvf file.jar
to see what's in there.
Well this can be very dangerous. Be sure to use gloves when dealing with the BAT. They bite and quite painfull. Also try getting jar that has a big enough opening, although the bats will fit almost through any hole.
Good luck, and don't try this at home. This is done by professionals.
Try copying file.jar to file.zip, and opening file.zip (e.g. just double clicking, or using e.g. 7-Zip). Can you find your .bat files inside? If the .bat file isn't there, you have to add the .bat file to your Eclipse project, and rebuild in Eclipse. If the .bat file is there, but you still get an IOException opening it from you .java application, then please post the code snippet triggering the IOException, and please give a full listing of file.zip (get the listing by extracting file.zip to C:\testdir, and running dir /s C:\testdir in the command line).
Please note that Jon Skeet is right that although it is possible to open any file in the .jar file as an InputStream using java.lang.Class.getResourceAsStream and java.lang.ClassLoader.getResourceAsStream (see tutorial or find them in the Java API docs), but in order to execute a .bat file inside, you have to extract the .jar file first.

Find text files in Jar

I have an app which has to read from a text file (using FileInputStream). The text file is in the directory structure relative to the class file (eg. "../textdir/text.txt"). When I run it normally (ie specifying the /bin folder containing the .class file in the cp) everything works fine. However, I somehow need to package everything into one jar and when I run the jar nothing works. The error is something like "FileNotFOund: MyJar.jar!/textdir/text.txt". I ran jar -tvf on the jarfile and the text file was indeed inside. I have read but not write access to the source code.
More than trying to solve my problem (I think there are plenty of workarounds), can someone explain to me how the whole thing work? How does the jar search for files? What if I want to read from current working directory of the command prompt instead of the directory of the .class in the jar file? Also, I recently had a similar problem with loading resources when I converted a non-jar project to a jar, how does that work?
Instead of opening the file as a FileInputStream, use getResourceAsStream which will work in both of your contexts ie. within the jar file or unpacked.

Categories

Resources