Why?When send a String type param from Java to C,I get An unexpected error.

The error trace like this:

#

# An unexpected error has been detected by HotSpot Virtual Machine:

#

# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x10001d9c, pid=1944, tid=3204

#

# Java VM: Java HotSpot(TM) Client VM (1.5.0_07-b03 mixed mode, sharing)

# Problematic frame:

# C [sample.dll+0x1d9c]

#

T H R E A D

Current thread (0x00036158): JavaThread "main" [_thread_in_native, id=3204]

siginfo: ExceptionCode=0xc0000005, reading address 0x00000051

Registers:

EAX=0x7ffffffe, EBX=0x26c24ad0, ECX=0x00000051, EDX=0x7fffffff

ESP=0x0007f748, EBP=0x0007f9fc, ESI=0x0007fa38, EDI=0x0007fa88

EIP=0x10001d9c, EFLAGS=0x00010206

Top of Stack: (sp=0x0007f748)

0x0007f748:0007fa88 0007fa38 26c24ad0 7c930738

0x0007f758:7fffffff 00000030 7c931596 7c9306eb

0x0007f768:00000007 00000008 26c24ad0 0007f788

0x0007f778:0007f7b4 00ae29a4 00000000 22bf60d0

0x0007f788:10001005 00000000 00000001 00000000

0x0007f798:0007f790 02eb02b0 0007f7e0 7ffffffe

0x0007f7a8:00000000 00000051 0007f7dc 0007f7f4

0x0007f7b8:00000000 00000000 00000000 00000000

Instructions: (pc=0x10001d9c)

0x10001d8c:89 85 a8 fd ff ff 85 d2 74 1e 8b 8d b0 fd ff ff

0x10001d9c:0f be 11 85 d2 74 11 8b 85 b0 fd ff ff 83 c0 01

Stack: [0x00040000,0x00080000), sp=0x0007f748, free space=253k

Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)

C [sample.dll+0x1d9c]

C [sample.dll+0x10ca]

C [sample.dll+0x12e7a]

j HelloWorld.sayHello(Ljava/lang/String;)V+0

j HelloWorld.main([Ljava/lang/String;)V+11

v ~StubRoutines::call_stub

V [jvm.dll+0x86401]

V [jvm.dll+0xdb172]

V [jvm.dll+0x862d2]

V [jvm.dll+0x8d2a2]

C [java.exe+0x14c5]

C [java.exe+0x69cd]

C [kernel32.dll+0x16d4f]

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)

j HelloWorld.sayHello(Ljava/lang/String;)V+0

j HelloWorld.main([Ljava/lang/String;)V+11

v ~StubRoutines::call_stub

P R O C E S S

Java Threads: ( => current thread )

0x00a7f6c8 JavaThread "Low Memory Detector" daemon [_thread_blocked, id=3216]

0x00a7e3c0 JavaThread "CompilerThread0" daemon [_thread_blocked, id=2040]

0x00a7d480 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=1760]

0x00a51f58 JavaThread "Finalizer" daemon [_thread_blocked, id=3212]

0x00a50a70 JavaThread "Reference Handler" daemon [_thread_blocked, id=3208]

=>0x00036158 JavaThread "main" [_thread_in_native, id=3204]

Other Threads:

0x00a4e178 VMThread [id=1952]

0x00a808e0 WatcherThread [id=3220]

VM state:not at safepoint (normal execution)

VM Mutex/Monitor currently owned by a thread: None

Heap

def new generationtotal 576K, used 238K [0x22bd0000, 0x22c70000, 0x230b0000)

eden space 512K, 46% used [0x22bd0000, 0x22c0b930, 0x22c50000)

from space 64K,0% used [0x22c50000, 0x22c50000, 0x22c60000)

tospace 64K,0% used [0x22c60000, 0x22c60000, 0x22c70000)

tenured generationtotal 1408K, used 0K [0x230b0000, 0x23210000, 0x26bd0000)

the space 1408K,0% used [0x230b0000, 0x230b0000, 0x230b0200, 0x23210000)

compacting perm gen total 8192K, used 339K [0x26bd0000, 0x273d0000, 0x2abd0000)

the space 8192K,4% used [0x26bd0000, 0x26c24e90, 0x26c25000, 0x273d0000)

ro space 8192K, 67% used [0x2abd0000, 0x2b12d9f8, 0x2b12da00, 0x2b3d0000)

rw space 12288K, 46% used [0x2b3d0000, 0x2b973808, 0x2b973a00, 0x2bfd0000)

Dynamic libraries:

0x00400000 - 0x0040d000 c:\Program Files\Java\jdk1.5.0_07\bin\java.exe

0x7c920000 - 0x7c9b4000 C:\WINDOWS\system32\ntdll.dll

0x7c800000 - 0x7c91c000 C:\WINDOWS\system32\kernel32.dll

0x77da0000 - 0x77e49000 C:\WINDOWS\system32\ADVAPI32.dll

0x77e50000 - 0x77ee1000 C:\WINDOWS\system32\RPCRT4.dll

0x77be0000 - 0x77c38000 C:\WINDOWS\system32\MSVCRT.dll

0x6d730000 - 0x6d8c7000 c:\Program Files\Java\jdk1.5.0_07\jre\bin\client\jvm.dll

