Mike Viens
Mike Viens

Reputation: 2517

How to create a self-executing JAR file

I am not looking for a how to make my JAR runnable, but rather self-executing. I found this post:

http://en.newinstance.it/2012/04/17/self-executing-jar-files/

I followed the steps listed, and created my files as needed. I thought I had it working (because it would run), but what I realized was that it was not actually running the correct verison of the JAR file (the one embedded in the BAT file), but rather the one from the file system.

When I delete the JAR file from the file system, Java errors out saying it is unable to find the file. Below is the "make" script I created for my little test project. You will need to substitute your own paths and Java file though.

Can anyone get a working example of this? I would ultimately like to be able to have a self-contained batch file, with embedded binary, that can be used.

@echo off
cd /d D:\current\battle
javac -d classes src\RemoveFiles.java
pushd classes
echo Main-Class: RemoveFiles > MANIFEST.MF
jar -cvmf MANIFEST.MF ..\RemoveFiles.jar RemoveFiles*.class 
popd
echo @java -jar RemoveFiles.jar %%^* > removeStub.bat
echo @exit /b >> removeStub.bat
copy removeStub.bat /b + RemoveFiles.jar /b RemoveFiles.bat /b

Thank you for any help...

Upvotes: 2

Views: 5689

Answers (1)

Mike Viens
Mike Viens

Reputation: 2517

Jim, yes you were correct. Indeed, I was not following exactly what needed to be done. You could have been a little bit more helpful though in pointing out which part I got wrong though, rather than the standard RTFM response.

For anyone else coming to this in the future, here is the full/correct/cleaned-up "make" batch file...

@echo off
setlocal
cls

set HOME_DIR=D:\current\battle
set CLASS_DIR=classes
set SRC_DIR=src
set CLASS_NAME=RemoveFiles
set STUB=stub.bat

cd /d %HOME_DIR%
javac -d %CLASS_DIR% src\%CLASS_NAME%.java
pushd %CLASS_DIR%
echo Main-Class: %CLASS_NAME% > MANIFEST.MF
jar -cvmf MANIFEST.MF ..\%CLASS_NAME%.jar %CLASS_NAME%*.class 
popd
jar -uf %CLASS_NAME%.jar %CLASS_DIR%\%CLASS_NAME%.java
echo @java -jar %%~n0%%~x0 %%* > %STUB%
echo @exit /b >> %STUB%
copy %STUB% /b + %CLASS_NAME%.jar /b %CLASS_NAME%.bat /b
del %CLASS_NAME%.jar
del %STUB%

endlocal

Note: It is important to not just use %0. If running the batch file as "RemoveFiles", it would not work, because the ".bat" would be missing from the $0 value. If it were run as "RemoveFiles.bat", then %0 would be the full filename, allowing Java to locate the batch file as the JAR file.

Because we make batch files to save us time and typing, they are frequently executed without specifying the ".bat" potion. So, what the %%~n0%%~x0 does is force both the name and extension regardless of what was specified on the command line. The % signs need to be escaped because they are being echoed, which is why there are two of them. In the final batch file, there will only be the one.

I hope this helps the next person looking for this...

Upvotes: 10

Related Questions