0

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
nits41089
  • 34
  • 6

2 Answers2

1

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

Community
  • 1
  • 1
incoherency
  • 121
  • 1
  • 6
  • Thanks a lot. However the applet is showing blank on the browser. I have checked java console but no error is present. – nits41089 Jan 23 '15 at 14:47
  • The issue has been fixed. I was using System.out.println to display in applet, but System.out.println displays the message in java console and not in the browser. To display the message in browser, I used drawString method of the Graphics class. However, there is one last issue, the code works properly in my machine but when I use the URL in some other machine it throws an error. Caused by: java.lang.UnsatisfiedLinkError: Can't load library: /var/www/libHelloWorld.so. Can anyone help me with this? – nits41089 Jan 27 '15 at 07:15
0

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.

nits41089
  • 34
  • 6