How do I debug my Java code using remote debugging when the JVM is started from C code? I need the password to connect the debugger to the JVM but the password is unobtainable when I call the JNI_CreateJavaVM function.
Created May 4, 2012
Davanum Srinivas The code below is for Win32 Environment. It assumes that your JDK1.3 is installed in D:JDK13 directory. Before you run the code please make sure that you set the PATH to include D:JDK13JREBINCLASSIC. JVM.DLL will be picked up from this location.
#include <jni.h> static jint JNICALL jvm_output(FILE *stream, const char *format, va_list arg) { return vfprintf(stdout, format, arg); } int main(int argc, char **argv) { JavaVMInitArgs vm_args; JavaVMOption options[3]; JavaVM *jvm = NULL; int optcnt = 0; JNIEnv *env = NULL; jint cr = 0; /* Set the debug Option */ options[optcnt++].optionString = "-Xdebug"; /* Set the boot classpath */ options[optcnt++].optionString = "-Xbootclasspath:D:jdk13jrelib t.jar;D:jdk13lib ools.jar"; /* Trap line-mode output from JVM */ options[optcnt].optionString = "vfprintf"; options[optcnt++].extraInfo = jvm_output; /* Initialize fields in startup struct */ vm_args.version = JNI_VERSION_1_2; /* Initialize version field */ vm_args.options = options; vm_args.nOptions = optcnt; vm_args.ignoreUnrecognized = JNI_FALSE; /* Create the JVM */ cr = JNI_CreateJavaVM(&jvm, (void **) &env, &vm_args); if (cr) { printf("Call to JNI_CreateJavaVM() failed with result value %ld. ", (long) cr); return 1; } printf("Created Java VM successfully "); /* Exit without doing anything useful */ return 0; }Here's the BUILD.BAT. Edit the first two lines to specify the correct directories and run it with the name of the CPP file as the argument (ex: "build myjvm.cpp).
@ECHO OFF SET DEVSTUDIO=D:Visual StudioVC98 SET JDK13=D:JDK13 SET COMPILE_CMD="%DEVSTUDIO%bincl" SET COMPILE_CMD=%COMPILE_CMD% %1 SET COMPILE_CMD=%COMPILE_CMD% -I"%JDK13%INCLUDE" SET COMPILE_CMD=%COMPILE_CMD% -I"%JDK13%INCLUDEWIN32" SET COMPILE_CMD=%COMPILE_CMD% -I"%DEVSTUDIO%Include" SET COMPILE_CMD=%COMPILE_CMD% -MD /link SET COMPILE_CMD=%COMPILE_CMD% /libpath:""%JDK13%lib"" SET COMPILE_CMD=%COMPILE_CMD% /libpath:""%DEVSTUDIO%lib"" SET COMPILE_CMD=%COMPILE_CMD% user32.lib gdi32.lib jvm.lib @ECHO ON %COMPILE_CMD%