0

My Java code

import org.bytedeco.javacpp.*;
import org.bytedeco.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();

        // to call the getter and setter functions 
        public native @StdString String get_property(); public native void set_property(String property);

        // to access the member variable directly
        public native @StdString String property();     public native void property(String property);
    }

    public static void main(String[] args) {
        // Pointer objects allocated in Java get deallocated once they become unreachable,
        // but C++ destructors can still be called in a timely fashion with Pointer.deallocate()
        LegacyClass l = new LegacyClass();
        l.set_property("Hello World!");
        System.out.println(l.property());
    }
}

LegacyLibrary.h code

#include <string>

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

Folder Structure

enter image description here

enter image description here

I am using Javacpp to access cpp functions in Java. I am trying to execute the example given in the documentation. I have build a jniLegacyLibrary.dll in Visual Studio 2013 and added it into the directory and executed the Java file in eclipse.

I am getting the following error.

Exception in thread "main" java.lang.UnsatisfiedLinkError: LegacyLibrary$LegacyClass.allocate()V at LegacyLibrary$LegacyClass.allocate(Native Method) at LegacyLibrary$LegacyClass.(LegacyLibrary.java:9) at LegacyLibrary.main(LegacyLibrary.java:22)

I am very new to cpp. What is the problem with my code?

Vishnu
  • 11,614
  • 6
  • 51
  • 90
  • that you keep refering to CPP as "legacy".. – David Haim Aug 05 '15 at 09:17
  • @Vishnu If you want to place the DLL to a custom location, try to use `System.load()` with the path first. @DavidHaim Good point. I'll have to change the name of that sample to say `NativeLibrary`? – Samuel Audet Aug 05 '15 at 09:30
  • @SamuelAudet `Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method load(Class) in the type Loader is not applicable for the arguments (String)` – Vishnu Aug 05 '15 at 09:35
  • @SamuelAudet http://stackoverflow.com/questions/31824161/java-lang-unsatisfiedlinkerror-no-jnilegacylibrary-in-java-library-path – Vishnu Aug 05 '15 at 09:39
  • `System.load()` needs the full absolute path to the DLL. – Samuel Audet Aug 17 '15 at 09:41

0 Answers0