JNI and Web Services

H

hogcia

I have a *BIG* problem concerning callin a C++ .so library function
from a java Web Service (I am using JNI). Every time I start the Web
Service client I get a following error:

javax.xml.ws.soap.SOAPFaultException: Unknown fault type:class
java.lang.UnsatisfiedLinkError
at
com.sun.xml.internal.ws.fault.SOAP11Fault.getProtocolException(SOAP11Fault.java:
171)
at
com.sun.xml.internal.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:
94)
at
com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:
240)
at
com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:
210)
at
com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:103)
at $Proxy28.p(Unknown Source)
at p_versionclient.Main.main(Main.java:31)

as if the function wasn't there! And it is!
Not to mention that when I call the function from a regular java
program (with a main function) OR a C program - it works! I have no
idea what's going on...
The regular java program looks like this:

//Web_Serwis.java
public class Web_Serwis
{

public native String
p();

public static void main(String[] args)
{
System.load("/u/agata/ala/
libmy.so");
Web_Serwis serwis = new
Web_Serwis();

String version =
serwis.p();

System.out.println("wersja=" +
version);
}
}

This is the header file:

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

#include
<jni.h>
/* Header for class Web_Serwis
*/
char
*p_version_c();
#ifndef
_Included_Web_Serwis
#define
_Included_Web_Serwis
#ifdef
__cplusplus
extern
"C" {
#endif

/
*
* Class:
Web_Serwis
* Method:
p
* Signature: ()Ljava/lang/
String;

*/
JNIEXPORT jstring JNICALL
Java_Web_1Serwis_p
(JNIEnv *,
jobject);

#ifdef
__cplusplus
}
#endif
#endif

And this is the C++ program (p_version_c() is a function from another
library that I link to this one):
//Web_Serwis.cc
#include
"Web_Serwis.h"
#include
<string>

JNIEXPORT jstring JNICALL
Java_Web_1Serwis_p
(JNIEnv *env, jobject
obj)
{
const char *str =
p_version_c();
if(str ==
NULL)

{
return (*env).NewStringUTF("ERROR: p_version_c()
returned NULL")
}
return
(*env).NewStringUTF(str);
}

Oh, and this is how I compile and run the program:
g++ -shared -m32 -c -g3 -gdwarf-2 -I/opt/SDK/jdk/include/linux -I/opt/
SDK/jdk/include -o Web_Serwis.o Web_Serwis.cc
g++ -shared -m32 -o libmy.so Web_Serwis.o /u/adm/bin.pg8.2/o/za/kk.o /
u/adm/bin.pg8.2/o/za/m_action.o /u/adm/bin.pg8.2/ar/k
/opt/jdk1.6/bin/java Web_Serwis


So far it works. Now I make a similar Web Service, exposing the call
to the library function (p() calls p_version_c()):

//p_versionSerwer.java
package ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

/**
*
* @author agata
*/
@WebService()
public class p_versionSerwer {
static
{
try {

System.out.println ("loading library...");
System.load("/u/agata/ws/libp.so");
}
catch (Exception e) {
System.out.println ("Can't load library!!!");
e.printStackTrace();
}
catch (Error err) {
System.out.println ("ERROR: Can't load
library!!!");
err.printStackTrace();
}
}
/**
* Web service operation
*/
@WebMethod
public native String p();
}

I create a header file:
//p_version.h
/* DO NOT EDIT THIS FILE - it is machine generated
*/
#include
<jni.h>
/* Header for class ws_p_versionSerwer
*/
char *
p_version_c();
#ifndef
_Included_ws_p_versionSerwer
#define
_Included_ws_p_versionSerwer
#ifdef
__cplusplus
extern
"C" {
#endif
/
*
* Class:
ws_p_versionSerwer
* Method:
p
* Signature: ()Ljava/lang/
String;

*/
JNIEXPORT jstring JNICALL
Java_ws_p_1versionSerwer_p
(JNIEnv *,
jobject);

#ifdef
__cplusplus
}

#endif
#endif

And a C++ file that calls p_version_c():
// p_version.cc
#include
"p_version.h"
#include
<string>

JNIEXPORT jstring JNICALL
Java_ws_p_1versionSerwer_p
(JNIEnv * env, jobject
obj)
{
const char *str =
p_version_c();

if(str==NULL)
Ireturn (*env).NewStringUTF("ERROR: funkcja p_version_c zwrocila
NULL");
return
(*env).NewStringUTF(str);
}

Finally I compile the files with other library files into libp.so:
g++ -shared -m32 -g3 -gdwarf-2 -I/opt/SDK/jdk/include/linux -I/opt/SDK/
jdk/include -o p_version.o p_version.cc
g++ -shared -m32 -o libp.so p_version.o /u/adm/bin.pg8.2/o/za/kk.o /u/
adm/bin.pg8.2/o/za/m_action.o /u/adm/bin.pg8.2/ar/k


And run a simple client:
//Main.java
package p_versionclient;

/**
*
* @author agata
*/
public class Main {

/** Creates a new instance of Main */
public Main() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

try { // Call Web Service Operation
cli.PVersionSerwerService service = new
cli.PVersionSerwerService();
cli.PVersionSerwer port = service.getPVersionSerwerPort();
// TODO process result here
java.lang.String result = port.p();
System.out.println("Result = "+result);
} catch (Exception ex) {
ex.printStackTrace();
}
}

}

And that's when I get the error message... I've been trying to get
this to work for a week now, so I would really appreciate some help
(maybe a way to check where's the problem or a way to get an error
message, that actually SAYS something) .
I'm running this Web Service on a remote server, if that's of any
significance.

Thanks in advance! :)
 
J

joerg

hogcia said:
...
javax.xml.ws.soap.SOAPFaultException: Unknown fault type:class
java.lang.UnsatisfiedLinkError
at
com.sun.xml.internal.ws.fault.SOAP11Fault.getProtocolException(SOAP11Fault..java:
171)
...

This looks like a problem with the library path.
The shared object containing the native code must be located
in a path that is listed in LD_LIBRARY_PATH or java.library.path
(asuming your code runs on linux).

This problem can also occur when trying to load a 32bit
library into a 64bit jre.
Check the java runtime used by the WebService. If this is
a 64bit jre, the library needs to be compiled as 64bit.

--
Jörg Matysiak
Software Development Systems Management
CENIT AG Systemhaus
http://www.cenit-group.com

+++ Newsticker +++ aktuelle Veranstaltungen unter http://www.cenit.de/events
+++ Newsticker +++
 
H

hogcia

This looks like a problem with the library path.
The shared object containing the native code must be located
in a path that is listed in LD_LIBRARY_PATH or java.library.path
(asuming your code runs on linux).
I would not agree:
There are two different ways to load a native library into a running
Java program: System.loadLibrary(String) and System.load(String). The
System.loadLibrary method allows us to load a library from the
"default" path. System.load allows us to load a library from anywhere
via its absolute path.
This problem can also occur when trying to load a 32bit
library into a 64bit jre.
Check the java runtime used by the WebService. If this is
a 64bit jre, the library needs to be compiled as 64bit.
This is definetly not an issue.

My colleague solved this problem by adding some symbolic links to
those other libraries linked to libp.so.

Thanks for your answer!
 
L

Lew

hogcia said:
This is definetly not an issue.

Please do not multi-post. It fragments the conversation and annoys the folks
with the most to offer in the way of help. Te FAQ post by David Alex Lamb has
pointers to sites that give such advice, and it's posted every five days so
you won't have trouble finding it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top