Java JNI在C中将jint转换为本机int

Java JNI在C中将jint转换为本机int,java,c,java-native-interface,Java,C,Java Native Interface,我在FileOutputStream.java中声明了以下函数 public native void write(int b) throws IOException; 我在书中读到,要将jint参数转换为原生int,只需将其强制转换。我的c代码: JNIEXPORT void JNICALL Java_FileOutputStream_write__I(JNIEnv* jni, jobject obj, jint b){ int native_b = (int)b; print

我在FileOutputStream.java中声明了以下函数

public native void write(int b) throws IOException;
我在书中读到,要将jint参数转换为原生int,只需将其强制转换。我的c代码:

JNIEXPORT void JNICALL Java_FileOutputStream_write__I(JNIEnv* jni, jobject obj, jint b){
    int native_b = (int)b;
    printf(b);
}
如果在java中调用该函数,则会收到以下错误消息:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007ffc7e01f3b2, pid=8700, tid=0x00000000000020e4
#
# JRE version: Java(TM) SE Runtime Environment (8.0_112-b15) (build 1.8.0_112-b15)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.112-b15 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [msvcrt.dll+0x4f3b2]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# <my_path>\JNI\hs_err_pid8700.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
#
#Java运行时环境检测到一个致命错误:
#
#pc=0x00007ffc7e01f3b2,pid=8700,tid=0x00000000000020e4时出现异常访问违规(0xc0000005)
#
#JRE版本:Java(TM)SE运行时环境(8.0_112-b15)(build 1.8.0_112-b15)
#Java虚拟机:Java热点(TM)64位服务器虚拟机(25.112-b15混合模式windows-amd64压缩oops)
#有问题的框架:
#C[msvcrt.dll+0x4f3b2]
#
#无法写入核心转储。默认情况下,在客户端版本的Windows上不启用小型转储
#
#包含详细信息的错误报告文件另存为:
#\JNI\hs\u err\u pid8700.log
#
#如果您想提交错误报告,请访问:
#   http://bugreport.java.com/bugreport/crash.jsp
#崩溃发生在Java虚拟机外部的本机代码中。
#有关报告错误的位置,请参见问题框。
#

所以我猜我的演员阵容是错的。我必须做些什么才能使它正确?

强制转换
jint
没有问题,但是
printf
需要一个带参数的格式字符串


您的代码试图访问地址
b
处的
char*
,由于
b
不是实际地址,因此会崩溃。查找编译器的
printf
文档(或任何
printf
文档,例如:)。

aah,愚蠢的错误。。。将其更改为printf(“%d”,b);而且它工作得很好!非常感谢!