NetBeans开发7 - Windows 7的64位... JNI本地电话......一个如何引导 [英] NetBeans Development 7 - Windows 7 64-bit … JNI native calls ... a how to guide

查看:254
本文介绍了NetBeans开发7 - Windows 7的64位... JNI本地电话......一个如何引导的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提供这个给你希望节省您的一些时间和痛苦。

由于我在结识NB的发展V7我的Windows 64位工作站上我发现了另一个令人沮丧的冒险试图让JNI(Java本地接口)的能力和在我的项目工作经历的一部分。因此,我包括所需的步骤简要总结(因为所有我发现的文件是完全不正确的就怎么做JNI这些版本的Windows和NetBeans)。它花了几个实验的天,并审议每一个网页,我能找到的,包括这些技术作为关键字搜索。育!不好玩。

首先,因为NetBeans开发是所有关于模块,如果你正在读这篇文章,你可能有你的模块需要一个或多个,以执行JNI调用。大多数的什么是可在本网站或一般的互联网(更不用说NB7帮助文件),要么是完全错误的,这些版本中,左右稀疏,以基本上无用以外的其他人JNI的专家。

下面是你在找什么......的切入正题 - 如何引导来获取一个JNI调用和处理您的NB7 / Windows 64位中

1)从你的NetBeans模块(而非主机应用程序内)声明本机方法(S),并确保你可以编译Java源代码没有错误。

例如:

 包org.mycompanyname.nativelogic;

    公共类NativeInterfaceTest
    {
        静止
        {
            尝试
            {
                如果(System.getProperty(os.arch).toLowerCase()。等于(AMD64))
                    的System.loadLibrary(小于64 bit_folder_name_on_file_system> /&其中; file_name.dll&GT);
                其他
                    的System.loadLibrary(小于32 bit_folder_name_on_file_system> /&其中; file_name.dll&GT);
            }
            赶上(SecurityException异常SE){}
            赶上(UnsatisfieldLinkError ULE){}
            赶上(NullPointerException异常NPE){}
        }

        公共NativeInterfaceTest(){}

        本地字符串echoString的(String s)将;
    }
 

留意了这样的事实,我们只载入大会一次(因为它是在静态块),否则你会抛出异常,如果尝试再次装入。同时注意标题为echoString的我们的单(在这个例子中)本地方法。这是我们的C / C ++应用程序要实现的方法,然后通过JNI的MAJIC,我们会打电话给我们的Java code。

2)如果使用Windows的64位版本(我们在这里),我们需要打开一个64位的Visual Studio命令提示符(相对于标准的32位版本),并执行vcvarsallBAT文件,以及一个AMD64命令行参数,来设置环境了64位工具。

例如:

 < path_to_Microsoft_Visual_Studio_10.0> /VC/vcvarsall.bat AMD64
 

请注意,你可以使用你希望的任何版本的C / C ++编译器从微软。我正好有安装在我的箱子Visual Studio 2005中,2008年和2010年,所以我选择了用10.0,但任何支持64位开发将正常工作。这里的另一个重要方面是AMD64参数。

3)在命令提示符变化的驱动器\你的计算机上的目录,以便你在文件系统上的完全限定类的位置,其中包含本机方法声明的根源。

例: 我的本地声明的方法的完全限定类名是org.mycompanyname.nativelogic.NativeInterfaceTest。当我们成功编译我们的Java在上述步骤1中,我们会发现它包含在我们类似下面的NetBeans模块的内容:

/编译/班/组织/ mycompanyname / nativelogic / NativeInterfaceTest.class

我们需要确保我们的命令提示符套,直接作为当前,/编译/班,因为我们的下一个步骤。

4)在这一步中,我们将创建我们的C / C ++头文件,该文件包含JNI所需的报表。请在命令提示符下键入以下:

javah的-jni org.mycompanyname.nativelogic.NativeInterfaceTest ,然后回车。如果您收到任何类型的错误,指出这是一个无法识别的命令,它只是意味着你的Windows电脑不知道的路径命令(它在你的/ bin文件夹)。无论是调用该应用程序运行时,从那里的命令,或包括完全合格的路径名,或将您的计算机的PATH环境变量,包括在其搜索这条道路。

这应该产生一种叫org_mycompanyname_nativelogic_NativeInterfaceTest.h...... C头文件的文件。我会做这个副本,以防以后需要一个备份。

5)编辑NativeInterfaceTest.h头文件,包括为echoString的()方法的实现。

例如:

  JNIEXPORT的jstring JNICALL Java_org_mycompanyname_nativelogic_NativeInterfaceTest_echoString
  (JNIEnv的* ENV,jobject jobj,JS的jstring)
{
    返回((* ENV) - > NewStringUTF(ENV,我的JNI是和大量的研究工作之后));
}
 

请注意,你不能简单地返回一个普通的Java字符串(因为你用C的时刻)。你必须告诉传入的JVM变量来创建一个Java String作为您将会返回。看看下面的甲骨文网页的其他数据类型以及如何创建它们的JNI的目的。

