如何创建一个自执行的JAR文件 [英] How to create a self-executing JAR file

查看:65
本文介绍了如何创建一个自执行的JAR文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是在寻找一种使我的JAR可运行的方法,而是在自我执行.我发现了这篇文章:

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/

我按照列出的步骤进行操作,并根据需要创建了文件.我以为我可以运行它(因为它可以运行),但是我意识到它实际上并没有运行JAR文件的正确版本(BAT文件中嵌入的版本),而是文件系统中的版本.

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.

当我从文件系统中删除JAR文件时,Java错误指出无法找到该文件.以下是我为我的小测试项目创建的"make"脚本.不过,您将需要替换自己的路径和Java文件.

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

谢谢您的帮助...

推荐答案

吉姆,是的,您是正确的.确实,我没有完全按照需要做的事情来做.虽然可以指出我出了错的那部分,而不是标准的RTFM响应,但可能会有所帮助.

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.

对于以后遇到此问题的任何人,这是完整/正确/清理过的"make"批处理文件...

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

注意:重要的是不要只使用%0.如果将批处理文件作为"RemoveFiles"运行,则它将不起作用,因为$ 0值中缺少".bat".如果以"RemoveFiles.bat"运行,则%0将是完整文件名,从而允许Java将批处理文件定位为JAR文件.

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.

因为我们制作批处理文件可以节省时间和键入时间,所以经常在不指定".bat"部分的情况下执行它们.因此, %%~n0%%~x0 的作用是强制名称和扩展名,无论在命令行上指定了什么.需要回避百分号,因为它们已被回显,这就是为什么其中有两个的原因.在最终的批处理文件中,只有一个.

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...

这篇关于如何创建一个自执行的JAR文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