English 中文(简体)
A. 如何与荷兰武装部队确定资源
原标题:How to define a resource with JNI
  • 时间:2023-12-19 05:14:36
  •  标签:
  • java
  • c++

在荷兰国立大学,我知道你可以使用<代码>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)
问题回答

A "resource" is just an ordinary file on one of the class path entries. If you run your code with java -cp ...:$SOME_DIRECTORY you can drop files in $SOME_DIRECTORY and they will be picked up. Of course at that point you might as well use plain file handling methods ..

为什么JNI doesDefineClass,这样你就可以界定新的 Java课,而不必先写到档案中,再由一年级的Loader审理。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签