JNI call ending in EXCEPTION_ACCESS_VIOLATION

G

Gordon Beaton

Hello,

I have been struggling to call a Java method from C++ program on
Microsoft Windows XP.


I use Visual C++ 7.1 and NetBeans tools to create the programs that
uses Java Native Interface(JNI).

The call results in EXCEPTION_ACCESS_VIOLATION when JNI_CreateJavaJVM
call is made .

There are a number of issues with the code, but I think your problem
is one of these:

- options[1].optionString is NULL, but you attempt to strcpy() to it.
The option only holds a pointer, you need to provide space for the
option string yourself.

- after calling JNI_CreateJavaVM() you check env->ExceptionOccurred()
before checking the result of the call to JNI_CreateJavaVM(). If the
JVM fails to start for any reason, then you can't expect env to be
initialized properly, so you should test result first.

- you *optionally* set the classpath, but you *always* set nOptions=2
even if you only set 1 option (leaving the second position
uninitialized).

/gordon
 
S

singhtjunior

Hello,

I have been struggling to call a Java method from C++ program on
Microsoft Windows XP.


I use Visual C++ 7.1 and NetBeans tools to create the programs that
uses Java Native Interface(JNI).

The call results in EXCEPTION_ACCESS_VIOLATION when JNI_CreateJavaJVM
call is made .

The error message is listed at the end of the article. The Error Log
file is also
appended at the end.

Has anyone faced this problem? Can you kindly share your approach to
the problem?
Thank you in advance for your kind help.

-Sincerely,
Singh T. Junior
(e-mail address removed)

==============C++ CODE ==========================================

The C++ Code is shown here.

// InitializeJNI
void InitializeJNI()
{
cout << "Welcome to test_JavaMethod()" << endl;
cout << "Version: " << GetVersion() << endl;
JNIEnv *env = NULL;
JavaVM *javaVM = NULL;
jint result = -1;
JavaVMInitArgs args;

jmethodID methodID;
jstring strVar;
jclass strCls;
jobjectArray objArgs;
char classpath[MAX_PATH];
JavaVMOption options[2];
options[0].optionString = "-Djava.complier=JIT";
char const* clspath = getenv("CLASSPATH");
if (clspath)
{
char* p1 = "-Djava.class.path=";
options[1].optionString = new char[strlen(p1)+strlen(clspath)+1];
strcpy(options[1].optionString, p1);
strcat(options[1].optionString, clspath);
}
else
{
printf("Classpath may not be set for java virtual machine\n");
}

args.nOptions = 2;
args.version = JNI_VERSION_1_4;
args.options = options;
args.ignoreUnrecognized = JNI_TRUE;


/*Create the Java VM*/
std::cout << "About to call JNI_CreateJavaVM" << std::endl;
result = JNI_CreateJavaVM(&javaVM, (void**)&env, &args);

if ( env->ExceptionOccurred())
{
std::cerr << "Unable to initialize VM" << std::endl;
env->ExceptionDescribe();
exit(-1);
}

if ( result == JNI_OK ) // Success
{
printf("JVM Succesfully Created!\n");
}
else
{
fprintf(stderr,"JNI_ERR Unable to create the Java VM %d\n", result);
//PrintStatus(result);
exit(1);
}
}


===== WINDOWS and JAVA VERSION Information ==================


Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\tsingh>java -version
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)




==================ERROR MESSAGE =================================
Version: 170393861
About to call JNI_CreateJavaVM
#
# An unexpected error has been detected by HotSpot Virtual Machine:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x1002561c, pid=2780,
tid=2784
#
# Java VM: Java HotSpot(TM) Client VM (1.5.0_06-b05 mixed mode,
sharing)
# Problematic frame:
# C [MYData.Java.dll+0x2561c]
#
# An error report file with more information is saved as
hs_err_pid2780.log
#
# If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp
#
Java Result: 1

============== ERROR LOG ======================================

