JNI:一个典型的错误——A fatal error has been detected by the Java Runtime Environment
2012-05-16 00:59
495 查看
原因:传递给本地函数的对象为null
// string操作
class JNIString
{
static
{
System.loadLibrary("JNIString");
}
public static native String strMethod(String s);
}
/*
javac JNIString.java
javah JNIString.java // get the file JNIString.h
*/
#include "JNIString.h"
#include <string.h>
JNIEXPORT jstring JNICALL Java_JNIString_strMethod(JNIEnv *env, jclass jcls, jstring jstr)
{
int i;
char str2[128]; // 变量定义只能在程序开头部分
//jsize len = env->GetStringLength(jstr);
const char *str = env->GetStringUTFChars(jstr, NULL); // must be const
// const jchar * GetStringChars(JNIEnv *env, jstring string, jboolean *isCopy);
printf("%s\n", str);
strcpy(str2,str);
env->ReleaseStringUTFChars(jstr, str);
return env->NewStringUTF(str2);
}
/*
set java_inc=E:\FILES\java\jdk1.6.0_29\include
cl -I%java_inc% -I%java_inc%\win32 -LD JNIString.c -FeJNIString.dll
*/
public class Test
{
public static void main(String[] args)
{
String str = "abc123abc"; // 123中国123湖北123武汉123
String s1 = new String(str);
String s2 = JNIString.strMethod(s1);
System.out.println(s2);
str = null;
s2 = JNIString.strMethod(str);
}
}
/* output:
abc123abc
abc123abc
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d9332ca, pid=4764, tid=5056
#
# JRE version: 6.0_29-b11
# Java VM: Java HotSpot(TM) Client VM (20.4-b02 mixed mode, sharing windows-x86
)
# Problematic frame:
# V [jvm.dll+0x932ca]
#
# An error report file with more information is saved as:
# G:\编程\Java\test\hs_err_pid4764.log
#
# If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp #
*/
// string操作
class JNIString
{
static
{
System.loadLibrary("JNIString");
}
public static native String strMethod(String s);
}
/*
javac JNIString.java
javah JNIString.java // get the file JNIString.h
*/
#include "JNIString.h"
#include <string.h>
JNIEXPORT jstring JNICALL Java_JNIString_strMethod(JNIEnv *env, jclass jcls, jstring jstr)
{
int i;
char str2[128]; // 变量定义只能在程序开头部分
//jsize len = env->GetStringLength(jstr);
const char *str = env->GetStringUTFChars(jstr, NULL); // must be const
// const jchar * GetStringChars(JNIEnv *env, jstring string, jboolean *isCopy);
printf("%s\n", str);
strcpy(str2,str);
env->ReleaseStringUTFChars(jstr, str);
return env->NewStringUTF(str2);
}
/*
set java_inc=E:\FILES\java\jdk1.6.0_29\include
cl -I%java_inc% -I%java_inc%\win32 -LD JNIString.c -FeJNIString.dll
*/
public class Test
{
public static void main(String[] args)
{
String str = "abc123abc"; // 123中国123湖北123武汉123
String s1 = new String(str);
String s2 = JNIString.strMethod(s1);
System.out.println(s2);
str = null;
s2 = JNIString.strMethod(str);
}
}
/* output:
abc123abc
abc123abc
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d9332ca, pid=4764, tid=5056
#
# JRE version: 6.0_29-b11
# Java VM: Java HotSpot(TM) Client VM (20.4-b02 mixed mode, sharing windows-x86
)
# Problematic frame:
# V [jvm.dll+0x932ca]
#
# An error report file with more information is saved as:
# G:\编程\Java\test\hs_err_pid4764.log
#
# If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp #
*/
相关文章推荐
- 一个JVM的错误让我遇到了:A fatal error has been detected by the Java Runtime Environment
- A fatal error has been detected by the Java Runtime Environment(jdk 1.6的一个BUG)
- A fatal error has been detected by the Java Runtime Environment(jdk 1.6的一个BUG)
- A fatal error has been detected by the Java Runtime Environment程序错误处理方法
- tomcat启动报错,A fatal error has been detected by the Java Runtime Environment
- tomcat自动关闭:A fatal error has been detected by the Java Runtime Environment
- Tomcat启动:A fatal error has been detected by the Java Runtime Environment(JVM Crash分析及相关资料)
- JAVA VM bug :A fatal error has been detected by the Java Runtime Environment
- Tomcat启动:A fatal error has been detected by the Java Runtime Environment(JVM Crash分析及相关资料)
- A fatal error has been detected by the Java Runtime Environment:
- A fatal error has been detected by the Java Runtime Environment:SIGSEGV
- Tomcat启动:A fatal error has been detected by the Java Runtime Environment(JVM Crash分析及相关资料)
- A fatal error has been detected by the Java Runtime Environment--排错
- tomcat启动报错,A fatal error has been detected by the Java Runtime Environment:
- Debain启动MyEclipse2015报错:A fatal error has been detected by the Java Runtime Environment:
- A fatal error has been detected by the Java Runtime Environment:
- JNA : A fatal error has been detected by the Java Runtime Environment
- A fatal error has been detected by the Java Runtime Environment(JVM Crash分析及相关资料)
- tomcat启动报错,A fatal error has been detected by the Java Runtime Environment: