Solved

How to make an applet works with JNI?

Posted on 1998-02-11
8
680 Views
Last Modified: 2013-11-23
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
0
Comment
Question by:diamondo
  • 5
  • 2
8 Comments
 

Author Comment

by:diamondo
ID: 1233012
Edited text of question
0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1233013
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
 

Author Comment

by:diamondo
ID: 1233014
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
 

Author Comment

by:diamondo
ID: 1233015
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 6

Expert Comment

by:jpk041897
ID: 1233016
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
 

Author Comment

by:diamondo
ID: 1233017
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
 
LVL 1

Accepted Solution

by:
ashishagarwal earned 100 total points
ID: 1233018
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
 

Author Comment

by:diamondo
ID: 1233019
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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now