第一个 JNI_CreateJavaVM函数创建java虚拟机
1、classpath环境变量设置:如果机器上安装java环境,则可以getenv读出来。也可以自己指定。
主要是 jar包。注意不能单纯指定路径
2、classpath设置好了,path变量也要配置好,否则要返回-1。如果系统是自己指定目录,必须按照一
定 目录 结构,因为java是相对寻找文件的。一般按照如下
\run\
--bin
--classic //该目录下存放执行文件,和编译好*.class文件。动态库jvm.dll
--hpi.dll ioser12.dll java.dll net.dll verify.dll zip.dll
--lib //jar文件
--rt.jar tools.jar 如果要其它包可都放在这里
第二个:
JNI_CreateJavaVM 一个进程内只能调用一次,线程内JNI_GetCreatedJavaVMs获得JavaVM,
然后m_jvm->AttachCurrentThread获得 JNIEnv 环境 .软件退出时候
m_jvm->DetachCurrentThread(); m_jvm->DestroyJavaVM()可以不用
//以下是我工作中的使用情况
///*.h
#pragma once
#include
class CCCallJava
{
public:
CCCallJava(void);
~CCCallJava(void);
int init_env(char *classPath);
void destroy_env();
JNIEnv* getEvn();
JavaVM* getJvm();
char* getClassPath();
int SendZipFile(char *szUrl,char* szService,char*file);
char *jstringToWindows( JNIEnv *env, jstring jstr );
const char* getRestult();
void GetJvmEnv(char *classpath);
protected:
JNIEnv *m_env;
JavaVM *m_jvm;
char m_szClassPath[4096];
CString m_strResult;
};
///////////*.cpp////////////////////////////////
#include "StdAfx.h"
#include ".\ccalljava.h"
#include "LogRecorder.h"
CCCallJava::CCCallJava(void):m_env(NULL),m_jvm(NULL)
{
}
CCCallJava::~CCCallJava(void)
{
}
int CCCallJava::init_env(char *classPath)
{
if(m_jvm!=NULL)
{
m_Logrecorder.WriteLogF("java 已经初始化");
return 0;
}
jint res;
//取得系统环境
/*char*p =getenv("CLASSPATH");
if(p==NULL)
{
m_Logrecorder.WriteLogF("getenv 异常");
return -1;
}*/
memset(m_szClassPath,0,sizeof(m_szClassPath));
//if(classPath!=NULL)
// sprintf(m_szClassPath,"\-Djava.class.path=%s;%s",p,classPath);
//else
// sprintf(m_szClassPath,"\-Djava.class.path=%s",p);
//设置classpath路径
CString strClassPathTemp = classPath;
strClassPathTemp.Replace("bin\\classic","lib");
CString strClassPath = strClassPathTemp+"\\dt.jar";
strClassPath+=";";
strClassPath += strClassPathTemp;
strClassPath += "\\tools.jar";
strClassPath+=";";
strClassPath += strClassPathTemp;
strClassPath += "\\axis.jar";
strClassPath+=";";
strClassPath += strClassPathTemp;
strClassPath += "\\commons-discovery-0.2.jar";
strClassPath+=";";
strClassPath += strClassPathTemp;
strClassPath += "\\commons-logging-1.0.4.jar";
//
strClassPath+=";";
strClassPath += strClassPathTemp;
strClassPath += "\\jaxrpc.jar";
strClassPath+=";";
strClassPath += strClassPathTemp;
strClassPath += "\\saaj.jar";
strClassPath+=";";
strClassPath += strClassPathTemp;
strClassPath += "\\log4j-1.2.8.jar";
strClassPath+=";";
strClassPath += strClassPathTemp;
strClassPath += "\\axis-ant.jar";
strClassPath+=";";
strClassPath += strClassPathTemp;
strClassPath += "\\wsdl4j-1.5.1.jar";
strClassPath+=";.";
if(classPath!=NULL)
sprintf(m_szClassPath,"\-Djava.class.path=%s;%s;%s",strClassPath,classPath);
else
sprintf(m_szClassPath,"\-Djava.class.path=%s;%s",strClassPath);
JavaVMInitArgs vm_args;
JavaVMOption options[3];
memset(&vm_args, 0, sizeof(vm_args));
//进行初始化工作
options[0].optionString = "-Djava.compiler=NONE";
//options[1].optionString = "-Djava.class.path=d:\\run\\bin\\classic\\";
options[1].optionString = m_szClassPath;
options[2].optionString = "-verbose:jni";
vm_args.version=JNI_VERSION_1_4; //版本号设置
vm_args.nOptions = 3;
vm_args.options = options;
vm_args.ignoreUnrecognized = JNI_TRUE;
res = JNI_CreateJavaVM(&m_jvm,(void**)&m_env,&vm_args); //创建JVM
if (res < 0)
{
//AfxMessageBox("虚拟机创建失败");
m_Logrecorder.WriteLogF("JNI_CreateJavaVM 失败");
return -2;
}
return 0;
}
void CCCallJava::GetJvmEnv(char *classpath)
{
JavaVM* Jvm=NULL;
jint jsize;
jint jret = JNI_GetCreatedJavaVMs(&Jvm,1,&jsize);
if(Jvm==NULL)
init_env(classpath);
else
{
m_jvm =Jvm;
// Jvm->GetEnv((void**)&m_env,JNI_VERSION_1_4);
jint aa =m_jvm->AttachCurrentThread((void**)&m_env,NULL);
}
}
void CCCallJava::destroy_env()
{
if(m_jvm!=NULL)
{
//m_jvm->DetachCurrentThread();
//m_jvm->DestroyJavaVM();
// m_jvm=NULL;
}
}
JNIEnv* CCCallJava::getEvn()
{
return m_env;
}
JavaVM* CCCallJava::getJvm()
{
return m_jvm;
}
char* CCCallJava::getClassPath()
{
return m_szClassPath;
}
char* CCCallJava::jstringToWindows( JNIEnv *env, jstring jstr )
{
int length = (env)->GetStringLength(jstr );
const jchar* jcstr = (env)->GetStringChars(jstr, 0 );
char* rtn = (char*)malloc( length*2+1 );
int size = 0;
size = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)jcstr, length, rtn,(length*2+1), NULL, NULL );
if( size <= 0 )
return NULL;
(env)->ReleaseStringChars(jstr, jcstr );
rtn[size] = 0;
return rtn;
}
int CCCallJava::SendZipFile(char *szUrl,char* szService,char*file)
{ // TODO: 在此添加控件通知处理程序代码
jclass cls;
jmethodID mid;
jstring jstrUrl;
jstring jstrService;
jstring jstrPathFile;
m_strResult.Empty();
//cls = env->FindClass("HelloWorld");
if(m_env==NULL) //获得环境
{
jint aa =m_jvm->AttachCurrentThread((void**)&m_env,NULL);
if(m_env==NULL)
{
m_Logrecorder.WriteLogF("AttachCurrentThread 异常");
return -1;
}
}
//获得类句柄
cls = m_env->FindClass("SoaClient");
if((m_env->ExceptionOccurred()!=NULL)||(cls==NULL))
{
m_Logrecorder.WriteLogF("FindClass 异常");
return -1;
}
//获得类类的调用方法(静态方法)
mid = m_env->GetStaticMethodID(cls,"callsoapclient2", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
if( (m_env->ExceptionOccurred()!=NULL)||(mid==NULL))
{
m_Logrecorder.WriteLogF("GetStaticMethodID 异常");
return -2;
}
jstrUrl = m_env->NewStringUTF(szUrl);
jstrService = m_env->NewStringUTF(szService);
jstrPathFile = m_env->NewStringUTF(file);
//调用方法
jobject jstr2 = m_env->CallStaticObjectMethod(cls, mid, jstrUrl,jstrService,jstrPathFile); //调用Java程序main()方法,启动Java程序
if( (m_env->ExceptionOccurred()!=NULL)||(jstr2==NULL))
{
m_Logrecorder.WriteLogF("CallStaticObjectMethod 连接SOA服务器 异常");
return -2;
}
const char *cmsg;
char *pCBuf = jstringToWindows(m_env,(jstring)jstr2);
m_strResult = pCBuf;
#ifdef _DEBUG
FILE *fp=fopen("d:\\a1.txt","w");
fwrite(pCBuf,1,strlen(pCBuf),fp);
fclose(fp);
#endif
free(pCBuf);
pCBuf=NULL;
return 0 ;
}
const char* CCCallJava::getRestult()
{
return m_strResult;
}
/////////////////相应的java类
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import java.util.ArrayList;
///////////
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class SoaClient{
public static String callsoapclient2(String serverUrl,String ServerService,String files)
{
String sResult = null;
String sTargetEndpointAddress = serverUrl;
Service service = new Service();
Call call = null;
try
{
call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL( sTargetEndpointAddress));
call.setOperationName(new QName("",ServerService));
sResult = (String) call.invoke(new Object[] { CMyFile.readBytesFromFile(files), "zip" });
//sResult = (String) call.invoke(new Object[] { CMyFile.readBytesFromFile("D:\\data0.zip"), "zip" });
//sResult=sTargetEndpointAddress;
}catch (Exception e)
{
e.printStackTrace();
}
return sResult;
}
}
class CMyFile
{
public CMyFile()
{
}
public static byte[] readBytesFromFile(String _sFileName)
throws IOException
{
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
try
{
fis = new FileInputStream(_sFileName);
byte buffer[] = new byte[1024];
bos = new ByteArrayOutputStream(2048);
for(int nLen = 0; (nLen = fis.read(buffer)) > 0;)
bos.write(buffer, 0, nLen);
return bos.toByteArray();
}
catch(Exception e) { }
try
{
if(fis != null)
fis.close();
if(bos != null)
bos.close();
}
catch(Exception e) {
e.printStackTrace();
}
return null;
}
}
阅读(3391) | 评论(0) | 转发(1) |