How to get the path to the executable when using launch4j?

狂风中的少年 提交于 2019-11-29 06:39:25
wallnuss

You might add to your launch4j configuration

...
<jre>
...
<opt>-Djna.library.path="%EXEDIR%\\path\\to\\lib"</opt>
<opt>-Djava.library.path="%EXEDIR%\\path\\to\\lib"</opt>
...
</jre>
...

If you need more then a you might seperate several paths by a semikolon as usual.

< opt> Optional, accepts everything you would normally pass to java/javaw launcher: assertion options, system properties and X options. Here you can map environment and special variables EXEDIR (exe's runtime directory), EXEFILE (exe's runtime full file path) to system properties. All variable references must be surrounded with percentage signs and quoted.

Source: http://launch4j.sourceforge.net/docs.html

Set -Djna.library.path=<relative path of native libraries> (if using JNA) and -Djava.library.path=<relative path of native libraries>.

Alternatively, this can be done in Java code as: System.setProperty("jna.library.path","<relative path of native libraries>") and System.setProperty("java.library.path","<relative path of native libraries>"). You can append as many paths to refer to. In Windows, use ; to separate the paths.

This setup only has its effect on the JVM runtime of that Java application (not globally like LD_LIBRARY_PATH in Linux.)

Or, you can put this in Launch4J JVM options list under JRE tab. This is what I do in my projects.

One of the options in configuration is to allow a chdir to executables directory. This will ser user.dir to same directory as exe, which you could use to find other application paths.

<chdir>

Optional. Change current directory to an arbitrary path relative to the executable. If you omit this property or leave it blank it will have no effect.

Setting it to . will change the current dir to the same directory as the executable. .. will change it to the parent directory, and so on.

<chdir>.</chdir>
<chdir>../somedir</chdir>

The code which find the actual path to executable will be dependent on OS (readlink, GetModuleFileName etc). Make sure you really test on target OSes..

If I understand your question correct, you have a launch4j executable and a native library within your installation directory:

/launch.exe
/bin/lib.dll
/lib/app.jar

Now you want to start you app.jar with the generated launcher (launch.exe). You app loads the lib.dll.

You can embed a file into your app.jar (marker.txt). Now you can use the ClassLoader

http://docs.oracle.com/javase/6/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)

getResource("marker.txt);

This will give you something like:

file://c://installdir/lib/app.jar!marker.txt

This String can be parsed. But in fact, I think there should be a better solution for this problem.

You can simply include the directory (e.g. ..\lib) where the libraries are located in the classpath tab in Launch4j. At least that worked for me.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!