简体   繁体   中英

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? Is it possibile execute it from memory or something like this without have 2 files on the HDD?

Thanks in advance

Using JNI:

  • Create a JVM instance.
  • Create a ByteArrayInputStream on the decoded base64 jar string.
  • Create a JarInputStream and pass it the ByteArrayInputStream.
  • Iterate each jarEntry in the JarInputStream and load the class with a ClassLoader.
  • Store each class in a HashMap for quick access.

Another way is to call JNI's:

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

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

I added the example to my Github since it was way too large to fit on StackOverflow. Note: You do not need the JNI wrapper that I wrote. It would cut down on code by a ton if you use raw 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.

EDIT: Beware that I hardcoded the path to libJVM and 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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