JNI%1不是有效的Win32应用程序 [英] JNI %1 is not a valid Win32 application

查看:6980
本文介绍了JNI%1不是有效的Win32应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在64位上运行Netbeans的Windows 8中,使用JDK 1.7_25(64位),之后为始JNI与NetBeans说明书(的 https://netbeans.org/kb/docs/cnd/beginning-jni-linux.html

的说明适用于Linux的,但原理是一样的Windows,我相信(生成一个.dll文件而不是。所以,用win32包括在JDK等)

我已经安装了Cygwin64以及Cygwin32。使用Cygwin64,我可以生成从我的C / C ++动态库项目中的64位的DLL。然而,当我打电话System.load(路径/到/ JNITest.dll),我得到的:

 在线程异常主要java.lang.UnsatisfiedLinkError中:C:\用户\安德鲁\文档\的NetBeansProjects \ JNITestLib \ DIST \ JNITest.dll:%1不是有效的Win32应用
在java.lang.ClassLoader的$ NativeLibrary.load(本机方法)
    在java.lang.ClassLoader.loadLibrary1(ClassLoader.java:1957)
    在java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1882)
    在java.lang.ClassLoader.loadLibrary(ClassLoader.java:1843)
    在java.lang.Runtime.load0(Runtime.java:795)
    在java.lang.System.load(System.java:1061)
    在jnitest.JNITest.main(JNITest.java:8)
Java结果:1
 

这是我收集,这是最常见的加载32位的虚拟机上的64位应用程序的时候,但我的netbeans.conf指向一个64位JVM的情况下。

此外,当我使用32位版本的Cygwin编译东西和运行,我得到

 无法加载IA 32位的.dll在AMD 64位平台
 

我是pretty的肯定,我正确生成的DLL文件,它只是一个简单的HelloWorld的printf遵循JNI的教程。我是很新的JNI和C,所以我真的不知道从哪里开始调试。我已经做了最好的尝试32位和64位的DLL,而且我确信我的C编译器(Cygwin的)是64位的,而我的JVM过。

我想AP preciate任何见解!

编辑:这是包含文件

===爪哇(JNITest.java)===

 包jnitest;

公共类JNITest {

    公共静态无效的主要(字串[] args){
        的System.out.println(JVM:+ System.getProperty(sun.arch.data.model));
        System.load(C:\\用户\\ \\安德鲁文件\\的NetBeansProjects \\ JNITestLib \\ \\ DIST JNITest.dll);

        新JNITest()doHello();
    }

    公共本地无效doHello();
}
 

===生成javah的头(jnitest_JNITest.h)===

  / *勿编辑这个文件 - 这是机器生成* /
#包括< jni.h>
/ *头类jnitest_JNITest * /

#ifndef的_Included_jnitest_JNITest
#定义_Included_jnitest_JNITest
的#ifdef __cplusplus
为externC{
#ENDIF
/ *
 *类别:jnitest_JNITest
 *方法:doHello
 *签名:()V
 * /
JNIEXPORT无效JNICALL Java_jnitest_JNITest_doHello
  (JNIEnv的*,jobject);

的#ifdef __cplusplus
}
#ENDIF
#ENDIF
 

=== C(JNITest.c)===

 的#include< jni.h>
#包括jnitest_JNITest.h

JNIEXPORT无效JNICALL Java_jnidemojava_Main_nativePrint
        (JNIEnv的* ENV,jobject OBJ)
{
    的printf(\ nHello世界从C \ N);

}
 

编辑:

这个问题似乎是与该DLL,因为我可以加载其他64位DLL就好了。我认为,Cygwin的可能是问题,所以我改变了我的编译器的MinGW-W64。它编译罚款,库加载,但现在我得到一个新的异常:

 在线程异常主要java.lang.UnsatisfiedLinkError中:jnitest.JNITest.doHello()V
    在jnitest.JNITest.doHello(本机方法)
    在jnitest.JNITest.main(JNITest.java:10)
Java结果:1
 

一些较挖发现,当类加载器读取libs.size(错误被抛出)位置:

  //调用在VM类链接code。
    静态长findNative(类加载器加载,字符串名称){
        矢量< NativeLibrary>库=
            装载机!= NULL? loader.nativeLibraries:systemNativeLibraries;
        同步(库){
            INT大小= libs.size();
            的for(int i = 0; I<大小;我++){
                NativeLibrary LIB = libs.elementAt(ⅰ);
                长项= lib.find(名称);
                如果(条目!= 0)
                    返回入境;
            }
        }
        返回0;
    }
 

编辑:解答

终于找到它了。

首先,什么是错与Cygwin64。使用不同的64位C编译器摆脱了不是有效的Win32应用程序错误的。

其次,我JNITest.c文件的方法签名是不正确的。它应该是:

  Java_jnitest_JNITest_doHello
 

相反

  Java_jnitest_Main_doHello
 

改变这种后,它的作品!

