Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Catching System.load() Errors

My main():

System.out.println("Start loading libraries");
boolean b2 = false;
try{
  b2 = FileManager.loadBinaries();
} catch (Exception e){
  System.out.println("Exception on loading");
}
System.out.println("Libraries loading ended");

LoadBinaries():

public static boolean loadBinaries(){
    String os = System.getProperty("os.name").toLowerCase();
    ArrayList<String> bins = new ArrayList<String>();

    if(os.indexOf("windows 7") >= 0 || os.indexOf("windows vista") >= 0){
        bins.add("/nm/metadata/bin/win/libcurld.dll");
        bins.add("/nm/metadata/bin/win/libfftw3f-3.dll");
        bins.add("/nm/metadata/bin/win/libmad.dll");
        bins.add("/nm/metadata/bin/win/libsamplerate.dll");
        bins.add("/nm/metadata/bin/win/seven/mylib.dll");
    }
    else if(os.indexOf("windows xp") >= 0){
        bins.add("/nm/metadata/bin/win/libcurld.dll");
        bins.add("/nm/metadata/bin/win/libfftw3f-3.dll");
        bins.add("/nm/metadata/bin/win/libmad.dll");
        bins.add("/nm/metadata/bin/win/libsamplerate.dll");
        bins.add("/nm/metadata/bin/win/xp/mylib.dll");
    } else if(os.indexOf("mac") >= 0){
        return false;
    }

    File f = null;
    for(String bin : bins){
        InputStream in = FileManager.class.getResourceAsStream(bin);
        byte[] buffer = new byte[1024];
        int read = -1;
        try {
            String[] temp = bin.split("/");
            f = new File(LIB_FOLDER + "/" + temp[temp.length-1]);
            File realF = new File(f.getAbsolutePath());

            if(!realF.exists()){
                FileOutputStream fos = new FileOutputStream(realF);

                while((read = in.read(buffer)) != -1) {
                    fos.write(buffer, 0, read);
                }
                fos.close();
                in.close();
            }
            System.out.println("Hello Load");
            System.load(f.getAbsolutePath());
            System.out.println("Bye Load");
        } catch (Exception e) { System.out.println("Bye Exception"); FileManager.log(e.getMessage(), true); librariesLoaded = false; return false; }
    }

    System.out.println("Bye Method");
    librariesLoaded = true;
    return true;
}

When I run this main I get next output:

Start loading libraries
Hello Load
Bye Load
Hello Load
Bye Load
Hello Load
Bye Load
Hello Load
Bye Load
Hello Load
Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\java\workspace\Lib\mylib.dll: The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log for more detail
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary0(Unknown Source)
    at java.lang.ClassLoader.loadLibrary(Unknown Source)
    at java.lang.Runtime.load0(Unknown Source)
    at java.lang.System.load(Unknown Source)
    at nm.player.FileManager.loadBinaries(FileManager.java:264)
    at nm.player.Player.<init>(Player.java:88)
    at nm.player.Player.main(Player.java:523)

This error is beacause it is missing some c++ system dlls. But my problem is not this. I am concern about where the program goes after this error! I don't see the print on the catch, the print after the loop in the method and neither the prints on the main, after loadbinaries is executed.

How can I catch this type of errors and deal with them? Example: When this error occur, I want to print "please intall c++ libraries" and control the flow after it.

like image 892
supertreta Avatar asked Apr 23 '11 12:04

supertreta


People also ask

How do you catch system errors in Java?

The try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

Can we catch an error in Java?

Yes, we can catch an error. The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the throw statement.

How do you handle exceptions in catch block in Java?

Java catch block is used to handle the Exception by declaring the type of exception within the parameter. The declared exception must be the parent class exception ( i.e., Exception) or the generated exception type. However, the good approach is to declare the generated type of exception.

What is Java loadLibrary system?

loadLibrary(String filename) method loads the dynamic library with the specified library name. A file containing native code is loaded from the local file system from a place where library files are conventionally obtained. The details of this process are implementation-dependent.


3 Answers

Try replacing

catch (Exception e)

at the bottom of your loadBinaries() method with

catch (UnsatisfiedLinkError e)

UnsatisfiedLinkError is a subclass of Error, which is not a subclass of Exception: Error and Exception are both subclasses of Throwable, the root of the Java exception hierarchy.

Normally, you don't catch Errors. However, it seems you have a reasonable case for doing so here, in that you can display a message to your users saying 'Library X is missing, please install it'.

like image 147
Luke Woodward Avatar answered Sep 27 '22 23:09

Luke Woodward


You're getting an UnsatisfiedLinkError which is not a subclass of Exception and thus is not caught by your catch clause. If you want it to be caught change the catch to catch(Error e).

You see, Java's exception hierarchy is a bit unintuitive. You have two classes, Exception and Error, each of which extends Throwable. Thus, if you want to catch absolutely everything you need to catch Throwable (not recommended).

RuntimException, by the way, is a subclass of Exception.

like image 28
Itay Maman Avatar answered Sep 27 '22 23:09

Itay Maman


Its an error. and its good practice to not to catch the errors as per java PMD.

you can follow these link for more info

When to catch java.lang.Error?

http://pmd.sourceforge.net/rules/strictexception.html

like image 42
GuruKulki Avatar answered Sep 27 '22 23:09

GuruKulki