nits41089
nits41089

Reputation: 34

Java Applet + JNI + .so file

I have implemented a simple java Hello world program which uses JNI to access a native C file. I was successful in implementing it. I have performed the steps mentioned in the below URL.

http://www.java-tips.org/other-api-tips/jni/simple-example-of-using-the-java-native-interface.html.

Now, I have these files -

  1. HelloWorld.class
  2. HelloWorld.h
  3. HelloWorld.so

I need to create an applet with the help of above mentioned files. In other words, I want to use Java Applets with JNI. I have tried searching for it but I got all the solutions for windows .dll file and not for .so file.

Could anyone help me out on this?

Upvotes: 0

Views: 286

Answers (2)

nits41089
nits41089

Reputation: 34

Here is the code and the steps I have performed-

HelloWorld.java

import javax.swing.JApplet; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.applet.*;

public class HelloWorld extends Applet{
        public native String print();

        static{
                System.load("/var/www/libHelloWorld.so");
        }

        public void paint(Graphics g){
                HelloWorld hw = new HelloWorld();
                g.drawString("Message: " + hw.print(), 5, 15);
        }

}

I have compiled the java file and created HelloWorld.h file using java jni.

Then created a C file and compiled it to produced libHelloWorld.so file.

Jar file inlcudes HelloWorld.class and libHelloWorld.so file.

The jar file is signed and the HTML code is as follows:

<HTML>
<HEAD>
</HEAD>
<BODY>
<div style="border:1px solid black;" >
<APPLET CODE="HelloWorld.class"  ARCHIVE="HelloWorld.jar" WIDTH="800" HEIGHT="800">
</APPLET>
</div>
</BODY>
</HTML>

The above works fine when I open the html in browser installed in my machine.

However, when I try to access it from a different machine, it throws an error.

Caused by: java.lang.UnsatisfiedLinkError: Can't load library: /var/www/libHelloWorld.so

I have tried searching the solution but found nothing helpful.

Upvotes: 0

incoherency
incoherency

Reputation: 121

Please, take a look to this thread. It seems to cover your question: Calling a DLL from an Applet via JNI

Note: You should just replace '.dll' file by your '.so' file (and file path OS relative formatting).

Upvotes: 1

Related Questions