我刚刚尝试了今天在Java中学习本机方法。我成功地成功地加载了Native.dll
文件,但在方法呼叫中遇到问题,也无法弄清楚发生了什么事。
我的摘要代码是:
nativetest.java
package javaapplication2;
import java.io.File;
import java.util.Scanner;
/**
*
* @author Chirag
*/
public class NativeTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Loading Library nativeLib");
try {
Native n = new Native();
System.out.println("Loading success");
System.out.print("Enter a number to get cube: ");
int x = sc.nextInt();
System.out.println(x + " * " + x + " * " + x + " = " + n.cubecal(x));
} catch (Exception e) {
System.out.println("Error loading native library");
e.printStackTrace();
}
}
}
class Native {
static {
System.setProperty("java.library.path", new File("").getAbsolutePath());
// try {
// Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
// fieldSysPath.setAccessible(true);
// fieldSysPath.set(null, null);
// } catch (Exception e) {
// }
String arch = System.getProperty("sun.cpu.isalist");
if (arch.trim().equalsIgnoreCase("amd64")) {
System.loadLibrary("Native");
} else {
System.loadLibrary("Native");
}
}
public native int cubecal(int x);
}
javac NativeTest.java
并通过javah Native
header
文件本机。H
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Native */
#ifndef _Included_Native
#define _Included_Native
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Native
* Method: cubecal
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_Native_cubecal
(JNIEnv *, jobject, jint);
#ifdef __cplusplus
}
#endif
#endif
和另一个snip
本机.cpp
#include "stdafx.h"
#include "Native.h"
JNIEXPORT jint JNICALL Java_Native_cubecal
(JNIEnv *env, jobject obj, jint val){
return val * val * val;
}
在所有这些剪辑之后,我在执行此代码
之后会遇到此错误Loading Library Native
Loading success
Enter a number to get cube: 4
Exception in thread "main" java.lang.UnsatisfiedLinkError: javaapplication2.Native.cubecal(I)I
at javaapplication2.Native.cubecal(Native Method)
at javaapplication2.NativeTest.main(NativeTest.java:28)
C:UsersChiragAppDataLocalNetBeansCache8.2executor-snippetsrun.xml:53: Java returned: 1
我知道只能通过Java代码轻松实现这一点,但是我正在尝试本机方法,以便我也可以在其他Java项目中使用C 的好处。
注意:我在Visual Studio 2012中将所有亲戚的路径添加到Project属性中,遵循
C:Program FilesJavajdk1.8.0_144include
和C:Program FilesJavajdk1.8.0_144includewin32
我还阅读了这些文章,这些文章与JAVA的本机方法和JAVA编程一起使用,但始终遇到此错误。
请帮助,也向我解释一下这个例外是什么。
谢谢:)
是!最后,经过太多的测试,我得到了答案。完整代码在a 软件包中,上面片段中的问题是我不是在考虑用于创建标题文件的包装。(hehehe ...我的错误。对不起。为此)
更新的代码示例是:
nativetest.java
package javaapplication2;
import java.io.File;
import java.util.Scanner;
/**
*
* @author Chirag
*/
public class NativeTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Loading library NativeCube");
try {
NativeCube n = new NativeCube();
System.out.println("Loading Library NativeCube succeeded");
System.out.print("Enter a number to get cube: ");
int x = sc.nextInt();
System.out.println(x + " * " + x + " * " + x + " = " + n.cubecal(x));
} catch (Throwable e) {
System.err.println("Error loading library nativeLib: " + e);
e.printStackTrace();
}
}
}
class NativeCube {
public NativeCube() {
System.setProperty("java.library.path", new File("").getAbsolutePath());
System.out.println("Pathes: " + System.getProperty("java.library.path"));
String arch = System.getProperty("sun.cpu.isalist");
if (arch.trim().equalsIgnoreCase("amd64")) {
System.loadLibrary("NativeCube");
} else {
System.loadLibrary("NativeCube_x86");
}
}
public native int cubecal(int x);
}
javah javaapplication2.NativeCube
javaapplication2_nativecube.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class javaapplication2_NativeCube */
#ifndef _Included_javaapplication2_NativeCube
#define _Included_javaapplication2_NativeCube
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: javaapplication2_NativeCube
* Method: cubecal
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_javaapplication2_NativeCube_cubecal
(JNIEnv *, jobject, jint);
#ifdef __cplusplus
}
#endif
#endif
main.cpp
#include "javaapplication2_NativeCube.h"
JNIEXPORT jint JNICALL Java_javaapplication2_NativeCube_cubecal
(JNIEnv *env, jobject obj, jint val){
return val * val * val;
}
,程序的输出为
Loading library NativeCube
Pathes: C:UsersChiragDocumentsNetBeansProjectsJavaApplication2
Loading Library NativeCube succeeded
Enter a number to get cube: 12
12 * 12 * 12 = 1728
学会了一个很好的章节,内容涉及不跳过包名称以创建标头文件。
感谢@Andrew S和@Sergey的快速回复。:)