#
# An unexpected error has been detected by HotSpot Virtual Machine:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x1002561c, pid=3560,
tid=3564
#
# Java VM: Java HotSpot(TM) Client VM (1.5.0_06-b05 mixed mode,
sharing)
# Problematic frame:
# C [Triton.MarketData.Feed.Java.dll+0x2561c]
#

--------------- T H R E A D ---------------

Current thread (0x00036098): JavaThread "main" [_thread_in_native,
id=3564]

siginfo: ExceptionCode=0xc0000005, reading address 0x00000000

Registers:
EAX=0x00000000, EBX=0x0007f680, ECX=0x00000000, EDX=0x00000001
ESP=0x0007f5a4, EBP=0x0007f680, ESI=0x0007f5a8, EDI=0x0007f680
EIP=0x1002561c, EFLAGS=0x00010216

Top of Stack: (sp=0x0007f5a4)
0x0007f5a4: 00000000 0007f684 0007f688 0007f680
0x0007f5b4: cccccccc cccccccc cccccccc cccccccc
0x0007f5c4: cccccccc cccccccc cccccccc cccccccc
0x0007f5d4: cccccccc cccccccc cccccccc cccccccc
0x0007f5e4: cccccccc cccccccc cccccccc cccccccc
0x0007f5f4: cccccccc cccccccc cccccccc cccccccc
0x0007f604: cccccccc cccccccc cccccccc cccccccc
0x0007f614: cccccccc cccccccc cccccccc cccccccc

Instructions: (pc=0x1002561c)
0x1002560c: cc f3 ab 59 89 4d f8 8b f4 8b 45 f8 50 8b 4d f8
0x1002561c: 8b 11 ff 52 3c 3b f4 e8 fb c8 ff ff 5f 5e 5b 81


