gpt4 book ai didi

java - 为 Linux 可执行文件引用库

转载 作者:太空狗 更新时间:2023-10-29 12:08:11 26 4
gpt4 key购买 nike

我用 Java 编写了一个应用程序并使用 gcj 成功编译了它.它运行得非常好,但我遇到了一个障碍:我只能通过 shell 脚本运行可执行文件,因为我必须指定库路径。

我需要的库是 SWT、Xerces 和 GNU-crypto。

gcj 中编译时是否有静态链接库的方法,或者这不是一个好主意?或者,我可以在编译时指定(相对)库路径吗?

目前,我的 shell 脚本如下所示:

#!/bin/sh
export LD_LIBRARY_PATH=./libs/:$LD_LIBRARY_PATH
exec ./MyJavaApp $*

最佳答案

想法是使静态字段“sys_paths”为空,以便它可以根据更改后的值构建路径。请参阅此处的帖子(AjaySingh516 的帖子#223)http://forums.sun.com/thread.jspa?messageID=3744346#3744346

Class clazz = ClassLoader.class;
Field field = clazz.getDeclaredField("sys_paths");
boolean accessible = field.isAccessible();
if (!accessible)
field.setAccessible(true);
Object original = field.get(clazz);
// Reset it to null so that whenever "System.loadLibrary" is called, it
// will be reconstructed with the changed value.
field.set(clazz, null);
try {
// Change the value and load the library.
System.setProperty("java.library.path", "./libs/");
System.loadLibrary("mylibapr");
} finally {
// Revert back the changes.
field.set(clazz, original);
field.setAccessible(accessible);
}

.

gcj 系统属性(参见:libgcj 支持的标准属性)

http://gcc.gnu.org/onlinedocs/gcj/System-properties.html

.

解决方案#2: 编译时设置系统环境变量

http://linux.die.net/man/1/gcj

为此你必须使用参数 -Djava.library.path=./libs/gcj

来自 gcj 手册(以上链接):

--main=类名

此选项在链接时使用,以指定在运行结果可执行文件时应调用其“main”方法的类的名称。

-Dname[=value]

此选项只能与“--main”一起使用。它定义了一个名为 name 的系统属性,其值为 value。如果未指定值,则默认为空字符串。 这些系统属性在程序启动时初始化,可以在运行时检索使用“java.lang.System.getProperty”方法。

我从未使用过 gcj,但根据文档,这些系统属性可以在运行时检索,因此它也可以移植到其他系统。

另见:http://gcc.gnu.org/wiki/Statically_linking_libgcj?action=show&redirect=Statically+linking+libgcj

关于java - 为 Linux 可执行文件引用库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2102568/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com