0x77d10000 - 0x77d9f000 C:\WINDOWS\system32\USER32.dll

0x77ef0000 - 0x77f37000 C:\WINDOWS\system32\GDI32.dll

0x76b10000 - 0x76b3a000 C:\WINDOWS\system32\WINMM.dll

0x76300000 - 0x7631d000 C:\WINDOWS\system32\IMM32.DLL

0x62c20000 - 0x62c29000 C:\WINDOWS\system32\LPK.DLL

0x73fa0000 - 0x7400b000 C:\WINDOWS\system32\USP10.dll

0x6d2f0000 - 0x6d2f8000 c:\Program Files\Java\jdk1.5.0_07\jre\bin\hpi.dll

0x76bc0000 - 0x76bcb000 C:\WINDOWS\system32\PSAPI.DLL

0x6d700000 - 0x6d70c000 c:\Program Files\Java\jdk1.5.0_07\jre\bin\verify.dll

0x6d370000 - 0x6d38d000 c:\Program Files\Java\jdk1.5.0_07\jre\bin\java.dll

0x6d720000 - 0x6d72f000 c:\Program Files\Java\jdk1.5.0_07\jre\bin\zip.dll

0x10000000 - 0x10033000 E:\jnis\sample.dll

VM Arguments:

java_command: HelloWorld

Launcher Type: SUN_STANDARD

Environment Variables:

JAVA_HOME=C:\Program Files\Java\jdk1.5.0_07

CLASSPATH=.;.;C:\PROGRA~1\JMF21~1.1E\lib\sound.jar;C:\PROGRA~1\JMF21~1.1E\lib\jmf.jar;C:\PROGRA~1\JMF21~1.1E\lib;

PATH=C:\cygwin\usr\local\bin;C:\cygwin\bin;C:\cygwin\bin;C:\cygwin\usr\X11R6\bin;c:\WINDOWS\system32;c:\WINDOWS;c:\WINDOWS\System32\Wbem;c:\Program Files\Java\jdk1.5.0_07\bin;c:\Program Files\OPENXTRA\Ethereal;c:\Program Files\OPENXTRA\WinDump;d:\Program Files\jProbe\bin;c:\maven-2.0.6-bin\maven-2.0.6\bin;d:\Open Source\apache-ant-1.7.0\bin;d:\swtdlls;d:\Program Files\CVSNT\;c:\Program Files\OPENXTRA\WinPCap

USERNAME=Administrator

OS=Windows_NT

PROCESSOR_IDENTIFIER=x86 Family 15 Model 4 Stepping 9, 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 515572k(238244k free), swap 1260260k(935080k free)

vm_info: Java HotSpot(TM) Client VM (1.5.0_07-b03) for windows-x86, built on May 3 2006 01:04:38 by "java_re" with MS VC++ 6.0

Java Code like this:

static{

try{

System.loadLibrary("sample");

}catch(Exception e){

System.out.println("Can't load sample.dll");

}

}

//Declare native method

publicnativevoid sayHello(String name);

publicstaticvoid main(String[] args){

HelloWorld hw=new HelloWorld();

hw.sayHello("QUKI");

}

And C code like this:

JNIEXPORTvoid JNICALL Java_HelloWorld_sayHello

(JNIEnv *env, jobject obj, jstring s)

{

/* Obtain a C-copy of the Java string */

//printf("Hello,every one");

constchar *str = (*env)->GetStringUTFChars(env, s, 0);

printf("Java Call C success....%s\n",*str);

}

Any one can tell me why?Thanx very much.

[7766 byte] By [Liusua] at [2007-11-27 3:38:25]
# 1
Hmm, how did you compile?const char *str = env->GetStringUTFChars( s, 0 );printf("Java Call C success....%s\n",str);env->ReleaseStringUTFChars( s, str );
Michael.Nazarov@sun.coma at 2007-7-12 8:41:44 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2
> Hmm, how did you compile?Perhaps because it was written in C?> const char *str = env->GetStringUTFChars( s, 0 );> printf("Java Call C success....%s\n",str);> env->ReleaseStringUTFChars( s, str );This is C++. Which is not the same as
jschella at 2007-7-12 8:41:44 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3

> const char *str = (*env)->GetStringUTFChars(env, s, 0);

> printf("Java Call C success....%s\n",*str);

> }

>

> Any one can tell me why?Thanx very much.

That asterisk(*) in front of str in the printf() statement is dereferencing the variable. Thus it is a char and not a char*. But %s requires a char*.

> EXCEPTION_ACCESS_VIOLATION (0xc0000005)

Note that when you get the above, with the ...05 error, that it always means you did something incorrect with a pointer somewhere.

jschella at 2007-7-12 8:41:44 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4
> Perhaps because it was written in C?Perhaps.> This is C++. Which is not the same as C.Really not the same? :)
Michael.Nazarov@sun.coma at 2007-7-12 8:41:44 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 5

> > Perhaps because it was written in C?

>

> Perhaps.

>

> > This is C++. Which is not the same as C.

>

> Really not the same? :)

Definitely not the same.

And the OPs code was correct for C. Your implied suggestion was that it wouldn't compile because it wasn't correct for C++.

Your implication was incorrect.

jschella at 2007-7-12 8:41:44 > top of Java-index,Java HotSpot Virtual Machine,Specifications...