Name: gb36485 Date: 02/10/99
My plateform is NEC company's EWS. My java version is 1.1.6. If you want to reproduct the problem, please just compile the following program as indicated. The erro message is :
SEGMENT violence
Followed is java program:
import java.lang.*;
public class testnative
{
/**
* declare native method
*/
public native String native_jnitest(int MethodId,String strPropeties);
/**
* load library
*/
static
{
try
{
System.load("/seagate/Filter/baij/jninative/libtest.so");
}
catch(UnsatisfiedLinkError e)
{
System.out.println("load library error");
}
}
}
Followed is C++ program:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include "testnative.h"
/**#include "corba.h"*/
extern "C"
jstring CStringToJava(JNIEnv *env, char *pSrc, int sizeSrc)
{
jstring jstrRet;
jclass jclsString;
jmethodID jid;
jbyteArray jarySrc;
/**
* create a new array object
*/
jarySrc = env->NewByteArray(sizeSrc);
if( jarySrc==NULL ) return(NULL);
/**
* create a new array object
*/
env->SetByteArrayRegion(jarySrc, 0, sizeSrc, (jbyte *)pSrc);
/**
* loads a locally-defined classt
*/
jclsString = env->FindClass("java/lang/String");
if( jclsString==NULL ) return(NULL);
/**
* get the method ID for an instance (nonstatic) method of a class or interface
*/
jid = env->GetMethodID(jclsString, "<init>", "([BII)V");
if( jid==NULL ) return(NULL);
/**
* get new object of java
*/
printf("The next line is jstrRet = (jstring)env->NewObject(jclsString, jid, jarySrc, 0, sizeSrc\n")
jstrRet = (jstring)env->NewObject(jclsString, jid, jarySrc, 0, sizeSrc);
return(jstrRet);
}
char *JavaStringToC(JNIEnv *env, jstring jstrSrc)
{
char *pRet;
jmethodID jid;
jbyteArray jarySrc;
jsize jsizeSrc;
/**
* get the method ID for an instance (nonstatic) method of a class or interface
*/
jid = env->GetMethodID(env->GetObjectClass(jstrSrc), "getBytes", "()[B");
if( jid==NULL ) return(NULL);
/**
* call a Java instance method from a native method.
*/
jarySrc = (jbyteArray)env->CallObjectMethod((jobject)jstrSrc, jid);
/**
* get the number of elements in the array.
*/
jsizeSrc = env->GetArrayLength(jarySrc);
pRet = (char *)malloc((unsigned int)(jsizeSrc+1));
if( pRet==NULL ) return(NULL);
/**
* copies a region of a primitive array into a pRet buffer.
*/
env->GetByteArrayRegion(jarySrc, 0, jsizeSrc, (jbyte *)pRet);
pRet[jsizeSrc] = 0;
return(pRet);
}
JNIEXPORT jstring JNICALL Java_testnative_native_1jnitest(JNIEnv *env, jobject jobj, jint jmetid, jstring propeties)
{
char *pStr=NULL;
jstring jstrResult;
/**corbatest cort;*/
/**
* java string to C
*/
printf("java String to C--\n");
pStr=JavaStringToC(env, propeties);
printf("java to c string oki\n");
if( pStr==NULL ) return(NULL);
printf("the propeties is %s\n",pStr);
/**
* call corba interface
*/
/**cort.testcorba(jmetid,&pStr);*/
printf("result is %s\n",*pStr);
/**
* c string to java
*/
printf("c string to java\n");
jstrResult=CStringToJava(env,pStr,strlen(pStr));
printf("c string to java ok\n");
if( jstrResult==NULL ) return(NULL);
return jstrResult;
}
Followed is java have main method:
import java.lang.*;
class Test
{
public static void main(String args[])
{
int intMethod;
String strPropeties;
if(args.length!=2)
{
System.out.println("java Test methodId propeties");
System.exit(0);
}
/**
* check parameter
*/
intMethod=Integer.valueOf(args[0]).intValue();
if(intMethod<1 || intMethod>6)
{
System.out.println("method parameter error,reason---method id is wrong");
System.exit(0);
}
strPropeties=args[1];
if(strPropeties==null)
{
System.out.println("method parameter error,reason---propeties is wrong");
System.exit(0);
}
/**
* call native method
*/
System.out.println("create a native object");
testnative testn=new testnative();
System.out.println("call native method");
String strResult=testn.native_jnitest(intMethod,strPropeties);
System.out.println("the return string is "+strResult);
}
}
(Review ID: 54000)
======================================================================