6)关闭并保存更改的头文件。现在你已经增加了实施的头改变文件扩展名从.H到.c的,因为它现在是一个C源$ C ​​$ C是正确实现了JNI所需的接口文件。

例:   NativeInterfaceTest.c

7)我们需要编译新创建的来源$ C ​​$ C文件,并链接了。从在命令提示符下键入以下内容:

CL /我path_to_my_jdks_include_folder/我path_to_my_jdks_include_win32_folder/ D:AMD64 = 1 / LD NativeInterfaceTest.c /FeNativeInterfaceTest.dll /链路/设备:64

例如:

  CL /我D:/ Program Files文件/的Java / jdk1.6.0_21 /包括/我D:/ Program Files文件/爪哇/ jdk1.6.0_21 /有/ win32的/ D:AMD64 = 1 / LD NativeInterfaceTest.c /FeNativeInterfaceTest.dll /链路/设备:64
 

注意周围路径引号的有和有/ win32的文件夹是必需的,因为我有空间在我的文件夹名称......程序文件,你可以包括他们,如果您有没有问题,没有空格,但都是强制性的,如果你使用命令提示符时,有空格。

这将生成多个文件,但它是我们感兴趣的DLL。这就是System.loadLirbary()Java方法所期待的。

8)Congratuations!你在最后一步。干脆把DLL组件,它在以下位置粘贴:

<$p$p><$c$c><path_of_NetBeansProjects_folder>/<project_name>/<module_name>/build/cluster/modules/lib/x64

请注意,您可能要创建lib目录和64的文件夹。

 示例:
C:\Users\<user_name>\Documents\NetBeansProjects\<application_name>\<module_name>\build\cluster\modules\lib\x64\NativeInterfaceTest.dll
 

Java的code ...注意如何我们不inlude在调用LoadLibrary带.DLL文件扩展名()调用?

 的System.loadLibrary(/ 64 / NativeInterfaceTest);
 

现在,在您的Java code,你可以创建一个NativeInterfaceTest对象,并调用echoString的()方法,它会回报你的NativeInterfaceTest.c源$ C ​​$ C文件输入的字符串值。

希望这会节省你的脑残我忍着试图找出这一切都自己出去。祝你好运和快乐编码!

解决方案

由于这个问题不是一个问题,而是一个HOWTO,我的回答是不是一个答案,但一个howIdo:

您32/64技术需要的DLL文件单独的目录。

 如果(System.getProperty(os.arch).toLowerCase()。等于(AMD64))
   的System.loadLibrary(小于64 bit_folder_name_on_file_system&GT; /&其中; file_name.dll&GT);
其他
   的System.loadLibrary(小于32 bit_folder_name_on_file_system&GT; /&其中; file_name.dll
 

这是howIdo:

 字符串archDataModel = System.getProperty(sun.arch.data.model);
的System.loadLibrary(DLLNAME+ archDataModel);
 

,它允许dllName32.dll和dllName64.dll被包装在相同的目录。

......又一个人的意见。

I provide this for you to hopefully save you some time and pain.

As part of my experience in getting to know NB Development v7 on my Windows 64-bit workstation I found another frustrating adventure in trying to get the JNI (Java Native Interface) abilities up and working in my project. As such, I am including a brief summary of steps required (as all the documentation I found was completely incorrect for these versions of Windows and NetBeans on how to do JNI). It took a couple of days of experimentation and reviewing every webpage I could find that included these technologies as keyword searches. Yuk!! Not fun.

To begin, as NetBeans Development is "all about modules" if you are reading this you probably have a need for one, or more, of your modules to perform JNI calls. Most of what is available on this site or the Internet in general (not to mention the help file in NB7) is either completely wrong for these versions, or so sparse as to be essentially unuseful to anyone other than a JNI expert.

Here is what you are looking for ... the "cut to the chase" - "how to guide" to get a JNI call up and working on your NB7 / Windows 64-bit box.

1) From within your NetBeans Module (not the host application) declare your native method(s) and make sure you can compile the Java source without errors.

Example:

package org.mycompanyname.nativelogic;

    public class NativeInterfaceTest
    {
        static
        {
            try
            {
                if (System.getProperty( "os.arch" ).toLowerCase().equals( "amd64" ) )
                    System.loadLibrary( <64-bit_folder_name_on_file_system>/<file_name.dll> );
                else
                    System.loadLibrary( <32-bit_folder_name_on_file_system>/<file_name.dll> );
            }
            catch (SecurityException se) {}
            catch (UnsatisfieldLinkError ule) {}
            catch (NullPointerException npe) {}
        }

        public NativeInterfaceTest() {}

        native String echoString(String s);
    }

Take notice to the fact that we only load the Assembly once (as it's in a static block), because otherwise you will throw exceptions if attempting to load it again. Also take note of our single (in this example) native method titled "echoString". This is the method that our C / C++ application is going to implement, then via the majic of JNI we'll call from our Java code.

2) If using a 64-bit version of Windows (which we are here) we need to open a 64-bit Visual Studio Command Prompt (versus the standard 32-bit version), and execute the "vcvarsall" BAT file, along with an "amd64" command line argument, to set the environment up for 64-bit tools.

