简体   繁体   English

我如何用Base64字符串执行c ++中包含的jar文件,而不在HDD上写入它?

[英]How can I execute a jar file included in c++ with a Base64 string without write it on the HDD?

How I can execute a jar file included in my c++ exe file in a Base64 string without write it on the HDD? 如何在不将其写入HDD的情况下执行Base64字符串中c ++ exe文件中包含的jar文件? Is it possibile execute it from memory or something like this without have 2 files on the HDD? 是否可以从内存或类似的方法在HDD上没有2个文件的情况下执行它?

Thanks in advance 提前致谢

Using JNI: 使用JNI:

  • Create a JVM instance. 创建一个JVM实例。
  • Create a ByteArrayInputStream on the decoded base64 jar string. 在解码的base64 jar字符串上创建ByteArrayInputStream。
  • Create a JarInputStream and pass it the ByteArrayInputStream. 创建一个JarInputStream并将其传递给ByteArrayInputStream。
  • Iterate each jarEntry in the JarInputStream and load the class with a ClassLoader. 迭代JarInputStream中的每个jarEntry,并使用ClassLoader加载类。
  • Store each class in a HashMap for quick access. 将每个类存储在HashMap中以便快速访问。

Another way is to call JNI's: 另一种方法是调用JNI:

jclass DefineClass(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize bufLen);

Example: https://github.com/Brandon-T/Cpp-JVM 示例: https //github.com/Brandon-T/Cpp-JVM

I added the example to my Github since it was way too large to fit on StackOverflow. 我将示例添加到我的Github中,因为它太大而无法放在StackOverflow上。 Note: You do not need the JNI wrapper that I wrote. 注意:您不需要我编写的JNI包装器。 It would cut down on code by a ton if you use raw JNI. 如果使用原始的JNI,它将减少一吨的代码。

The wrapper is there from some legacy code I wrote a long time ago and I decided to just use it and have it keep track of the JavaVM and JNIEnv for me. 包装器是我很久以前编写的一些遗留代码中的一部分,我决定只使用它,并让它为我跟踪JavaVM和JNIEnv。

EDIT: Beware that I hardcoded the path to libJVM and libJNI: 编辑:请注意,我对libJVM和libJNI的路径进行了硬编码:

https://github.com/Brandon-T/Cpp-JVM/blob/master/JVM.cpp#L41 because I couldn't get the JVM running on OSX without it. https://github.com/Brandon-T/Cpp-JVM/blob/master/JVM.cpp#L41,因为没有它我无法让JVM在OSX上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM