Pregunta

After one loop the program throws an exception at the new component line and quits. How do I loop until the user selects a file that works?

    while(!next){
            NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), folderChooser());
            try{
                EmbeddedMediaPlayerComponent a = new EmbeddedMediaPlayerComponent();
                a.release();
                next = true;
            }
            catch(Exception e){
                next = false;
            }
    }

Error:

Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class uk.co.caprica.vlcj.binding.LibVlc
    at uk.co.caprica.vlcj.binding.LibVlcFactory.create(LibVlcFactory.java:158)
    at uk.co.caprica.vlcj.player.MediaPlayerFactory.<init>(MediaPlayerFactory.java:236)
    at uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent.onGetMediaPlayerFactory(EmbeddedMediaPlayerComponent.java:278)
    at uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent.<init>(EmbeddedMediaPlayerComponent.java:168)
    at sv1.MainRun.try1(MainRun.java:107)
    at sv1.MainRun.<init>(MainRun.java:82)
    at sv1.Start.main(Start.java:7)
¿Fue útil?

Solución

NoClassDefFoundError ncdx2 = null;
try  {

   ...

}  catch(NoClassDefFoundError ncdx)  {
   ncdx2 = ncdx;
}
if(ncdx != null)  {
   System.out.println("Try again: " + ncdx.getMessage());
}  else  {
   next = true;
}

This works, but it is definitely not recommended to use a the absence of an error as the terminating condition for a loop. Is there any way to check this without having to crash? Hopefully the API you are using has some information about some isOkeyDokey() function or something. If not, you could probably do some reflection. Anything is better than using an error as a replacement for logic.

More information: https://www.google.com/search?q=how+to+catch+an+error+in+java

Otros consejos

see following modification

try
{
while(!next)
{
   NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), folderChooser());

   EmbeddedMediaPlayerComponent a = new EmbeddedMediaPlayerComponent();
   a.release();
   next = true;
}
}
catch(Exception e){
next = false;
}

Well, I would try move the try line

while(!next){
try{
        NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), folderChooser());

            EmbeddedMediaPlayerComponent a = new EmbeddedMediaPlayerComponent();
            a.release();
        }
        catch(Exception e){

        }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top