栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

是否可以制作调用jdbc的Java JNI?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

是否可以制作调用jdbc的Java JNI?

我不会以正确或错误的方式向您讲解您要尝试做的事情。但是,如果您尝试调用Java代码(JDBC.jar),那么以下内容适合您。.否则,请随意投票。

JVM.hpp:

#ifndef JVM_HPP_INCLUDED#define JVM_HPP_INCLUDED#include "../java/jni.h"#include <windows.h>#include <iostream>#include <stdexcept>#include <algorithm>class Jvm{    private:        JavaVM* jvm;        JNIEnv* env;        JavaVMInitArgs jvm_args;        jclass systemClassLoader;    public:        Jvm(std::string ClassPath = ".");        ~Jvm();        inline JavaVM* GetJVM() const {return jvm;}        inline JNIEnv* GetENV() const {return env;}        inline jclass GetSystemClassLoader() const {return systemClassLoader;}        void DestroyJVM();        void PrintStackTrace();        jclass DefineClass(const char* FullClassName, const void* ClassBuffer, std::uint32_t BufferLength);        jclass DefineClass(const char* FullClassName, jobject ClassLoader, const void* ClassBuffer, std::uint32_t BufferLength);        void RegisterNativeMethod(const char* MethodName, const char* MethodSignature, void* func_ptr);        void RegisterNativeMethod(jobject ClassLoader, const char* MethodName, const char* MethodSignature, void* func_ptr);        void RegisterNativeMethods(JNINativeMethod* Methods, std::uint32_t MethodCount);        void RegisterNativeMethods(jobject ClassLoader, JNINativeMethod* Methods, std::uint32_t MethodCount);    protected:        void InitClassLoader();};#endif // JVM_HPP_INCLUDED

JVM.cpp:

#include "JVM.hpp"Jvm::~Jvm(){    env->DeleteGlobalRef(this->systemClassLoader);    jvm->DestroyJavaVM();}Jvm::Jvm(std::string ClassPath) : jvm(NULL), env(NULL), jvm_args(), systemClassLoader(NULL){    JavaVMOption* options = new JavaVMOption[2];    jvm_args.version = JNI_VERSION_1_6;    JNI_GetDefaultJavaVMInitArgs(&jvm_args);    options[0].optionString = const_cast<char*>("-Djava.compiler=NONE");    options[1].optionString = const_cast<char*>(("-Djava.class.path=" + ClassPath).c_str());    jvm_args.nOptions = 2;    jvm_args.options = options;    jvm_args.ignoreUnrecognized = false;    if (JNI_CreateJavaVM(&jvm, reinterpret_cast<void**>(&env), &jvm_args))    {        delete[] options;        throw std::runtime_error("Failed To Create JVM Instance.");    }    delete[] options;}void Jvm::InitClassLoader(){    if (!this->systemClassLoader)    {        jclass classloader = env->FindClass("Ljava/lang/ClassLoader;");        if (!classloader)        { throw std::runtime_error("Failed To find ClassLoader.");        }        jmethodID SystemLoaderMethod = env->GetStaticMethodID(classloader, "getSystemClassLoader", "()Ljava/lang/ClassLoader;");        jobject loader = env->CallStaticObjectMethod(classloader, SystemLoaderMethod);        this->systemClassLoader = reinterpret_cast<jclass>(env->NewGlobalRef(loader));    }}void Jvm::PrintStackTrace(){    if (env->ExceptionOccurred())    {        env->ExceptionDescribe();        env->ExceptionClear();    }}jclass Jvm::DefineClass(const char* FullClassName, const void* ClassBuffer, std::uint32_t BufferLength){    this->InitClassLoader();    return this->DefineClass(FullClassName, this->systemClassLoader, ClassBuffer, BufferLength);}jclass Jvm::DefineClass(const char* FullClassName, jobject ClassLoader, const void* ClassBuffer, std::uint32_t BufferLength){    return ClassLoader ? env->DefineClass(FullClassName, ClassLoader, static_cast<const jbyte*>(ClassBuffer), BufferLength) : NULL;}void Jvm::RegisterNativeMethod(const char* MethodName, const char* MethodSignature, void* func_ptr){    JNINativeMethod method;    method.name = const_cast<char*>(MethodName);    method.signature = const_cast<char*>(MethodSignature);    method.fnPtr = func_ptr;    this->RegisterNativeMethods(&method, 1);}void Jvm::RegisterNativeMethod(jobject ClassLoader, const char* MethodName, const char* MethodSignature, void* func_ptr){    JNINativeMethod method;    method.name = const_cast<char*>(MethodName);    method.signature = const_cast<char*>(MethodSignature);    method.fnPtr = func_ptr;    this->RegisterNativeMethods(ClassLoader, &method, 1);}void Jvm::RegisterNativeMethods(JNINativeMethod* Methods, std::uint32_t MethodCount){    this->InitClassLoader();    this->RegisterNativeMethods(this->systemClassLoader, Methods, MethodCount);}void Jvm::RegisterNativeMethods(jobject ClassLoader, JNINativeMethod* Methods, std::uint32_t MethodCount){    if (ClassLoader)    {        env->RegisterNatives(static_cast<jclass>(ClassLoader), Methods, MethodCount);    }}

然后,您可以创建一个实例来加载您的jar。

int main(){    Jvm VM("C:/Users/Brandon/IdeaProjects/Eos/out/production/Eos/Bot.jar");    jclass jMain = VM.GetENV()->FindClass("eos/Main");    if (jMain != nullptr)    {        jmethodID mainMethod = env->GetStaticMethodID(jMain, "main", "([Ljava/lang/String;)V");        jclass StringClass = env->FindClass("java/lang/String");        jobjectArray Args = env->NewObjectArray(0, StringClass, 0);        env->CallStaticVoidMethod(jMain, MainMethod, Args);    }}

现在,这仅显示了如何使用Main Method运行jar。但是,您可以从jar中访问ANY类,并使用所需的许多参数来调用它。它不需要主电源。

现在要做很多工作,但是我不会教你。问题是是否可能,答案是肯定的。只要您创建“ JVM”的实例。之后,只需通过Java中的“ Package /
Class”(而不是“ Package.Class”)访问类,然后调用所需的任何方法即可。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/404420.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号