Blub
Blub

Reputation: 13624

How do I catch an error thrown by JNI_CreateJavaVM?

PtrCreateJavaVM ptrCreateJavaVM = 
    (PtrCreateJavaVM)GetProcAddress(hinstLib, "JNI_CreateJavaVM");
JNIEnv* env = NULL;
try {
    int x = ptrCreateJavaVM(&jvm, (void **)&env, &args);
}catch (int e)
{
    printf("%d", e);
    fflush(0);
}

In case of a missing attached debugger, this code just exists the whole program and prints this to the console:

ERROR: transport error 202: connect failed: Connection refused ERROR: JDWP Transport dt_socket failed to initialize, TRANSPORT_INIT(510) JDWP exit error AGENT_ERROR_TRANSPORT_INIT(197): No transports initialized [../. ./../src/share/back/debugInit.c:741] FATAL ERROR in native method: JDWP No transports initialized, jvmtiError=AGENT_E RROR_TRANSPORT_INIT(197)

I'd like to continue and log the error. How do I do that?

Upvotes: 1

Views: 1344

Answers (2)

Tom Blodget
Tom Blodget

Reputation: 20802

The fatal error is probably from the call that uses ptrCreateJavaVM. For it to work, hinstLib has to be a valid handle for jvm.dll (or equivalent) and GetProcAddress has to succeed.

The documentation for GetProcAddress says:

If the function fails, the return value is NULL. To get extended error information, call GetLastError.

Also be aware that the Win32 API that deals with strings has "ANSI" and Unicode versions of functions and structures. In recent decades, it's generally easier to use Unicode everywhere you can. (Java and .NET do; VB did; Windows and the WWW generally do.) So, try:

GetProcAddressW(hinstLib, L"JNI_CreateJavaVM")

where the "W" and "L" mean "wide" characters, which in the Win32 API and MSVC mean the UTF-16LE encoding of Unicode.

BTW—You could call JNI_CreateJavaVM directly and link in jvm.lib.


UPDATE:

Okay, you say the cause is your args results in a FATAL ERROR. So, you'll have to say either that is a bug in the JVM or your args aren't what you want since you do want to avoid a FATAL ERROR. Have you tried "suspend=y"?

Upvotes: 0

main--
main--

Reputation: 3973

JNI_CreateJavaVM is a normal C function and therefore doesn't throw C++ exceptions, so your try-catch is useless. It communicates through status codes. If x is JNI_OK, then the call was successful. Otherwise the value inside x is the error code.

Upvotes: 4

Related Questions