Eclipse + JNI:可以找到库,但不能找到方法
Eclipse + JNI: Can find library but not method
我正在尝试为库编写c++绑定,因此为了向您学习,我使用以下简单的java和c++代码。我正在使用SWIG。
java代码:public class App {
static{
System.loadLibrary("demo");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(System.getProperty("java.library.path"));
printHello("Lars");
}
native static void printHello(String name);
}
我的c++代码:
#include <iostream>
#include <string>
#include "demo.hpp"
void printHello(std::string name){
std::cout << "Hello: "<< name << "n";
}
我的头:
void printHello(std::string);
SWIG的.i文件:
%module demo
%include "std_string.i"
%{
#include <iostream>
#include <string>
#include "demo.hpp"
%}
extern void printHello(std::string);
我运行swig -c++ -java demo。我在那里没有问题。编译:
g++ -c -fPIC demo_wrap.cxx -I/usr/lib64/jvm/java-1.7.0-openjdk-1.7.0/include/ -I/usr/lib64/jvm/java-1.7.0-openjdk-1.7.0/include/linux
创建一个共享库并移动到/usr/lib
g++ -shared demo_wrap.o -o libdemo.so
但是当我尝试使用eclipse运行我的java程序时,我得到了这个错误:
Exception in thread "main" java.lang.UnsatisfiedLinkError: App.printHello(Ljava/lang/String;)V
at App.printHello(Native Method)
at App.main(App.java:11)
所以从我可以告诉,eclipse/java知道我的库存在,但它不能找到我的函数/方法在库中。是我做错了什么吗?还是我遗漏了什么?
Thanks it advance
EDIT:如果demoJNI.java的内容可能有帮助
public class demoJNI {
public final static native void printHello(String jarg1);
}
Demo_wrap.cxx
/* ----------------------------------------------------------------------------
* This file was automatically generated by SWIG (http://www.swig.org).
* Version 2.0.10
*
* This file is not intended to be easily readable and contains a number of
* coding conventions designed to improve portability and efficiency. Do not make
* changes to this file unless you know what you are doing--modify the SWIG
* interface file instead.
* ----------------------------------------------------------------------------- */
#define SWIGJAVA
#ifdef __cplusplus
/* SwigValueWrapper is described in swig.swg */
template<typename T> class SwigValueWrapper {
struct SwigMovePointer {
T *ptr;
SwigMovePointer(T *p) : ptr(p) { }
~SwigMovePointer() { delete ptr; }
SwigMovePointer& operator=(SwigMovePointer& rhs) { T* oldptr = ptr; ptr = 0; delete oldptr; ptr = rhs.ptr; rhs.ptr = 0; return *this; }
} pointer;
SwigValueWrapper& operator=(const SwigValueWrapper<T>& rhs);
SwigValueWrapper(const SwigValueWrapper<T>& rhs);
public:
SwigValueWrapper() : pointer(0) { }
SwigValueWrapper& operator=(const T& t) { SwigMovePointer tmp(new T(t)); pointer = tmp; return *this; }
operator T&() const { return *pointer.ptr; }
T *operator&() { return pointer.ptr; }
};
template <typename T> T SwigValueInit() {
return T();
}
#endif
/* -----------------------------------------------------------------------------
* This section contains generic SWIG labels for method/variable
* declarations/attributes, and other compiler dependent labels.
* ----------------------------------------------------------------------------- */
/* template workaround for compilers that cannot correctly implement the C++ standard */
#ifndef SWIGTEMPLATEDISAMBIGUATOR
# if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x560)
# define SWIGTEMPLATEDISAMBIGUATOR template
# elif defined(__HP_aCC)
/* Needed even with `aCC -AA' when `aCC -V' reports HP ANSI C++ B3910B A.03.55 */
/* If we find a maximum version that requires this, the test would be __HP_aCC <= 35500 for A.03.55 */
# define SWIGTEMPLATEDISAMBIGUATOR template
# else
# define SWIGTEMPLATEDISAMBIGUATOR
# endif
#endif
/* inline attribute */
#ifndef SWIGINLINE
# if defined(__cplusplus) || (defined(__GNUC__) && !defined(__STRICT_ANSI__))
# define SWIGINLINE inline
# else
# define SWIGINLINE
# endif
#endif
/* attribute recognised by some compilers to avoid 'unused' warnings */
#ifndef SWIGUNUSED
# if defined(__GNUC__)
# if !(defined(__cplusplus)) || (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
# define SWIGUNUSED __attribute__ ((__unused__))
# else
# define SWIGUNUSED
# endif
# elif defined(__ICC)
# define SWIGUNUSED __attribute__ ((__unused__))
# else
# define SWIGUNUSED
# endif
#endif
#ifndef SWIG_MSC_UNSUPPRESS_4505
# if defined(_MSC_VER)
# pragma warning(disable : 4505) /* unreferenced local function has been removed */
# endif
#endif
#ifndef SWIGUNUSEDPARM
# ifdef __cplusplus
# define SWIGUNUSEDPARM(p)
# else
# define SWIGUNUSEDPARM(p) p SWIGUNUSED
# endif
#endif
/* internal SWIG method */
#ifndef SWIGINTERN
# define SWIGINTERN static SWIGUNUSED
#endif
/* internal inline SWIG method */
#ifndef SWIGINTERNINLINE
# define SWIGINTERNINLINE SWIGINTERN SWIGINLINE
#endif
/* exporting methods */
#if (__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
# ifndef GCC_HASCLASSVISIBILITY
# define GCC_HASCLASSVISIBILITY
# endif
#endif
#ifndef SWIGEXPORT
# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
# if defined(STATIC_LINKED)
# define SWIGEXPORT
# else
# define SWIGEXPORT __declspec(dllexport)
# endif
# else
# if defined(__GNUC__) && defined(GCC_HASCLASSVISIBILITY)
# define SWIGEXPORT __attribute__ ((visibility("default")))
# else
# define SWIGEXPORT
# endif
# endif
#endif
/* calling conventions for Windows */
#ifndef SWIGSTDCALL
# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
# define SWIGSTDCALL __stdcall
# else
# define SWIGSTDCALL
# endif
#endif
/* Deal with Microsoft's attempt at deprecating C standard runtime functions */
#if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE)
# define _CRT_SECURE_NO_DEPRECATE
#endif
/* Deal with Microsoft's attempt at deprecating methods in the standard C++ library */
#if !defined(SWIG_NO_SCL_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_SCL_SECURE_NO_DEPRECATE)
# define _SCL_SECURE_NO_DEPRECATE
#endif
/* Fix for jlong on some versions of gcc on Windows */
#if defined(__GNUC__) && !defined(__INTEL_COMPILER)
typedef long long __int64;
#endif
/* Fix for jlong on 64-bit x86 Solaris */
#if defined(__x86_64)
# ifdef _LP64
# undef _LP64
# endif
#endif
#include <jni.h>
#include <stdlib.h>
#include <string.h>
/* Support for throwing Java exceptions */
typedef enum {
SWIG_JavaOutOfMemoryError = 1,
SWIG_JavaIOException,
SWIG_JavaRuntimeException,
SWIG_JavaIndexOutOfBoundsException,
SWIG_JavaArithmeticException,
SWIG_JavaIllegalArgumentException,
SWIG_JavaNullPointerException,
SWIG_JavaDirectorPureVirtual,
SWIG_JavaUnknownError
} SWIG_JavaExceptionCodes;
typedef struct {
SWIG_JavaExceptionCodes code;
const char *java_exception;
} SWIG_JavaExceptions_t;
static void SWIGUNUSED SWIG_JavaThrowException(JNIEnv *jenv, SWIG_JavaExceptionCodes code, const char *msg) {
jclass excep;
static const SWIG_JavaExceptions_t java_exceptions[] = {
{ SWIG_JavaOutOfMemoryError, "java/lang/OutOfMemoryError" },
{ SWIG_JavaIOException, "java/io/IOException" },
{ SWIG_JavaRuntimeException, "java/lang/RuntimeException" },
{ SWIG_JavaIndexOutOfBoundsException, "java/lang/IndexOutOfBoundsException" },
{ SWIG_JavaArithmeticException, "java/lang/ArithmeticException" },
{ SWIG_JavaIllegalArgumentException, "java/lang/IllegalArgumentException" },
{ SWIG_JavaNullPointerException, "java/lang/NullPointerException" },
{ SWIG_JavaDirectorPureVirtual, "java/lang/RuntimeException" },
{ SWIG_JavaUnknownError, "java/lang/UnknownError" },
{ (SWIG_JavaExceptionCodes)0, "java/lang/UnknownError" }
};
const SWIG_JavaExceptions_t *except_ptr = java_exceptions;
while (except_ptr->code != code && except_ptr->code)
except_ptr++;
jenv->ExceptionClear();
excep = jenv->FindClass(except_ptr->java_exception);
if (excep)
jenv->ThrowNew(excep, msg);
}
/* Contract support */
#define SWIG_contract_assert(nullreturn, expr, msg) if (!(expr)) {SWIG_JavaThrowException(jenv, SWIG_JavaIllegalArgumentException, msg); return nullreturn; } else
#include <string>
#include <iostream>
#include <string>
#include "demo.hpp"
#ifdef __cplusplus
extern "C" {
#endif
SWIGEXPORT void JNICALL Java_demoJNI_printHello(JNIEnv *jenv, jclass jcls, jstring jarg1) {
std::string arg1 ;
(void)jenv;
(void)jcls;
if(!jarg1) {
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null string");
return ;
}
const char *arg1_pstr = (const char *)jenv->GetStringUTFChars(jarg1, 0);
if (!arg1_pstr) return ;
(&arg1)->assign(arg1_pstr);
jenv->ReleaseStringUTFChars(jarg1, arg1_pstr);
printHello(arg1);
}
#ifdef __cplusplus
}
#endif
原来我对SWIG的工作有误解。也许,你也是。
SWIG的目的是自动构建C/c++库的包装器。它不是javah的替代品。通过SWIG,您可以从C或c++代码开始,并接收自动生成的c++和Java代码。这样,您就可以在项目中使用这些包装Java类(在您的示例中是demoJNI)。您不需要在Java代码中声明原生方法。您可以通过包装器Java类和包装器C/c++代码(Demo_wrap.cxx
和demoJNI.java
)与c++库通信。
SWIG文档明确指出,它不是将Java代码连接到几个本机函数的正确工具。它的设计目标是自动包装带有复杂api、Java、Python和许多其他高级语言的大型c++项目。
在你的c++代码中,你应该这样做:
extern "C" {
JNIEXPORT void JNICALL
Java_App_printHello(JNIEnv* env, jobject obj, jstring name) {
const char* c_name = env -> GetStringUTFChars(name , NULL);
std::cout << "Hello: "<< c_str << "n";
env->ReleaseStringUTFChars(name, c_name);
}
}
相关文章:
- 是否可以将函数导入命名空间,但不能导出它?
- std::unique_ptr 在 GCC 中工作,但不能在 Visual Studio 中编译
- 声明C++具有动态大小的数组类型在 Linux 中工作正常,但不能在 Windows 中工作
- Casablanca/cpprestsdk listener.support接受函数,但不支持方法
- 为什么我可以隐式地将字符*转换为常量字符*,但不能将无符号字符*
- std::chrono::d uration 可以按秒初始化,但不能按毫秒初始化?
- Python/C++:可以导入犰狳(arma::)但不能导入子程序arma::arma_rng::randn
- 为什么我可以从C++文件中读取 int 值,但不能浮点?
- C++程序在将 int 与 cin 一起使用时有效,但不能使用字符串
- SqLite c++,可以创建数据库,但不能将 anthing 插入到表中
- std::remove() 按预期处理文字,但不能与取消引用的迭代器一起工作
- 16 位系统中的程序如何访问大于 65535 的整数,但不能访问地址
- C++:返回本地对象,但不能正常工作
- 可以访问一个类中的播放器结构,但不能访问另一个类中的播放器结构
- 返回实例变量的c++方法可以访问变量中的数据,但不能更改它,但在编译时不会生成错误
- 用C++替换std::字符串中的一个子字符串,但不能全部替换
- C++ 特征:为什么可以为方法添加赋值,但不能为方法添加 Diagonal()
- 设置为实例的静态值在从静态方法设置时有效,但不能从类内的静态方法设置
- 当使用CreateProcessLogonW或类似的方法启动时,进程启动,但不能运行
- Eclipse + JNI:可以找到库,但不能找到方法