1:创建Helloworld.java 文件代码:class Helloworld{public native void displayHelloworld();static{System.loadLibrary("hello");}public static void main(String[] args){new Helloworld().displayHelloworld();}}E:\javalesson\lesson3>javac Helloworld.java2:创建头文件Helloworld.hE:\javalesson\lesson3>javah -jni Helloworld代码:/* DO NOT EDIT THIS FILE - it is machine generated */#include /* Header for class Helloworld */#ifndef _Included_Helloworld#define _Included_Helloworld#ifdef __cplusplusextern "C" {#endif/** Class: Helloworld* Method: displayHelloworld* Signature: ()V*/JNIEXPORT void JNICALLJava_Helloworld_displayHelloworld(JNIEnv *, jobject);#ifdef __cplusplus}#endif#endif3:本地方法的实现在lesson3目录下创建HelloworldImp.c代码:#include#include"Helloworld.h"#include JNIEXPORT void JNICALL Java_Helloworld_displayHelloworld(JNIEnv *env, jobject obj){printf("Hello world!\n");return ;}4:创建共享库E:\javalesson\lesson3>cl -ID:\Program Files\Java\jdk1.6.0_22\include -ID:\Program Files\Java\jdk1.6.0_22\include\win32 -LD HelloworldImp.cm -Fehello.dll问题出在这了Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86Copyright (C) Microsoft Corp 1984-1998. All rights reserved.Command line warning D4024 : unrecognized source file type 'Files\Java\jdk1.6.0_22\include', object file assumedCommand line warning D4024 : unrecognized source file type 'Files\Java\jdk1.6.0_22\include\win32', object file assumedCommand line warning D4024 : unrecognized source file type 'HelloworldImp.cm', object file assumedMicrosoft (R) Incremental Linker Version 6.00.8168Copyright (C) Microsoft Corp 1992-1998. All rights reserved./dll/implib:hello.lib/out:hello.dllFiles\Java\jdk1.6.0_22\includeFiles\Java\jdk1.6.0_22\include\win32HelloworldImp.cmLINK : fatal error LNK1181: cannot open input file "Files\Java\jdk1.6.0_22\include.obj"这里我崩溃了 找了好久都找不到原因…… 
请大侠指教

解决方案 »

  1.   

    我也在学习JNI,或许我能解答你的问题:
    1、报出这一段错误时你的目录名"Program Files"导致的,因为它中间有个空格(这个问题让我决定VC++真的很弱智),其实你可以把jdk的安装目录找到jni.h和jni_md.h这俩文件给复制到HelloworldImp.c所在的目录即可:
    Command line warning D4024 : unrecognized source file type 'Files\Java\jdk1.6.0_22\include', object file assumedCommand line warning D4024 : unrecognized source file type 'Files\Java\jdk1.6.0_22\include\win32', object file assumed
    2、这一句Command line warning D4024 : unrecognized source file type 'HelloworldImp.cm', object file assumed错误信息很明显,你的命令里将HelloworldImp.c写成了HelloworldImp.cm,所以就报出了一个文件类型错误!
    希望这些对你有所帮助!
      

  2.   

    创建动态链接库出错,我没在windows下边搞过动态链接库,估计是由于Program Files空格的原因.你做一个转义:Program\ Files