How to make an applet works with JNI?

Dear Experts

I have an simple applet that construct a class that developed with C++. This class (C++) only print some message to screen using printf command.

Next, I compile the applet and produce *.class file.
I use javah utility to make header file.
Using Visual C++ v5.0, I create a *.dll file.
I store this file in same directory with *.class file.

Next, I make a jar file from this *.class to make it signed
I sign the jar file using javakey command

I test on appletviewer and it is working properly
Next I try using Hotjava 1.1 and see the result in HotJava Console. It is not working!

The exception thrown is:
java.lang.UnsatisfiedLinkError: hello

      at HelloApplet.init(HelloApplet.java:16)

      at sunw.hotjava.applet.AppletPanel.processEvent(AppletPanel.java:320)

      at sunw.hotjava.applet.AppletPanel.run(AppletPanel.java:293)

      at java.lang.Thread.run(Thread.java:474)


I have read the documentation on package index of JDK and this exception was produced because it can not find the *.dll file, but I store the file in the same directory with my *.jar file. I have tried to include this *.dll on my *.jar file but it still doesn't work.

I will be glad to hear your opinion and solution.
Thank you for your attention.

Regards
Diamondo
PT. Sigma Cipta Caraka
Jakarta - Indonesia
diamondoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

diamondoAuthor Commented:
Edited text of question
0
jpk041897Commented:
The way the OS looks for a DLL is diffrent from the way that JAVA looks for class files.

If your problem is that the server cannot find the DLL then the most likley source of your problem is the PATH (not the CLASSPATH) environment variable.

The OS will look for a DLL in the same directory as the running application that invokes it first (The JAVA VM in this case) and then the path.

The way you can work arround the problem is to either place the DLL in a directory that allready appera in the PATH variable, or to add the directory where you are placing the DLL to the PATH and rebooting.

You could also place the DLL in the same directory as java.exe but this is a messy solution and should be avoided.

Let me know if this solves your problem. If it doesn't we can look at some other alternatives.
0
diamondoAuthor Commented:
Yes, it's working when I access the HTML pages on the same machine with the *.dll files located. (PATH configured)

But when I move to another machine, use same browser and it throws same exceptions (UnsatisfiedLinkError: hello)

Is it means that in the client side have to install a *.dll file?
and configure PATH also?

Another issue is please review my source code:

// HelloApplet.java
import java.awt.*;
import java.applet.*;

public class HelloApplet extends Applet {

   public native void hello();
     
   static {
      System.loadLibrary("HelloApplet");
   }
   
   
   public void paint(Graphics g) {
        try {
            g.drawString( "Hello World 1", 10, 10 );
            new HelloApplet().hello();
            g.drawString( "Hello World 2", 10, 20 );
        }
        catch (Exception e) {
            System.out.println( "Exception: " + e.toString() );
            g.drawString( "Exception: " + e.toString(), 10, 30);
        }
   }
}

/* HelloApplet.cpp */
#include<jni.h>
#include<stdio.h>
#include"HelloApplet.h"

JNIEXPORT void JNICALL Java_HelloApplet_hello(JNIEnv * env, jobject obj) {
   printf("Hello! This is my first Applet using Native Methods!\n");
}

The problem is: The Browser prints Hello World 1 and Hello World 2 successfully without throws any exceptions, but when I see the HotJava Console Window, I cannot find "Hello! This is my first Applet Using Native Methods!" message.

So Is Native Code executed or not?

Thank you for your advice, I'm realy appreciated.

Regards
Diamondo


0
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

diamondoAuthor Commented:
Addition: I try using appletviewer and it's working perfectly.
          I see my Hello World 1 and Hello World 2 on the appletviewer window and my "Hello! This is my first..." on the DOS Prompt Window.

Once again, thank you very much.
0
jpk041897Commented:
If I uderstand what you are doing correctly then yes, the .dll file must be installed in the clients machine. It must also be installed in a directory that is included in the client machines PATH or whoms directory is precisley specified  in the System.loadLibrary() call, for instance:

System.loadLibrary("C:\\Netscape\\Java\\HelloApplet");

Of course, this implies that the dll is actualy stored at that location.

How you actualy deploy the dll is a matter of choice. The most common way of acomplishing it is to include the dll in a signed jar file. In this fashion you can download the .dll to your local browser cache directory as a trusted file and avoid several security box issues that can cause problems for you in more complicated JNI dll's.

The problems with this approach are that you need a class 2 or better Verisign signature and that the procedure to actualy sign the JAR file changes from browser to browser.

Another option of course, is to use a setup program and have the user download and execute it before loading your pages. This has the disadvantages of forcing your user to take extra steps in loading your product plus making upgrades much more disfficult.

Other alternatives include the use of server side includes, servlets or making socket calls to the server in order to call the DLL at the server or using either RMI or CORBA to make the call. Each one has its own set of advantages and disadvantages from increased networking traffic to more complicated programming and are not realy recomended for an applet the size of the one you are writting.

The main point though is that the client must either have the DLL file stored localy or you must establish a mechanism by which the client can either pre-load the DLL file or access it via remote proccesing of some kind.



0
diamondoAuthor Commented:
I have a copy of DLL file and store in particular directory on client machine
I have added that directory to PATH variable on client machine

When I try to load an applet (hotjava browser and appletviewer) on the server, it still throws UnsatisfiedLinkError exception

Could you explain something wrong with this?
I put the applet on jar files and signed with javakey.


0
ashishagarwalCommented:
have you set up up Library path ?????

you need to set it up:

on unix it is:
%setenv LD_LIBRARY_PATH mylibrarypath

similarly you can set it ofr windows!!

hope it helps
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
diamondoAuthor Commented:
Yes I did in Windows environment.
Based on jpk's answer, I added DLL file location to my PATH variable but still doesn't work. Any other idea?

Point to tutorial is appreciated, Thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.