(虽然我不能回答我的问题了另外6小时... ...所以达姆德达姆)

解决方案

终于找到它了。

首先,什么是错与Cygwin64。使用不同的64位C编译器摆脱了不是有效的Win32应用程序错误的。

其次,我JNITest.c文件的方法签名是不正确的。它应该是:

  Java_jnitest_JNITest_doHello
 

相反

  Java_jnitest_Main_doHello
 

改变这种后,它的作品!

I'm running Netbeans on 64-bit Windows 8, with JDK 1.7_25 (64-bit), following the instructions for the Beginning JNI with NetBeans (https://netbeans.org/kb/docs/cnd/beginning-jni-linux.html)

The instructions are for linux, but the principle is the same for Windows I believe (generating a .dll file instead of .so, using win32 includes in the JDK, etc)

I have Cygwin64 installed as well as Cygwin32. Using Cygwin64, I'm able to generate a 64-bit DLL from my C/C++ Dynamic Library project. However, when I call System.load("path/to/JNITest.dll"), I get:

Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\Users\Andrew\Documents\NetBeansProjects\JNITestLib\dist\JNITest.dll: %1 is not a valid Win32 application
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary1(ClassLoader.java:1957)
    at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1882)
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1843)
    at java.lang.Runtime.load0(Runtime.java:795)
    at java.lang.System.load(System.java:1061)
    at jnitest.JNITest.main(JNITest.java:8)
Java Result: 1

From what I gather, this is most often the case when loading a 64-bit application on a 32-bit virtual machine, but my netbeans.conf is pointing to a 64-bit JVM.

Additionally, when I use the 32-bit version of Cygwin to compile things and run, I get

Can't load IA 32-bit .dll on a AMD 64-bit platform

I'm pretty sure I'm correctly generating the DLL file, it's just a simple HelloWorld printf to follow the JNI tutorial. I'm very new to JNI and C, so I'm not really sure where to start debugging. The best I've done is tried both 32 and 64-bit DLLs, and I've made sure my C compiler (Cygwin) is 64-bit, and my JVM is too.

I'd appreciate any insight!

Edit: Here are the included files

=== Java (JNITest.java) ===

package jnitest;

public class JNITest {

    public static void main(String[] args) {
        System.out.println("JVM: " + System.getProperty("sun.arch.data.model"));
        System.load("C:\\Users\\Andrew\\Documents\\NetBeansProjects\\JNITestLib\\dist\\JNITest.dll");

        new JNITest().doHello();
    }

    public native void doHello();
}

=== Generated javah header (jnitest_JNITest.h) ===

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class jnitest_JNITest */

#ifndef _Included_jnitest_JNITest
#define _Included_jnitest_JNITest
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     jnitest_JNITest
 * Method:    doHello
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_jnitest_JNITest_doHello
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

=== C (JNITest.c) ===

#include <jni.h>
#include "jnitest_JNITest.h"

JNIEXPORT void JNICALL Java_jnidemojava_Main_nativePrint
        (JNIEnv *env, jobject obj) 
{
    printf("\nHello World from C\n");

}

Edit:

The problem seems to be with the DLL, since I can load other 64-bit DLLs just fine. I thought that Cygwin might be the problem, so I changed my compiler to MinGW-w64. It compiled fine, and the library loads, but now I get a new exception:

Exception in thread "main" java.lang.UnsatisfiedLinkError: jnitest.JNITest.doHello()V
    at jnitest.JNITest.doHello(Native Method)
    at jnitest.JNITest.main(JNITest.java:10)
Java Result: 1

Some more digging found that the error is thrown when ClassLoader reads libs.size() here:

// Invoked in the VM class linking code.
    static long findNative(ClassLoader loader, String name) {
        Vector<NativeLibrary> libs =
            loader != null ? loader.nativeLibraries : systemNativeLibraries;
        synchronized (libs) {
            int size = libs.size();
            for (int i = 0; i < size; i++) {
                NativeLibrary lib = libs.elementAt(i);
                long entry = lib.find(name);
                if (entry != 0)
                    return entry;
            }
        }
        return 0;
    }

Edit: ANSWERS!

Finally figured it out.

Firstly, something was wrong with Cygwin64. Using a different 64-bit C compiler got rid of the not a valid win32 application error.

Secondly, my JNITest.c file's method signature was incorrect. It should have been:

Java_jnitest_JNITest_doHello

Instead of

Java_jnitest_Main_doHello

After changing that, it works!

(though I can't answer my own question for another 6 hours... so dum de dum)

解决方案

Finally figured it out.

Firstly, something was wrong with Cygwin64. Using a different 64-bit C compiler got rid of the not a valid win32 application error.

Secondly, my JNITest.c file's method signature was incorrect. It should have been:

Java_jnitest_JNITest_doHello

Instead of

Java_jnitest_Main_doHello

After changing that, it works!

这篇关于JNI%1不是有效的Win32应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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