If I use "JSP -> Java -> JNI" ?

When the Java -> JNI -> C is writen and tested OK.How can I use JSP to import the Class into JSP page?I tried. But failure.Have someone show a sample? help~ Q_Q
[208 byte] By [cofensen] at [2007-9-26 4:00:17]
# 1

Depends on how your Java wrapper is implemented. However..

You can create a JavaBean that wraps your Java native interface class and exposes the interface via suitable methods.

Creating your bean simply becomes a matter of

<jsp:useBean id="myJNIBean" class="your.fullyqualified.MyJNIBean" scope="page|request|session|application" />

in the jsp you can then access the bean so..

<%

myJNIBean.someMethod(someParam);

// bean wraps the JNI up, someMethod exposes the native method call

%>

andyba at 2007-6-29 12:55:06 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

Thanks, I try to do this

all put in the JavaBean, but it have an error shown below

(It's strange.. why can't call my JNI method...help~~)

-

500 Internal Server Error

java.lang.NoClassDefFoundError

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Unknown Source)

at com.evermind.server.http.HttpApplication.v6(JAX)

at com.evermind.server.http.ei.tx(JAX)

at com.evermind.server.http.ei.s_(JAX)

at com.evermind.server.http.JSPPage.s_(JAX)

at com.evermind.server.http.HttpApplication.wh(JAX)

at com.evermind.server.http.HttpApplication.xj(JAX)

at com.evermind.server.http.JSPServlet.service(JAX)

at com.evermind.server.http.ea.doFilter(JAX)

at com.orionsupport.cocoon.CocoonFilter.doFilter(CocoonFilter.java:65)

at com.evermind.server.http.d3.sw(JAX)

at com.evermind.server.http.d3.su(JAX)

at com.evermind.server.http.ef.s1(JAX)

at com.evermind.server.http.ef.do(JAX)

at com.evermind.util.f.run(JAX)

cofensen at 2007-6-29 12:55:06 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3

You have to make sure that both the bean AND the wrapper class are on the web apps classpath.

If your servlet engine is J2EE compliant (Tomcat, JRun are for example) you can simply copy the class directories and files into the WEB-INF/classes directory and restart the server.

Your native library must also be available to the web application (read documentation about how to do this).

Lastly, check that your code correctly names the methods to be called and their signatures (the parameter lists for them). Also, make sure that the native methods are publicly accessible

ie public native methodName....

Judging by the server error being thrown your native wrapper class is not on the web applications classpath, you can make it available as described above.

There is another way to deploy your wrapper classes, if they are packed in a jar file, place the jar file in the WEB-INF/lib directory and restart the server. Any jar files in the lib directory will be automatically picked up when the server starts.

andyba at 2007-6-29 12:55:06 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4

Thanks you again... ^_^ to help

All My step and files are shown below:

--[1 step]--[ JSP file ]--

<jsp:useBean id="myBean" scope="page" class="Counter"/>

<jsp:getProperty name="myBean" property="count"/>

--[2 step][ write JavaBean ]--

public class Counter{

public Counter(){}

public native String printt(); // It's Native entry

public static void main(String[] args){ // This just for Java in DOS runable.

new Counter().printt();

}

public String getCount(){

return(new Counter().printt());

@@@@@@@@@@@@@@@@ <= the server will call getCount() method, which the DLL's create the printt() method.it may cause the 1st errors because can't find printt().I guess.

}

public void setCount(int newCount){}

static{

System.loadLibrary("HelloWorld"); //LOAD the DLL's from C create

}

}

[3 step] [ Create Counter.h ] -- CMD:[ javah - jni Counter ]

/* DO NOT EDIT THIS FILE - it is machine generated */

#include <jni.h>

/* Header for class Counter */

#ifndef _Included_Counter

#define _Included_Counter

#ifdef __cplusplus

extern "C" {

#endif

/*

* Class: Counter

* Method: printt

* Signature: ()Ljava/lang/String;

*/

JNIEXPORT jstring JNICALL Java_Counter_printt

(JNIEnv *, jobject);

#ifdef __cplusplus

}

#endif

#endif

-[4 step] [Write HelloWorld.c ]-

#include <jni.h>

#include <stdio.h>

#include "Counter.h"

JNIEXPORT jstring JNICALL

Java_Counter_printt(JNIEnv *env,jobject this){

printf("Hello World!\n hihihi success!");

return;

}

[5 step]-- [use VC++ 5 to create the DLL from HelloWorld.c ]

[6 step] -- The result that Server says -

500 Internal Server Error

java.lang.NoClassDefFoundError

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Class.java:195)

...

cofensen at 2007-6-29 12:55:07 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 5

A JavaBean -must- implement the java.io.Serializable interface so public class Counter should read public class Counter implements java.io.Serializable {...

Apart from that your code looks fine. The exception that causes the server error indicates that the class cannot be found by the server (check the documentation for what NoClassDefFoundError means and what it implies).

Make sure that the server has all your classes available to it on the correct classpath as I defined before

andyba at 2007-6-29 12:55:07 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 6

Thanks for your answer.

Before combine the JavaBean + JNI way.

I have tried to use Servlet + JNI. And they have the same error!

For a long time I can't solve the Servlet + JNI error.

I turn to use JavaBean + JNI...but STILL can't solve...help~~

they all the same error. so strange...

--

500 Internal Server Error

java.lang.NoClassDefFoundError

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Class.java:195)

...

cofensen at 2007-6-29 12:55:07 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 7
In the DOS.Java can run my JavaBean (Because I have the "main" method in it).When I put the Bean in server. then the errors will appear like back one of the topic.my god.... help~~
cofensen at 2007-6-29 12:55:07 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 8
I keep telling you what the problem is, the web server cannot find the ALL the classes for your JNI wrapper and/or JavaBean. I repeat, look at the definition of the Error thrown by the server, it gives you a big clue.
andyba at 2007-6-29 12:55:07 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 9

Thanks so much!!!!! I find the error.

You are currectly right~ thanks you

Because I use Windows 2000 pro.

so I must put the DLL file in the "\win2000\system32\"

and the Server is loading the DLL file

(I try to DELETE DLL file, then can't. It's means java lock the file.)

But new question is comming~

I can't see anything in the browser...

cofensen at 2007-6-29 12:55:07 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 10
I success!I can doing the JSP -> JavaBean -> JNI !!Thanks a lot for helping my doing the programming thinking past 3 months. ^_^
cofensen at 2007-6-29 12:55:07 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 11
can u mail me how you have done that...I am trying on weblogic on win2k but failed...my mail-id is malu4ram@yahoo.com Thanks a lot.
malu4ram at 2007-6-29 12:55:07 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 12

Hello

Currently i am facing the same problem as you did before.

You said you solve the prob by declaring the path into win2000/win32....

How and where did you declare that? because i am using windows XP..

any suggestion will be highly appreciated.

Thank you,

Suwandy

ps: if its not troubling you, would you be kind enuff to email it to me at suwandy82@hotmail.com

Thank you

Suwandy_wong at 2007-6-29 12:55:07 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 13

Hi Suwandy,

I am also using Windows XP and Tomcat.

All that you need to do is

SET LIB_PATH=%LIB%;<Dir. of where you put the DLL in the webapp of Tomcat>

Use this while starting your Tomcat. i.e. in the catalina.bat

Append this path -Djava.library.path=%LIB_PATH%

I have tried the same in Weblogic also.

You have to append the same to the startWeblogic.cmd

It works for me.

Kindly let us know if you still face problems.

Dhamo.

LathaDhamo at 2007-6-29 12:55:07 > top of Java-index,Java HotSpot Virtual Machine,Specifications...