Results 1 to 15 of 15
  1. #1
    Sarkhan is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Post Use JNI in Web Services

    Hi everybody.
    I have already tested JNI on Desktop application with successfully.
    Now,I want to use JNI in Java Web Service With Glassfish 2.1v .
    But I can't find any book or documentation about this topic.
    Who can help me ?


    And It is my web service code .
    Java Code:
    @WebService()
    @Stateless()
    public class FMServices {
    static boolean loaded = false;
    public native String stringMethod(String text);
    
    /**
    * Web service operation
    */
    
    @WebMethod(operationName = "getPositionfromCpp")
    public String getPositionfromCpp(@WebParam(name = "txt")
    String txt) {
    if(!loaded) {
    System.loadLibrary("Sample1");
    loaded = true;
    }
    String text = stringMethod("JAVAxxxxxx");
    return text;
    }
    }
    Last edited by Eranga; 01-11-2012 at 06:10 AM. Reason: code tags added

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Sarkhan is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default Re: Use JNI in Web Services

    Of course but I get such errors when I use System.loadLibrary("Sample1");
    Caused by: java.lang.UnsatisfiedLinkError: no Sample1 in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java :1709) at java.lang.Runtime.loadLibrary0(Runtime.java:823) at java.lang.System.loadLibrary(System.java:1030) at FGis.FMServices .getPositionfromCpp(FMServices .java:31) at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25) at

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Re: Use JNI in Web Services

    UnsatisfiedLinkError, so are you sure that the library names 'Sampl1' is in the relevant location?

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Re: Use JNI in Web Services

    To confirm the library path simple comment out the LoadLibrary method and run your code. You may add a println to check it.

  6. #6
    Sarkhan is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default Re: Use JNI in Web Services

    Hi again. I haven't solved this problem jet.
    After putting Sample1.dll into the glassfish lib directory (C:\Sun\AppServer\lib) already library loading.
    But now I have another problems. Now, when I want to access native metod I get below errors .

    Caused by: java.lang.UnsatisfiedLinkError: services.Sample1.intMethod(I)I at services.Sample1.intMethod(Native Method) at services.Sample1.resultcpp(Sample1.java:25) at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.sun.enterprise.security.application.EJBSecurit yManager.runMethod(EJBSecurityManager.java:1011) at com.sun.enterprise.security.SecurityUtil.invoke(Se curityUtil.java:175) at com.sun.ejb.containers.BaseContainer.invokeTargetB eanMethod(BaseContainer.java:2929) at com.sun.ejb.containers.BaseContainer.intercept(Bas eContainer.java:4020) at com.sun.ejb.containers.WebServiceInvocationHandler .invoke(WebServiceInvocationHandler.java:190)

    Realy ,anywhere no any documentation about jni and web services?

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default Re: Use JNI in Web Services

    Whether it's a web service or not should have no effect on how you write your code.
    Your native code needs to be on the correct path (there's a -Djava.library.path option you need to use I think). That's all.

  8. #8
    Sarkhan is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default Re: Use JNI in Web Services

    Is it possible generally (glassfish and jni )?
    Some programmers say that it is impossible.
    Even you can do it,you will meet with many problems.

  9. #9
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default Re: Use JNI in Web Services

    Why would it be impossible?
    You can do JNI calls in Tomcat, so what do you think it is about Glassfish that would make it impossible to do?

  10. #10
    Sarkhan is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default Re: Use JNI in Web Services

    I must use only glassfish.Do you have any web servise example which used jni library(with glassfish )?

  11. #11
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default Re: Use JNI in Web Services

    This is probably the most relevant post at StackOverflow, though the jumping through hoops is largely down to redeployment.

  12. #12
    arielb is offline Member
    Join Date
    Jan 2012
    Location
    Panamá
    Posts
    41
    Rep Power
    0

    Default Re: Use JNI in Web Services

    You should check the java.library.path environment variable and put the dll in this option.
    If 32-bit windows you can place it in c: \ windows \ system32, if 64-bit must be either in c: \ windows \ system32 and in c: \ windows \ syswow64.

    That error is because you do not find the dll

  13. #13
    Sarkhan is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default Re: Use JNI in Web Services

    arielb, all is ok as you say. All is well with java desktop application.
    and this is my full source code.

  14. #14
    Sarkhan is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default Re: Use JNI in Web Services

    java desktop application code
    public class Sample1
    {
    public native int intMethod(int n);
    public native boolean booleanMethod(boolean bool);
    public native String stringMethod(String text);
    public native int intArrayMethod(int[] intArray);

    public static void main(String[] args)
    {
    System.loadLibrary("Sample1");
    Sample1 sample = new Sample1();
    int square = sample.intMethod(5);
    boolean bool = sample.booleanMethod(true);
    String text = sample.stringMethod("JAVA");
    int sum = sample.intArrayMethod(
    new int[]{1,1,2,3,5,8,13} );
    System.out.println("intMethod: " + square);
    System.out.println("booleanMethod: " + bool);
    System.out.println("stringMethod: " + text);
    System.out.println("intArrayMethod: " + sum);
    }
    }


    javah code

    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class Sample1 */

    #ifndef _Included_Sample1
    #define _Included_Sample1
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
    * Class: Sample1
    * Method: intMethod
    * Signature: (I)I
    */
    JNIEXPORT jint JNICALL Java_Sample1_intMethod
    (JNIEnv *, jobject, jint);

    /*
    * Class: Sample1
    * Method: booleanMethod
    * Signature: (Z)Z
    */
    JNIEXPORT jboolean JNICALL Java_Sample1_booleanMethod
    (JNIEnv *, jobject, jboolean);

    /*
    * Class: Sample1
    * Method: stringMethod
    * Signature: (Ljava/lang/String;)Ljava/lang/String;
    */
    JNIEXPORT jstring JNICALL Java_Sample1_stringMethod
    (JNIEnv *, jobject, jstring);

    /*
    * Class: Sample1
    * Method: intArrayMethod
    * Signature: ([I)I
    */
    JNIEXPORT jint JNICALL Java_Sample1_intArrayMethod
    (JNIEnv *, jobject, jintArray);

    #ifdef __cplusplus
    }
    #endif
    #endif


    and this is c++ code

    #include "Sample1.h"
    #include <string.h>
    JNIEXPORT jint JNICALL Java_Sample1_intMethod
    (JNIEnv *env, jobject obj, jint num) {
    return num * num;
    }
    JNIEXPORT jboolean JNICALL Java_Sample1_booleanMethod
    (JNIEnv *env, jobject obj, jboolean boolean) {
    return !boolean;
    }
    JNIEXPORT jstring JNICALL Java_Sample1_stringMethod
    (JNIEnv *env, jobject obj, jstring string) {
    const char *str = env->GetStringUTFChars(string, 0);
    char cap[128];
    strcpy(cap, str);
    env->ReleaseStringUTFChars(string, str);
    return env->NewStringUTF(strupr(cap));
    }
    JNIEXPORT jint JNICALL Java_Sample1_intArrayMethod
    (JNIEnv *env, jobject obj, jintArray array) {
    int i, sum = 0;
    jsize len = env->GetArrayLength(array);
    jint *body = env->GetIntArrayElements(array, 0);
    for (i=0; i<len; i++)
    { sum += body[i];
    }
    env->ReleaseIntArrayElements(array, body, 0);
    return sum;
    }
    void main(){}

    all is ok with java desktop application or with console.
    but web servise code can't return result .
    and this is my web servise code .

    package services;
    import javax.jws.WebMethod;
    import javax.jws.WebParam;
    import javax.jws.WebService;
    import javax.ejb.Stateless;
    /**
    *
    * @author xxxxxxx
    */
    @WebService()
    @Stateless()
    public class Sample1 {
    public native int intMethod(int n);
    public native boolean booleanMethod(boolean bool);
    public native String stringMethod(String text);
    public native int intArrayMethod(int[] intArray);
    /**
    * Web service operation
    */
    @WebMethod(operationName = "resultcpp")
    public String resultcpp(@WebParam(name = "txt")
    String txt){
    System.loadLibrary("Sample1");
    Sample1 sample = new Sample1();
    int square = sample.intMethod(5);
    boolean bool = sample.booleanMethod(true);
    String text = sample.stringMethod("JAVA");
    int sum = sample.intArrayMethod(
    new int[]{1,1,2,3,5,8,13} );
    // System.out.println("intMethod: " + square);
    // System.out.println("booleanMethod: " + bool);
    // System.out.println("stringMethod: " + text);
    // System.out.println("intArrayMethod: " + sum);
    return text;
    }

    }

  15. #15
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default Re: Use JNI in Web Services

    Did you read through the StackOverflow post I linked to above?
    That explains where to put things and why.

Similar Threads

  1. Web Services
    By nikosdi in forum Web Frameworks
    Replies: 3
    Last Post: 06-04-2011, 02:28 PM
  2. services.xml
    By madness in forum New To Java
    Replies: 2
    Last Post: 01-06-2011, 01:31 PM
  3. web services
    By k_paramesh82 in forum Java Software
    Replies: 5
    Last Post: 04-02-2009, 01:03 PM
  4. Web Services: Axis vs Metro vs Spring Web Services
    By davidbaker29 in forum Advanced Java
    Replies: 0
    Last Post: 01-28-2009, 12:20 PM
  5. Web services
    By Sharmistha in forum Web Frameworks
    Replies: 0
    Last Post: 06-02-2008, 02:06 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •