java本身建议创建ClassLoader子类,并完成类似addURL方法已达到动态加载类路径。但在第三方包运行时,对方已经实现了ClassLoader子类且未提供此方法。则可以利用JVMPI技术完成这一操作。
java 部分:
<com.myCom.myproj.myclass>
extClassLoader = Thread.currentThread().getContextClassLoader();
File extDir = new File( System.getProperty( "cyanea.home" )+"/lib/ext/" );
File extFiles[] = extDir.listFiles();
ArrayList loaders = null;
if( extFiles != null )
{
for( int i = 0 ; i < extFiles.length ; i ++ )
{
try
{
loaders = addClassPath( extClassLoader, extFiles[i].toURL() );
}catch( Exception e )
{
System.out.println( "exception in addClassPath" );
}
}
C 部分:
#include "classloader.h"
#pragma convlit(resume)
//it is a helper function to get the URL of a loader
//addClassPath will return an arraylist of loader, iterate the arraylist and call this function
//will print out the class loader loading sequence
JNIEXPORT jobject JNICALL Java_com_myCom_myproj_myclass_addClassPath( JNIEnv *env, jobject thisObject, jobject extclassloader, jobject url )
{
jobject loaders = NULL;
try{
if( extclassloader == NULL || url == NULL ) throw 0;
jclass extclassloaderclass = env->GetObjectClass( extclassloader );
//add code to deal with the ClassLoader Implement Class here.
//load
jfieldID ucpid = env->GetFieldID( extclassloaderclass, "ucp", "Lsun/misc/URLClassPath;" );
if( ucpid == NULL ) throw 5;
jobject ucp = env->GetObjectField( extclassloader, ucpid );
if( ucp == NULL ) throw 6;
jclass ucpclass = env->GetObjectClass( ucp );
jfieldID pathid = env->GetFieldID( ucpclass, "path", "Ljava/util/ArrayList;" );
if( pathid == NULL ) throw 7;
jobject path = env->GetObjectField( ucp, pathid );
if( path == NULL ) throw 8;
jclass pathclass = env->GetObjectClass( path );
jmethodID add2path = env->GetMethodID( pathclass, "add", "(ILjava/lang/Object;)V" );
if( add2path == NULL ) throw 9;
env->CallVoidMethod( path, add2path, 0, url );
if( env->ExceptionOccurred() ) throw 10;
jmethodID check = env->GetStaticMethodID( ucpclass, "check", "(Ljava/net/URL;)V" );
if( check == NULL ) throw 11;
env->CallStaticVoidMethod( ucpclass, check, url );
if( env->ExceptionOccurred() ) throw 12;
//create loader and add to loaders and lmap
共2页 1 2






