简体   繁体   English

使用launch4j时如何获取可执行文件的路径?

[英]How to get the path to the executable when using launch4j?

I'm using launch4j to wrap an executable jar file in my Windows application, but I need to pass references to some of its libraries in through the JVM arguments.我正在使用 launch4j 在我的 Windows 应用程序中包装一个可执行的 jar 文件,但我需要通过 JVM 参数传递对其某些库的引用。 The libraries in question reside in the application install directory, and are always located in the same place, relative to the executable.有问题的库驻留在应用程序安装目录中,并且始终位于相对于可执行文件的同一位置。

I'd like to tell launch4j to use executable-relative paths in the JVM options.我想告诉 launch4j 在 JVM 选项中使用可执行文件相对路径。 I know this information is available at the Windows batch script level, but how do you configure launch4j to fetch it?我知道此信息在 Windows 批处理脚本级别可用,但是您如何配置 launch4j 以获取它?

Edit for clarification: I'm looking specifically for how to make the paths relative to the binary itself, not how to make them relative to the current working directory.编辑澄清:我正在寻找如何使路径相对于二进制文件本身,而不是如何使它们相对于当前工作目录。 The two aren't necessarily the same.两者不一定相同。

You might add to your launch4j configuration您可能会添加到您的 launch4j 配置

...
<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. < opt> 可选,接受您通常会传递给 java/javaw 启动器的所有内容:断言选项、系统属性和 X 选项。 Here you can map environment and special variables EXEDIR (exe's runtime directory), EXEFILE (exe's runtime full file path) to system properties.在这里,您可以将环境和特殊变量 EXEDIR(exe 的运行时目录)、EXEFILE(exe 的运行时完整文件路径)映射到系统属性。 All variable references must be surrounded with percentage signs and quoted.所有变量引用都必须用百分号包围并引用。

Source: http://launch4j.sourceforge.net/docs.html来源: 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> .设置-Djna.library.path=<relative path of native libraries> (如果使用 JNA)和-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>") .或者,这可以在 Java 代码中完成: System.setProperty("jna.library.path","<relative path of native libraries>")System.setProperty("java.library.path","<relative path of native libraries>") You can append as many paths to refer to.您可以附加尽可能多的路径来引用。 In Windows, use ;在 Windows 中,使用; 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.)此设置仅对 Java 应用程序的 JVM 运行时有影响(不像 Linux 中的LD_LIBRARY_PATH那样全局。)

Or, you can put this in Launch4J JVM options list under JRE tab.或者,您可以将其放在JRE选项卡下的 Launch4J JVM 选项列表中。 This is what I do in my projects.这就是我在我的项目中所做的。

One of the options in configuration is to allow a change directory chdir to the executables directory.配置中的选项之一是允许将目录chdir更改为可执行文件目录。 This will set user.dir to same directory as exe, which you could use to find other application paths.这会将 user.dir 设置为与 exe 相同的目录,您可以使用它来查找其他应用程序路径。

<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).找到可执行文件实际路径的代码将取决于操作系统(readlink、GetModuleFileName 等)。 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:如果我理解您的问题是正确的,那么您的安装目录中有一个 launch4j 可执行文件和一个本机库:

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

Now you want to start you app.jar with the generated launcher (launch.exe).现在您要使用生成的启动器 (launch.exe) 启动 app.jar。 You app loads the lib.dll.您的应用程序加载 lib.dll。

You can embed a file into your app.jar (marker.txt).您可以将文件嵌入到您的 app.jar (marker.txt) 中。 Now you can use the ClassLoader现在您可以使用 ClassLoader

http://docs.oracle.com/javase/6/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String ) 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 (eg ..\\lib) where the libraries are located in the classpath tab in Launch4j.您可以简单地在 Launch4j 的类路径选项卡中包含库所在的目录(例如 ..\\lib)。 At least that worked for me.至少这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM