Search code examples
c++jarincludebase64shellexecute

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


Solution

  • 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.