Windows下的JNI和C入门 [英] Getting Started with JNI and C under Windows

查看:102
本文介绍了Windows下的JNI和C入门的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java学习的新手,第一次想开始使用JNI.我正在使用Cygwin,并且创建了一个扩展名为.java(Helloworld.java)的文件,如下所示:

I'm new in Java learning and first time want to get start JNI. And I am working with Cygwin and I have created a file with .java (Helloworld.java) extension as follows:

class HelloWorld {
     private native void print();
     public static void main(String[] args) {
         new HelloWorld().print();
     }
     static {
         System.loadLibrary("HelloWorld");
     }
 }

然后我通过命令行(javac Helloworld.java)编译文件,然后通过命令javah –jni Helloworld创建本机头文件 然后还通过创建一个C文件来实现:

Then I compile the file through the command line (javac Helloworld.java) after that create a native header file through command javah –jni Helloworld Then also implement by creating a C file as:

#include <jni.h>
 #include <stdio.h>
 #include "HelloWorld.h"

 JNIEXPORT void JNICALL 
 Java_HelloWorld_print(JNIEnv *env, jobject obj)
 {
     printf("Hello World!\n");
     return;
 }

在那之后,我运行命令

 Gcc –wall –g Helloworld.c –o hello 

在Cygwin的命令行提示符中给出消息

Give the message in command prompt line in Cygwin

 helloworld.c:1:17: fatal error: jni.h: No such file or directory
 compilation terminated.

如何运行以及设置执行文件路径的过程是什么?请任何人帮助我.谢谢.

How to run and what is the procedure to set the path to execute the file? Please help me any one. Thanks.

推荐答案

我在Windows上使用了MinGW.在将; C:\ MinGW \ bin \"添加到路径环境变量后,我可以使用以下命令从Windows命令提示符运行gcc.exe.

I used MinGW on Windows. After adding ";C:\MinGW\bin\" to the Path Environment variable, I was able to run gcc.exe from the Windows command prompt using the command below.

jni.h文件位于java home包含文件夹中.您可以使用JAVA_HOME环境变量或明确指定.

The jni.h file is located in java home include folder. You can use the JAVA_HOME environment variable or specify explicitly.

 gcc -c -I"C:\Program Files\Java\jdk1.6.0_26\include" -I"C:\Program Files\Java\jdk1.6.0_26\include\win32" "S:\Accessibility\tools\src\test\java\com\accessibility\HelloWorld.c"

这创建了一个HelloWorld.o文件.

This created a HelloWorld.o file.

这篇关于Windows下的JNI和C入门的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