Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何处理javajni不一致行为_Java_Dll_Java Native Interface - Fatal编程技术网

如何处理javajni不一致行为

如何处理javajni不一致行为,java,dll,java-native-interface,Java,Dll,Java Native Interface,我在Java JNI函数上面临不一致的行为。相同的代码在不同的环境中表现出不同的行为。我需要你的帮助 我使用java JNI和C++创建了一个函数DLL文件。该函数将字符串作为唯一的参数并读取该字符串。我创建了一个测试程序来测试DLL。在测试程序中有一个JNI类CppWrapper,用于加载DLL并调用DLL中的本机函数。试验成功了。 下面是JNI类 public class CppWrapper { static{ String jvmBits = Sys

我在Java JNI函数上面临不一致的行为。相同的代码在不同的环境中表现出不同的行为。我需要你的帮助

我使用java JNI和C++创建了一个函数DLL文件。该函数将字符串作为唯一的参数并读取该字符串。我创建了一个测试程序来测试DLL。在测试程序中有一个JNI类CppWrapper,用于加载DLL并调用DLL中的本机函数。试验成功了。 下面是JNI类

public class CppWrapper {       
    static{
        String jvmBits = System.getProperty("os.arch");
        if(jvmBits.equals("x86")){
            System.loadLibrary("voice32");
            System.out.println(“CppWrapper voice32 loaded”);    
        }else{
            System.loadLibrary("voice64");
            System.out.println(“CppWrapper voice64 loaded”);
        }    
    }   
    native public static int speak(String str);      
}
在JNI类中调用本机方法:

String line = toSpeakTextArea.getText();
CppWrapper.speak(line);
我将整个JNI类复制到我正在处理的应用程序中,确切的代码导致了“java.lang.UnsatifiedLinkError”。已加载DLL文件,但找不到DLL中的函数。在测试程序中更改JNI类的名称会导致相同的错误。将原始测试程序带到其他PC机也会以同样的方式失败

我将测试程序制作成一个jar文件,并将其用作我的应用程序的依赖项

现在的情况是,与JNI完全相关的代码只能在特定类名下的特定PC上工作。否则它将失败。如何解决这个问题

我使用Netbeans 8.01、JDK1.7.0-67和JRE1.7.0-67。所有项目都是Maven项目

下面的错误消息来自在不同的PC上运行原始测试程序

Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: com.mycompany.speechuserxp.CppWrapper.speak(Ljava/lang/String;)I
at com.mycompany.speechuserxp.CppWrapper.speak(Native Method)
at com.mycompany.speechuserxp.SpeechUserXP.speakButtonActionPerformed(SpeechUserXP.java:143)
at com.mycompany.speechuserxp.SpeechUserXP.access$100(SpeechUserXP.java:15)
at com.mycompany.speechuserxp.SpeechUserXP$2.actionPerformed(SpeechUserXP.java:97)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

您是否尝试使用带有绝对路径名的
System.load()
,而不是
System.loadLibrary()
?@SamuelAudet我尝试过。我收到一条错误消息,说“在library.path上找不到c:/windows/system32/voice32.dll”