eclipse中的javacpp不起作用--ClassNotFoundException(使用JNI)

时间:2012-01-04 03:19:01

标签: java c++ eclipse java-native-interface

我正在尝试使用javacpp并遇到eclipse(+ mac OS)的一些困难。

当我在命令行中运行它时 - 它可以正常运行

#include <string>

namespace LegacyLibrary {
    class LegacyClass {
        public:
            const std::string& getProperty() { return property; }
            void setProperty(const std::string& property) { this->property = property; }
        private:
            std::string property;
    };
}

import com.googlecode.javacpp.*;
import com.googlecode.javacpp.annotation.*;

@Platform(include="LegacyLibrary.h")
@Namespace("LegacyLibrary")
public class LegacyLibrary {
public static class LegacyClass extends Pointer {
    static { Loader.load(); }
    public LegacyClass() { allocate(); }
    private native void allocate();

    public native @ByRef String getProperty();
    public native void setProperty(String property);
}

public static void main(String[] args) {
    LegacyClass l = new LegacyClass();
    l.setProperty("Hello World!");
    System.out.println(l.getProperty());
}

}

this

中所述

我得到了最终结果:“你好世界”。

现在我把它带到我的eclipse IDE中,出于某种原因,我收到了这个错误:

Exception in thread "main" java.lang.NoClassDefFoundError: java.lang.ClassNotFoundException: com.test.LegacyLibrary
    at com.googlecode.javacpp.Loader.load(Loader.java:293)
    at com.googlecode.javacpp.Loader.load(Loader.java:271)
    at com.test.LegacyLibrary$LegacyClass.<clinit>(LegacyLibrary.java:12)
    at com.test.LegacyLibrary.main(LegacyLibrary.java:23)
Caused by: java.lang.ClassNotFoundException: com.test.LegacyLibrary
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at sun.misc.Launcher$ExtClassLoader.findClass(Launcher.java:229)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at com.googlecode.javacpp.Loader.load(Loader.java:291)
    ... 3 more

我将2个文件放入com.test包中,并将javacpp.jar放入类路径中。 这里有什么我想念的吗?!?!错误消息表明类路径中的某些内容不正确(或丢失),但它是什么? (我也尝试将属性指向另一个文件夹中的javacpp.jar,但是我得到了相同的错误消息)。也许虚拟机参数?!?!

谢谢!

3 个答案:

答案 0 :(得分:0)

您是否将javacpp.jar文件添加到项目库中?转到项目属性,然后是构建路径信息,应该有一个库的选项卡。添加外部Jar文件,然后选择javacpp.jar文件。

enter image description here

答案 1 :(得分:0)

转到项目属性 - &gt; Java构建路径,在选项卡中单击箭头以展开源文件夹,然后选择Native Library Location并单击Edit,找到LegacyLibrary.h文件的路径,应该工作。

Add native library to Eclipse project

答案 2 :(得分:0)

我的猜测是你的maven文件配置错误,因此gcc编译器无法找到你的源文件LegacyLibrary.h

使用以下链接作为参考配置您的pom.xml。

https://github.com/oltzen/JavaCppExample/blob/master/pom.xml

注意第53和65行。填写正确的包装名称。这有助于编译器找出LegacyLibrary.h的位置。

另外,观看此视频片段https://www.youtube.com/watch?v=LZrrqZLhtmw,它将引导您完成整个过程,了解如何使用maven和eclipse运行Javacpp。