VLCJ在运行时设置自定义库位置 [英] VLCJ setting custom library location at runtime

查看:257
本文介绍了VLCJ在运行时设置自定义库位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java程序,该程序使用vlcj播放视频并将VLC库打包在jar中.在运行时,将VLC库提取到用户的家中,即路径A.指示此vlcj路径的常规方法是通过jna方法:

I have a Java program that uses vlcj to play videos and that packages the VLC libs in the jar. At runtime, the VLC libs are extracted to the user's home, let's say path A. The normal way to indicate this path to vlcj is through the jna method:

NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "A");

这在Windows和MacOSX下有效,但在Linux上会抛出 UnsatisfiedLinkError .

This works under Windows and MacOSX but not Linux where it throws a UnsatisfiedLinkError.

经过反复试验,我发现唯一可以在linux下运行的方法是使用

After some trial and error, I found that the only to get this to work under linux was by using

export LD_LIBRARY_PATH=A

在执行之前,尽管有JNA文档,但这些都不在JVM设置中起作用:

prior to execution and despite the JNA documentation, none of these worked in JVM settings:

-Djava.library.path=A
-Djna.library.path=A
-Djna.platform.library.path=A

使用 LD_LIBRARY_PATH 的问题是我无法在运行时设置它(可以吗?).有人知道解决这个问题的方法吗?

My problem with using LD_LIBRARY_PATH is that it is not something I can set at runtime (can I?) which I need to do. Does anyone know of a way to get around this?

推荐答案

我自己从来没有找到理想的解决方案,但这是在我自己的vlcj项目试用期间发现的.

I never found an ideal solution to this myself, but this is what I found during my trials with my own vlcj projects.

如果您是在Linux上自行构建VLC的,则会看到以下警告:

If you build VLC yourself on Linux you will see these warnings:

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,-rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to `/etc/ld.so.conf'

它没有暗示您可以在JVM的内部中进行任何操作,至少不能不通过特权提升调用本机代码.

None of those things it suggests can you do from within your JVM, at least not without calling native code with privilege escalation.

因此,通常剩下的是:-Djna.library.path=LIBDIR应该可以工作;或者在代码System.setProperty("jna.library.path", "LIBDIR");中也应该起作用.

So, in general what you are left with is: -Djna.library.path=LIBDIR should work; alternatively in code System.setProperty("jna.library.path", "LIBDIR"); should also work.

事实上,我只是用我自己的vlcj项目中使用的本机库进行了测试,这两种方法都很好用.

In fact I just tested that with my own native library that I happen to use in my own vlcj projects and both of those approaches worked just fine.

但是,似乎VLC本身并不容易,这可能是因为VLC加载其插件的方式.

However, it seems it is not so easy with VLC itself, probably because of the way VLC loads its plugins.

理论上,如果您正确地构建目录结构,则应该会自动发现插件,因此您只需将jna.library.path指向包含libvlclibvlccore共享库的目录即可.在我的VLC构建中,目录结构如下所示:

In theory, if you structure your directories correctly, the plugins should get discovered automatically so you only need to point jna.library.path to the directory that contains the libvlc and libvlccore shared objects. In my build of VLC, the directory structure looks like this:

VLCDIR
VLCDIR/libvlc.so
VLCDIR/libvlc.so.5
VLCDIR/libvlc.so.5.4.0
VLCDIR/libvlccore.so
VLCDIR/libvlccore.so.7.0.0
VLCDIR/vlc/plugins

如果仍然失败,那么从理论上讲,您可以将VLC_PLUGIN_PATH环境变量设置为指向包含VLC插件的目录.问题是必须为本机进程设置此设置,如果您从Java应用程序内部将其设置为系统属性,则它起作用.

If this still fails then, again in theory, you can set the VLC_PLUGIN_PATH environment variable to point to the directory containing the VLC plugins. The problem is that this must be set for the native process, it will not work if you set it as a system property from inside your Java application.

我只能建议您在安装应用程序时生成一个可以正确设置环境的shell脚本文件,或者如果您想在JVM中以编程方式进行操作,则可能会有一个引导应用程序来准备本机环境然后为您的实际应用程序启动一个新的Java流程-但是那样做很麻烦.

I can only really suggest you generate a shell-script file that sets up the environment correctly when you install your application, or if you want to do it programmatically inside the JVM you could maybe have a bootstrap application that prepares the native environment and then kicks off a new Java process for your actual application - but it's messy to do things that way.

我在Linux上还看到的是,库路径似乎嵌入"到".so"文件中,并且您不能仅将这些文件复制到任何地方并且仍然希望它可以工作.这就是为什么您必须使用例如LD_LIBRARY_PATHlibtool或其他提议的解决方案之一.

What I have also seen on Linux is that the library paths seem "baked in" to the ".so" files, and you can't just copy those files anywhere and still expect it to work. That is why you must use e.g. LD_LIBRARY_PATH or libtool or one of the other proposed solutions.

这甚至还没有涉及到您在运行时对VLC及其插件可能依赖的所有其他库执行的操作-您是否也将发布所有这些库?

And this does not even touch on what you do with all the other libraries that VLC and its plugins may depend on at run-time - are you going to ship all those too?

我的建议实际上只是通过使用OS本机软件包安装命令让用户首先安装VLC,或者让安装程序应用程序首先安装VLC.不理想,但是可以.

My recommendation really is just to have the user install VLC first, or have your installer application install VLC first, by using the OS native package installation commands. Not ideal, but it works.

这篇关于VLCJ在运行时设置自定义库位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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