Example:

<path_to_Microsoft_Visual_Studio_10.0>/VC/vcvarsall.bat amd64

Take note that you can use any version of the C / C++ compiler from Microsoft you wish. I happen to have Visual Studio 2005, 2008, and 2010 installed on my box so I chose to use "v10.0" but any that support 64-bit development will work fine. The other important aspect here is the "amd64" param.

3) In the Command Prompt change drives \ directories on your computer so that you are at the root of the fully qualified Class location on the file system that contains your native method declaration.

Example: The fully qualified class name for my natively declared method is "org.mycompanyname.nativelogic.NativeInterfaceTest". As we successfully compiled our Java in Step 1 above, we should find it contained in our NetBeans Module something similar to the following:

"/build/classes/org/mycompanyname/nativelogic/NativeInterfaceTest.class"

We need to make sure our Command Prompt sets, as the current directly, "/build/classes" because of our next step.

4) In this step we'll create our C / C++ Header file that contains the JNI required statements. Type the following in the Command Prompt:

javah -jni org.mycompanyname.nativelogic.NativeInterfaceTest and hit enter. If you receive any kind of error that states this is an unrecognized command that simply means your Windows computer does not know the PATH to that command (it's in your /bin folder). Either run the command from there, or include the fully qualified path name when invoking this application, or set your computer's PATH environmental variable to include that path in its search.

This should produce a file called "org_mycompanyname_nativelogic_NativeInterfaceTest.h" ... a C Header file. I'd make a copy of this in case you need a backup later.

5) Edit the NativeInterfaceTest.h header file and include an implementation for the echoString() method.

Example:

JNIEXPORT jstring JNICALL Java_org_mycompanyname_nativelogic_NativeInterfaceTest_echoString
  (JNIEnv *env, jobject jobj, jstring js)
{
    return((*env)->NewStringUTF(env, "My JNI is up and working after lots of research"));
}

Notice how you can't simply return a normal Java String (because you're in C at the moment). You have to tell the passed in JVM variable to create a Java String for you that will be returned back. Check out the following Oracle web page for other data types and how to create them for JNI purposes.

6) Close and Save your changes to the Header file. Now that you've added an implementation to the Header change the file extension from ".h" to ".c" as it's now a C source code file that properly implements the JNI required interface.

Example: NativeInterfaceTest.c

7) We need to compile the newly created source code file and Link it too. From within the Command Prompt type the following:

cl /I"path_to_my_jdks_include_folder" /I"path_to_my_jdks_include_win32_folder" /D:AMD64=1 /LD NativeInterfaceTest.c /FeNativeInterfaceTest.dll /link /machine:x64

Example:

cl /I"D:/Program Files/Java/jdk1.6.0_21/include" /I"D:/Program Files/java/jdk1.6.0_21/include/win32" /D:AMD64=1 /LD NativeInterfaceTest.c /FeNativeInterfaceTest.dll /link /machine:x64

Notice the quotes around the paths to the 'include" and 'include/win32' folders is required because I have spaces in my folder names ... 'Program Files'. You can include them if you have no spaces without problems, but they are mandatory if you have spaces when using a command prompt.

This will generate several files, but it's the DLL we're interested in. This is what the System.loadLirbary() java method is looking for.

8) Congratuations! You're at the last step. Simply take the DLL Assembly and paste it at the following location:

<path_of_NetBeansProjects_folder>/<project_name>/<module_name>/build/cluster/modules/lib/x64

Note that you'll probably have to create the "lib" and "x64" folders.

Example:
C:\Users\<user_name>\Documents\NetBeansProjects\<application_name>\<module_name>\build\cluster\modules\lib\x64\NativeInterfaceTest.dll

Java code ... notice how we don't inlude the ".dll" file extension in the loadLibrary() call?

System.loadLibrary( "/x64/NativeInterfaceTest" );

Now, in your Java code you can create a NativeInterfaceTest object and call the echoString() method and it will return the String value you typed in the NativeInterfaceTest.c source code file.

Hopefully this will save you the brain damage I endured trying to figure all this out on my own. Good luck and happy coding!

解决方案

Since this question is not a question but a howto, my answer is not an answer but a howIdo:

Your 32/64 technique requires seperate directories for the DLLs.

if (System.getProperty( "os.arch" ).toLowerCase().equals( "amd64" ) )
   System.loadLibrary( <64-bit_folder_name_on_file_system>/<file_name.dll> );
else
   System.loadLibrary( <32-bit_folder_name_on_file_system>/<file_name.dll

This is howIdo:

String archDataModel = System.getProperty("sun.arch.data.model");
System.loadLibrary("dllName" + archDataModel);

which allows dllName32.dll and dllName64.dll to be packaged in the same directory.

...Just another man's opinion.

这篇关于NetBeans开发7 - Windows 7的64位... JNI本地电话......一个如何引导的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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