Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java, JNI and Matlab

843829Jul 9 2009 — edited Jul 10 2009
I'm following the Matlab tutorial to try to call native code generated from Matlab in java ( see [http://www.mathworks.com/support/solutions/en/data/1-1Q9IQ2/index.html?product=CO|http://www.mathworks.com/support/solutions/en/data/1-1Q9IQ2/index.html?product=CO] )

I followed all the steps but when I try to run the java code, I got an error message.

Here is the java class (I added some line to display better what happens ):
/*

 * Java class to the MatrixDriver example from Java.

 * This class assumes a shared lib called 

 * (lib)jnimatrixdriver.(so,dll) that contians the

 * necessary gateway functions for initialization,

 * termination, and execution of the compiled m-functions.

 * This shared library in turn links against a compiler

 * generated shared lib (libmatrix) that contians the actual

 * m-functions. 

 */

public class MatrixDriver

{

    /* 

     * Native methods corresponding to the functions

     * that we need to call in the compiler-generated

     * shared library.

     */

    public static native void applicationInitialize();

    public static native void libInitialize();

    public native double[][] addMatrix(double[][] a, double[][] b);

    public native double[][] multiplyMatrix(double[][] a, double[][] b);

    public native double[][] eigMatrix(double[][] a);

    public static native void libTerminate();

    public static native void applicationTerminate();



    public MatrixDriver()

    {

    }

    public static void main(String[] args)

    {

        /* Input arrays */

        double[][] a = {{1,4,7},{2,5,8},{3,6,9}};

        double[][] b = {{1,4,7},{2,5,8},{3,6,9}};

        /* Output array */

        double[][] c = null;

 

        /* Call the application intialization routine. */

        applicationInitialize();

        /* Call the library intialization routine. */

        libInitialize();

        /* Call the library functions */

        MatrixDriver md = new MatrixDriver();

        c = md.addMatrix(a, b);

        System.out.println("The value of added matrix is:");

        display(c);

        c = md.multiplyMatrix(a, b);

        System.out.println("The value of the multiplied matrix is:");

        display(c);

        c = md.eigMatrix(a);

        System.out.println("The Eigen value of the first matrix is:");

        display(c);

        /* Call the library termination routine. */

        libTerminate();

        /* Call the application termination routine. */

        applicationTerminate();

    }

    /* Helper function to print out a double matrix */

    private static void display(double[][] a)

    {

        for (int i=0; i<a.length; i++)

        {

            double[] row = a;

for (int j=0; j<row.length; j++)

{

System.out.print(row[j] + " ");

}

System.out.print("\n");

}

System.out.print("\n");

}

static

{

/*

* Load the jnimatrixdriver library

* containing the native method implementations.

*/
System.out.println(System.getProperty("java.library.path"));
java.io.File f = new java.io.File(System.getProperty("java.library.path") +
java.io.File.separatorChar + "jnimatrixdriver.so");
System.out.println("f.getPath() :" +f.getPath());
System.out.println("f.isFile() : " +f.isFile());
System.out.println("f.canRead() : " +f.canRead());
System.out.println("f.canExecute() : "+f.canExecute());

//System.load(f.getPath());

System.loadLibrary("jnimatrixdriver");

}

}

And here is the output :
user:~/Documents/MATLAB/TEST_SHARED_LIBRARY$ java -Djava.library.path=/home/bpavie/Documents/MATLAB/TEST_SHARED_LIBRARY/ MatrixDriver
/home/bpavie/Documents/MATLAB/TEST_SHARED_LIBRARY/
f.getPath() :/home/bpavie/Documents/MATLAB/TEST_SHARED_LIBRARY/jnimatrixdriver.so
f.isFile() : true
f.canRead() : true
f.canExecute() : true
Exception in thread "main" java.lang.UnsatisfiedLinkError: /home/bpavie/Documents/MATLAB/TEST_SHARED_LIBRARY/jnimatrixdriver.so: libmatrix.so: cannot open shared object file: No such file or directory
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1778)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1674)
at java.lang.Runtime.load0(Runtime.java:770)
at java.lang.System.load(System.java:1005)
at MatrixDriver.<clinit>(MatrixDriver.java:87)
Could not find the main class: MatrixDriver. Program will exit.
So we quickly see that there is a big contradiction between the test I do in 
static
{
/*
* Load the jnimatrixdriver library
* containing the native method implementations.
*/
System.out.println(System.getProperty("java.library.path"));
java.io.File f = new java.io.File(System.getProperty("java.library.path") +
java.io.File.separatorChar + "jnimatrixdriver.so");
System.out.println("f.getPath() :" +f.getPath());
System.out.println("f.isFile() : " +f.isFile());
System.out.println("f.canRead() : " +f.canRead());
System.out.println("f.canExecute() : "+f.canExecute());

//System.load(f.getPath());

System.loadLibrary("jnimatrixdriver");
}
and the output of the java command which say that "
{code}
/home/bpavie/Documents/MATLAB/TEST_SHARED_LIBRARY/jnimatrixdriver.so: libmatrix.so: cannot open shared object file: No such file or directory
{code}

I'm totally lost right, I don't see any problem...

If someone has a solution....

Benjamin

Edited by: NeboP on Jul 9, 2009 3:40 PM

Edited by: NeboP on Jul 9, 2009 3:41 PM

Edited by: NeboP on Jul 9, 2009 3:42 PM

Edited by: NeboP on Jul 9, 2009 3:43 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 7 2009
Added on Jul 9 2009
5 comments
816 views