Stack: [0x00040000,0x00080000), sp=0x0007f5a4, free space=253k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code,
C=native code)
C [Triton.MarketData.Feed.Java.dll+0x2561c]
C [Triton.MarketData.Feed.Java.dll+0x259b2]
C [Triton.MarketData.Feed.Java.dll+0x23a3f]
j TritonMarketDataFeed.ConsoleApp.Connect()Z+0
j TritonMarketDataFeed.ConsoleApp.Handle_Connect()V+3
j TritonMarketDataFeed.ConsoleApp.ExecuteSelection(I)V+105
j TritonMarketDataFeed.ConsoleApp.Run()V+17
j TritonMarketDataFeed.Main.runTest2()V+17
j TritonMarketDataFeed.Main.main([Ljava/lang/String;)V+17
v ~StubRoutines::call_stub
V [jvm.dll+0x845a9]
V [jvm.dll+0xd9317]
V [jvm.dll+0x8447a]
V [jvm.dll+0x8b44a]
C [java.exe+0x14c5]
C [java.exe+0x64dd]
C [kernel32.dll+0x16d4f]

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j TritonMarketDataFeed.ConsoleApp.Connect()Z+0
j TritonMarketDataFeed.ConsoleApp.Handle_Connect()V+3
j TritonMarketDataFeed.ConsoleApp.ExecuteSelection(I)V+105
j TritonMarketDataFeed.ConsoleApp.Run()V+17
j TritonMarketDataFeed.Main.runTest2()V+17
j TritonMarketDataFeed.Main.main([Ljava/lang/String;)V+17
v ~StubRoutines::call_stub

--------------- P R O C E S S ---------------

Java Threads: ( => current thread )
0x00a6e6b8 JavaThread "Low Memory Detector" daemon [_thread_blocked,
id=3588]
0x00a6d288 JavaThread "CompilerThread0" daemon [_thread_blocked,
id=3584]
0x00a48e58 JavaThread "Signal Dispatcher" daemon [_thread_blocked,
id=3580]
0x0003fe30 JavaThread "Finalizer" daemon [_thread_blocked, id=3576]
0x0003faa0 JavaThread "Reference Handler" daemon [_thread_blocked,
id=3572]
=>0x00036098 JavaThread "main" [_thread_in_native, id=3564]

Other Threads:
0x00a388b8 VMThread [id=3568]
0x00a6f8d0 WatcherThread [id=3592]

VM state:not at safepoint (normal execution)

VM Mutex/Monitor currently owned by a thread: None

Heap
def new generation total 576K, used 225K [0x22a70000, 0x22b10000,
0x22f50000)
eden space 512K, 43% used [0x22a70000, 0x22aa8490, 0x22af0000)
from space 64K, 0% used [0x22af0000, 0x22af0000, 0x22b00000)
to space 64K, 0% used [0x22b00000, 0x22b00000, 0x22b10000)
tenured generation total 1408K, used 0K [0x22f50000, 0x230b0000,
0x26a70000)
the space 1408K, 0% used [0x22f50000, 0x22f50000, 0x22f50200,
0x230b0000)
compacting perm gen total 8192K, used 83K [0x26a70000, 0x27270000,
0x2aa70000)
the space 8192K, 1% used [0x26a70000, 0x26a84d58, 0x26a84e00,
0x27270000)
ro space 8192K, 63% used [0x2aa70000, 0x2af7b178, 0x2af7b200,
0x2b270000)
rw space 12288K, 46% used [0x2b270000, 0x2b809fa8, 0x2b80a000,
0x2be70000)

Dynamic libraries:
0x00400000 - 0x0040c000 C:\WINDOWS\system32\java.exe
0x7c900000 - 0x7c9b0000 C:\WINDOWS\system32\ntdll.dll
0x7c800000 - 0x7c8f4000 C:\WINDOWS\system32\kernel32.dll
0x77dd0000 - 0x77e6b000 C:\WINDOWS\system32\ADVAPI32.dll
0x77e70000 - 0x77f01000 C:\WINDOWS\system32\RPCRT4.dll
0x77c10000 - 0x77c68000 C:\WINDOWS\system32\MSVCRT.dll
0x6d670000 - 0x6d804000 C:\Program
Files\Java\jre1.5.0_06\bin\client\jvm.dll
0x77d40000 - 0x77dd0000 C:\WINDOWS\system32\USER32.dll
0x77f10000 - 0x77f57000 C:\WINDOWS\system32\GDI32.dll
0x76b40000 - 0x76b6d000 C:\WINDOWS\system32\WINMM.dll
0x6d280000 - 0x6d288000 C:\Program Files\Java\jre1.5.0_06\bin\hpi.dll
0x76bf0000 - 0x76bfb000 C:\WINDOWS\system32\PSAPI.DLL
0x6d640000 - 0x6d64c000 C:\Program
Files\Java\jre1.5.0_06\bin\verify.dll
0x6d300000 - 0x6d31d000 C:\Program Files\Java\jre1.5.0_06\bin\java.dll
0x6d660000 - 0x6d66f000 C:\Program Files\Java\jre1.5.0_06\bin\zip.dll
0x10000000 - 0x10077000
C:\Sandbox\Triton\Projects\MD\Bin\Triton.MarketData.Feed.Java.dll
0x10200000 - 0x10287000 C:\WINDOWS\system32\MSVCR71D.dll
0x10480000 - 0x1053c000 C:\WINDOWS\system32\MSVCP71D.dll
0x61000000 - 0x6105b000 C:\singh.dir\dbcapi.dll
0x71ad0000 - 0x71ad9000 C:\WINDOWS\system32\WSOCK32.dll
0x71ab0000 - 0x71ac7000 C:\WINDOWS\system32\WS2_32.dll
0x71aa0000 - 0x71aa8000 C:\WINDOWS\system32\WS2HELP.dll
0x771b0000 - 0x77256000 C:\WINDOWS\system32\WININET.dll
0x77a80000 - 0x77b14000 C:\WINDOWS\system32\CRYPT32.dll
0x77b20000 - 0x77b32000 C:\WINDOWS\system32\MSASN1.dll
0x77120000 - 0x771ac000 C:\WINDOWS\system32\OLEAUT32.dll
0x774e0000 - 0x7761d000 C:\WINDOWS\system32\ole32.dll
0x77f60000 - 0x77fd6000 C:\WINDOWS\system32\SHLWAPI.dll
0x02c50000 - 0x02c5a000 C:\WINDOWS\system32\IMPLODE.DLL
0x773d0000 - 0x774d2000
C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9\comctl32.dll

VM Arguments:
java_command: TritonMarketDataFeed.Main
Launcher Type: SUN_STANDARD

Environment Variables:
CLASSPATH=C:\singh.dir\TSESignalAPITest\build\classes\TritonMarketDataFeed;C:\singh.dir\TSESignalAPITest\build\classes\
PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program
Files\QuickTime\QTSystem\;C:\Program Files\Microsoft SQL
Server\80\Tools\BINN;C:\Program Files\Java\jre1.5.0_06\bin\client
USERNAME=tsingh
LD_LIBRARY_PATH=C:\Program Files\Java\jdk1.5.0_06\lib
OS=Windows_NT
PROCESSOR_IDENTIFIER=x86 Family 15 Model 2 Stepping 4, GenuineIntel



--------------- S Y S T E M ---------------

OS: Windows XP Build 2600 Service Pack 2

CPU:total 1 family 15, cmov, cx8, fxsr, mmx, sse, sse2, ht

Memory: 4k page, physical 523760k(52280k free), swap 1280228k(900136k
free)

vm_info: Java HotSpot(TM) Client VM (1.5.0_06-b05) for windows-x86,
built on Nov 10 2005 11:12:14 by "java_re" with MS VC++ 6.0
 
S

singhtjunior

Gordon,

You are right about the not checking env. However, I continue to get -1
from the JNI_CreateJavaJVM call, that corresonds to JNI_ERR.

Do you have a sample code or project with proper settings for Microsoft
Visual C++
that succesfully creates an instance of JVM using the call above?

Thank you for your answer. It indeed solved part of the problem after I
moved the code for checking the exception after the result check.

void TSJavaLib::InitializeJNI()
{
cout << "Welcome to test_JavaMethod()" << endl;
cout << "Version: " << GetVersion() << endl;

jint result = -1;
JavaVMInitArgs args;

JavaVMOption options[2];
options[0].optionString = "-Djava.complier=JIT";
char const* clspath = getenv("CLASSPATH");
if (clspath)
{
char* p1 = "-Djava.class.path=";
options[1].optionString = new char[strlen(p1)+strlen(clspath)+1];
strcpy(options[1].optionString, p1);
strcat(options[1].optionString, clspath);
}
else
{
printf("Classpath may not be set for java virtual machine\n");
}

args.nOptions = 2;
args.version = JNI_VERSION_1_4;
args.options = options;
args.ignoreUnrecognized = JNI_TRUE;


/*Create the Java VM*/
std::cout << "About to call JNI_CreateJavaVM" << std::endl;
result = JNI_CreateJavaVM(&jvm_, (void**)&env_, &args);

if ( result == JNI_OK ) // Success
{
printf("JVM Succesfully Created!\n");
}
else
{
fprintf(stderr,"JNI_ERR Unable to create the Java VM %d\n", result);
exit(1);
}

if ( env_->ExceptionOccurred())
{
std::cerr << "Unable to initialize VM" << std::endl;
env_->ExceptionDescribe();
exit(-1);
}


}
 
G

Gordon Beaton

You are right about the not checking env. However, I continue to get
-1 from the JNI_CreateJavaJVM call, that corresonds to JNI_ERR.

Do you have a sample code or project with proper settings for
Microsoft Visual C++ that succesfully creates an instance of JVM
using the call above?

Are the JVM dlls visible in the PATH when you run the program?

Sorry I have no experience with VC++ or really any programming on
Windows. However similar questions come up in this group relatively
frequently, so searching for "visual" and "JNI_CreateJavaVM" in
GoogleGroups should lead you to an answer.

/gordon
 
S

singhtjunior

Gordon,

I removed jvm.dll from various directories that were not part of Java
install. It works great now. Thank you for all the help.

-Sincerely,
Singh T. Junior
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top