0

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)
Reid
  • 3,989
  • 8
  • 35
  • 64
  • You don't have the `LibVlc` bindings on your classpath. – chrylis -cautiouslyoptimistic- Mar 01 '14 at 03:01
  • put continue; in the catch block if you want to ignore the error and loop.... not sure why, – markgiaconia Mar 01 '14 at 03:01
  • What does that mean? How do I get them there. I know that when the user selects the correct file for VLCJ the program works. – Reid Mar 01 '14 at 03:02
  • 1
    `NoClassDefFoundError` is a subclass of `Error`, not `Exception`. Therefore, `catch(Exception e)` won't catch it. However, I won't recommend that you change the `catch`, because this kind of error needs to be addressed somehow. However, I don't know anything about `EmbeddedMediaPlayerComponent`. (chrylis probably has the answer.) – ajb Mar 01 '14 at 03:03
  • @markg continue does not work – Reid Mar 01 '14 at 03:04
  • @ajb how do handle an error? I know that it is a problem (why I am aksing the user to repeat their action. – Reid Mar 01 '14 at 03:05
  • @Reid If you go back and try again, you'll just get the same error over again. Somebody has to fix something first and then rerun the program. – ajb Mar 01 '14 at 03:06
  • that is where the folderchooser function comes in handy. It lets the user choose the function. – Reid Mar 01 '14 at 03:08
  • @Reid check out http://stackoverflow.com/questions/352780/when-to-catch-java-lang-error. However, I think this probably one of those cases that falls into the "generally, never" category. – ajb Mar 01 '14 at 03:08
  • @Reid OK, after looking at the answer and your comments, I guess this might be a case where catching this one specific error may make sense. Like I said, I don't know anything about the classes you're using. – ajb Mar 01 '14 at 03:12

3 Answers3

1
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

aliteralmind
  • 18,274
  • 16
  • 66
  • 102
  • Unfortunately not. Where does ncdx2 come from? – Reid Mar 01 '14 at 03:07
  • It works great. I meant there is no isOkeyDokey() function. Thanks!!! (and my program is a hack job anyways, so a little more hacking is just par for the course) – Reid Mar 01 '14 at 03:10
  • Really! I honestly thought I was going to need to delete the answer. Glad to help. I just taught myself something! – aliteralmind Mar 01 '14 at 03:11
  • 1
    `catch(Someclass x)` will catch any `Throwable` that is an instance of `Someclass` or a subclass of it; this is true regardless of whether `Someclass` is an `Exception` or an `Error`. – ajb Mar 01 '14 at 03:14
0

see following modification

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

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

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

        }
}
mfs_brz
  • 41
  • 7