Solved

How to make an applet works with JNI?

Posted on 1998-02-11
8
694 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

726 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