简体   繁体   中英

Java try catch loop not catching after 1 loop

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)
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

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){

        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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