在荷兰国立大学,我知道你可以使用<代码>env->DefineClass界定一个类别。 例如,
jclass klass = client->env->DefineClass(nullptr, classLoader, (const jbyte*) classBytes, (jsize) classBytes_size);
同样,我预计还有一项职能,在运行时间界定资源。 通过“资源”,我指的是在杰尔档案中包装的档案,如图像或声音。 但是,我找不到这样做的任何职能。 为什么没有<代码>DefineResource功能? 如果有可能,你如何这样做?
Edit:我是如何向上班机添加一部《URL》。 然而,该方案仍然无法找到我的资源。
Code (jni):
// Creates a File object to the folder, then converts to a URL and then passes it to the ClassLoader.
#define checkNull(x) checkNull_(#x, x)
template <class T>
void checkNull_(const std::string &varName, T x) {
if (!x) throw std::runtime_error(varName + " is null");
}
void JavaUtils::addClassLoaderURL() {
jobject classLoader = getClassLoader();
checkNull(classLoader);
jclass URLClassLoader = app->getClass("java.net.URLClassLoader");
checkNull(URLClassLoader);
jclass File = app->getClass("java.io.File");
checkNull(File);
jclass URI = app->getClass("java.net.URI");
checkNull(URI);
jmethodID fileConstructor = app->env->GetMethodID(File, "<init>", "(Ljava/lang/String;)V");
checkNull(fileConstructor);
jstring classpath = app->env->NewStringUTF("~/classpath");
checkNull(classpath);
jobject fileObject = app->env->NewObject(File, fileConstructor, classpath);
checkNull(fileObject);
jmethodID toURI = app->env->GetMethodID(File, "toURI", "()Ljava/net/URI;");
checkNull(toURI);
jmethodID toURL = app->env->GetMethodID(URI, "toURL", "()Ljava/net/URL;");
checkNull(toURL);
jobject URIObject = app->env->CallObjectMethod(fileObject, toURI);
checkNull(toURL);
jobject URLObject = app->env->CallObjectMethod(URIObject, toURL);
checkNull(URLObject);
jmethodID addURL = app->env->GetMethodID(URLClassLoader, "addURL", "(Ljava/net/URL;)V");
checkNull(addURL);
app->env->CallVoidMethod(classLoader, addURL, URLObject);
if (app->env->ExceptionCheck() == JNI_TRUE) {
std::cout << "Exception:" << std::endl;
jthrowable exception = app->env->ExceptionOccurred();
app->env->ExceptionClear();
JavaUtils::getInformation(exception);
return;
}
std::cout << "Successfully added path to classloader" << std::endl;
}
The contents of the folder:
~
- classpath
- - test
- - - version.txt
否则:
InputStream input = VersionManager.class.getResourceAsStream("/assets/test/version.txt");
Scanner scanner = new Scanner(input); // null pointer exception (because the resource does not exist)