Search code examples
javajnavlcj

vlcj cannot locate plugins library in default install folder, NativeDiscovery works


I am trying to bundle vlcj with my java application however I cannot load vlcj without native discovery. Both my Java and VLC installs are 64bit so no issues should be there. I can get my videos to play just fine while using native discovery. I tried JNA 3.5.2, JNA 4.1.0, JNA 4.5.1 and all yield the same results

The simplest example I can get:

public class Test {

private final JFrame frame;

private final EmbeddedMediaPlayerComponent mediaPlayerComponent;

public static void main(final String[] args) {

    NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:\\Program Files\\VideoLAN\\VLC");
    System.setProperty("VLC_PLUGIN_PATH",  "C:\\Program Files\\VideoLAN\\VLC\\plugins");
    System.out.println(LibVlc.INSTANCE.libvlc_get_version());

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Test(args);
        }
    });
   }

public Test(String[] args) {
    frame = new JFrame("My First Media Player");
    frame.setBounds(100, 100, 600, 400);
    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    frame.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            mediaPlayerComponent.release();
            System.exit(0);
        }
    });
    mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
}
   }

The stacktrace:

[main] INFO uk.co.caprica.vlcj.Info - vlcj: 3.10.1
[main] INFO uk.co.caprica.vlcj.Info - java: 10.0.1 Oracle Corporation
[main] INFO uk.co.caprica.vlcj.Info - java home: C:\Program Files\Java\jdk-10.0.1
[main] INFO uk.co.caprica.vlcj.Info - os: Windows 10 10.0 amd64
3.0.3 Vetinari
[AWT-EventQueue-0] INFO uk.co.caprica.vlcj.binding.LibVlcFactory - vlc:      3.0.3 Vetinari, changeset 3.0.3-1-0-gc2bb759264
[AWT-EventQueue-0] INFO uk.co.caprica.vlcj.binding.LibVlcFactory - libvlc: C:\Program Files\VideoLAN\VLC\libvlc.dll
[AWT-EventQueue-0] ERROR uk.co.caprica.vlcj.player.MediaPlayerFactory - Failed to initialise libvlc
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Failed to initialise libvlc.

This is most often caused either by an invalid vlc option being passed when creating a MediaPlayerFactory or by libvlc being unable to locate the required plugins.

If libvlc is unable to locate the required plugins the instructions below may help:

In the text below <libvlc-path> represents the name of the directory containing "libvlc.dll" and "libvlccore.dll" and <plugins-path> represents the name of the directory containing the vlc plugins...

For libvlc to function correctly the vlc plugins must be available, there are a number of different ways to achieve this:
 1. Make sure the plugins are installed in the "<libvlc-path>/plugins" directory, this should be the case with a normal vlc installation.
 2. Set the VLC_PLUGIN_PATH operating system environment variable to point to "<plugins-path>".

More information may be available in the log.


at uk.co.caprica.vlcj.player.MediaPlayerFactory.<init>(MediaPlayerFactory.java:300)
at uk.co.caprica.vlcj.player.MediaPlayerFactory.<init>(MediaPlayerFactory.java:259)
at uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent.onGetMediaPlayerFactory(EmbeddedMediaPlayerComponent.java:349)
at uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent.<init>(EmbeddedMediaPlayerComponent.java:217)
at vlctest.Test.<init>(Test.java:46)
at vlctest.Test$1.run(Test.java:30)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
at java.desktop/java.awt.EventQueue.access$600(EventQueue.java:97)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

Any idea what might be the cause?


Solution

  • So thanks to cubrr I was able to solve this. Simply creating a batch file that sets an environmental variable and then runs the jar makes it work.

    set VLC_PLUGIN_PATH=C:\Program Files\VideoLAN\VLC\plugins
    start cmd.exe /c start "" javaw -jar test.jar
    

    Note that using javaw to run the jar makes the console window close so it doesnt stay below the app window while using java makes the console stay.

    Also just leaving this here: A ClassLoader can be used to load the libvlc.dll and libvlccore.dll

    File jarDir = new File(ClassLoader.getSystemClassLoader().getResource(".").getPath());
    NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), jarDir.getAbsolutePath());`
    

    This will work if your jar file and dlls are in the same directory.