- 论坛徽章:
- 0
|
AIX5.3 下,C如何创建Java 虚拟机。
下面的c代码编译通过,但是运行时,创建Java虚拟机失败。
#include <jni.h>
#include <stdlib.h>
#include <string.h>
#include "SignedData.h"
static JavaVM* s_jvm=0;
static JNIEnv* s_env=0;
/*
返回0 成功
*/
int InitJavaVM()
{
int res;
JavaVMInitArgs vm_args= {0};
JavaVMOption options[2]= {0};
if(s_jvm && s_env)return 0;
options[0].optionString = "-Djava.compiler=NONE";
options[1].optionString = "-Djava.class.path=secuInter.jar";
vm_args.version =JNI_VERSION_1_4;
vm_args.nOptions = 2;
vm_args.options = options;
vm_args.ignoreUnrecognized = JNI_TRUE;
res = JNI_CreateJavaVM(&s_jvm,(void**)&s_env,&vm_args);
if (res < 0) {
printf("Can't create Java VM\n" ;
return -1;
}
return 0;
}
void ReleaseJavaVM()
{
if(s_jvm)
(*s_jvm)->DestroyJavaVM(s_jvm);
s_jvm=0;
s_env=0;
}
typedef char* pchar;
char** Verify(const char *signeddata)
{
jclass sdata_class= 0;
jmethodID mid= 0;
jstring jstr= 0;
jobjectArray jj= 0;
int len= 0;
pchar* result= 0;
int i= 0;
if(!s_env)return 0;
sdata_class=(*s_env)->FindClass(s_env,"net/netca/secuInter/SignTools" ;
if(sdata_class==0){
printf("class not found!\n" ;
return 0;
}
mid= (*s_env)->GetStaticMethodID(s_env,sdata_class,"verify","(Ljava/lang/String [Ljava/lang/String;" ;
if(mid==0){
printf("method not found!\n" ;
return 0;
}
jstr = (*s_env)->NewStringUTF(s_env,signeddata);
jj = (jobjectArray)(*s_env)->CallStaticObjectMethod(s_env,sdata_class,mid,jstr);
len= (*s_env)->GetArrayLength(s_env,jj);
result=(pchar*)malloc(sizeof(pchar)*len);
for(i=0;i<len;i++)
{
jstring js= 0;
const char* tmp = 0;
js=(jstring)(*s_env)->GetObjectArrayElement(s_env,jj,i);
tmp = (*s_env)->GetStringUTFChars(s_env,js,0);
result[i] = (pchar)malloc(strlen(tmp)+1);
strcpy(result[i],tmp);
(*s_env)->ReleaseStringUTFChars(s_env,js,tmp);
}
return result;
}
char** Decrypt(const char *decdata)
{
jclass sdata_class= 0;
jmethodID mid= 0;
jstring jstr= 0;
jobjectArray jj= 0;
int len= 0;
pchar* result= 0;
int i= 0;
if(!(*s_env))return 0;
sdata_class=(*s_env)->FindClass(s_env,"net/netca/secuInter/SignTools" ;
if(sdata_class==0){
printf("class not found!\n" ;
return 0;
}
mid= (*s_env)->GetStaticMethodID(s_env,sdata_class,"decrypt","(Ljava/lang/String [Ljava/lang/String;" ;
if(mid==0){
printf("method not found!\n");
return 0;
}
jstr= (*s_env)->NewStringUTF(s_env,decdata);
jj= (jobjectArray)(*s_env)->CallStaticObjectMethod(s_env,sdata_class,mid,jstr);
len = (*s_env)->GetArrayLength(s_env,jj);
result=(pchar*)malloc(sizeof(pchar)*len);
for(i=0;i<len;i++)
{
jstring js= 0;
const char* tmp =0;
js=(jstring)(*s_env)->GetObjectArrayElement(s_env,jj,i);
tmp = (*s_env)->GetStringUTFChars(s_env,js,0);
result[i] = (pchar)malloc(strlen(tmp)+1);
strcpy(result[i],tmp);
(*s_env)->ReleaseStringUTFChars(s_env,js,tmp);
}
return result;
}
static char* p_plain="test";
static char* p="other";
int main()
{
char** p_r1= NULL;
char** p_r2= NULL;
if(InitJavaVM())return -1;
p_r1= Decrypt(p);
printf("返回信息:%s\n",p_r1[0]);
printf("返回数据:%s\n",p_r1[1]);
if(strcmp(p_r1[0],"0")!=0) return 0;
p_r2=Verify(p_r1[1]);
printf("返回信息:%s\n",p_r2[0]);
printf("返回数据:%s\n",p_r2[1]);
free(p_r1[0]);
free(p_r1[1]);
free(p_r1);
free(p_r2[0]);
free(p_r2[1]);
free(p_r2);
ReleaseJavaVM();
return 0;
} |
|