GithubHelp home page GithubHelp logo

Comments (10)

saudet avatar saudet commented on June 2, 2024

A call to JavaCPP_detach(attached) is missing at the end... Could you try again with the latest version of JavaCPP to see if the same thing happens? And if so, could you also provide the FunctionPointer declaration? Thanks!

from javacpp.

 avatar commented on June 2, 2024

I've tried with the latest version (0.10) and I'm getting the same result.

This is the FunctionPointer declaration:

public @Name("LocalizedCaseInsensitiveCompare") int call(@Cast("const char *") String str1,@Cast("const char *") String str2, int charsToCompare)

And this is the full method (using the latest version 0.10)

int JavaCPP_com_mypackage_MyClass_00024LocalizedCaseInsensitiveCompareCallback::operator()(const char * arg0, const char * arg1, int arg2) {
    jint rarg = 0;
    jthrowable exc = NULL;
    JNIEnv* env;
    bool attached = JavaCPP_getEnv(&env);
    if (env == NULL) {
        goto end;
    }
{
    jvalue args[3];
    jstring obj0 = (const char*)arg0 == NULL ? NULL : env->NewStringUTF((const char*)arg0);
    args[0].l = obj0;
    jstring obj1 = (const char*)arg1 == NULL ? NULL : env->NewStringUTF((const char*)arg1);
    args[1].l = obj1;
    args[2].i = (jint)arg2;
    if (obj == NULL) {
        obj = env->NewGlobalRef(JavaCPP_createPointer(env, 70));
        if (obj == NULL) {
            JavaCPP_log("Error creating global reference of com_mypackage_MyClass.LocalizedCaseInsensitiveCompareCallback instance for callback.");
        } else {
            env->SetLongField(obj, JavaCPP_addressFID, ptr_to_jlong(this));
        }
        ptr = &LocalizedCaseInsensitiveCompare;
    }
    if (mid == NULL) {
        mid = JavaCPP_getMethodID(env, 70, "call", "(Ljava/lang/String;Ljava/lang/String;I)I");
    }
    if (env->IsSameObject(obj, NULL)) {
        JavaCPP_log("Function pointer object is NULL in callback for com_mypackage_MyClass.LocalizedCaseInsensitiveCompareCallback.");
    } else if (mid == NULL) {
        JavaCPP_log("Error getting method ID of function caller \"public int com_mypackage_MyClass$LocalizedCaseInsensitiveCompareCallback.call(java.lang.String,java.lang.String,int)\" for callback.");
    } else {
        rarg = env->CallIntMethodA(obj, mid, args);
        if ((exc = env->ExceptionOccurred()) != NULL) {
            env->ExceptionClear();
        }
    }
    env->DeleteLocalRef(obj0);
    env->DeleteLocalRef(obj1);
}
end:
    if (exc != NULL) {
        jstring str = (jstring)env->CallObjectMethod(exc, JavaCPP_toStringMID);
        env->DeleteLocalRef(exc);
        const char *msg = env->GetStringUTFChars(str, NULL);
        JavaCPP_exception e(msg);
        env->ReleaseStringUTFChars(str, msg);
        env->DeleteLocalRef(str);
        JavaCPP_detach(attached);
        throw e;
    } else {
        JavaCPP_detach(attached);
    }
    return rarg;
}

from javacpp.

saudet avatar saudet commented on June 2, 2024

On my machine, with the following files:

Foo.java

import org.bytedeco.javacpp.*;
import org.bytedeco.javacpp.annotation.*;

public class Foo {
    public static class Callback extends FunctionPointer {
        public @Name("foo") int call(String a, String b, int n) throws Exception { 
            return n;
        }
    }
}

foo.cpp

#include <iostream>
#include "jniFoo.h"

int main() {
    JavaCPP_init(0, NULL);
    for (int i = 0; i < 1000000; i++) {
        foo("foo", "bar", 42);
    }
    JavaCPP_uninit();
}

And the following commands:

$ javac -cp javacpp.jar Foo.java
$ java -jar javacpp.jar Foo -header
$ g++ -I/usr/lib/jvm/java/include/ -I/usr/lib/jvm/java/include/linux/ linux-x86_64/libjniFoo.so foo.cpp -o foo
$ ./foo

Nothing bad happens. Could you please tell me what results you get with that on your machine?

from javacpp.

spotell avatar spotell commented on June 2, 2024

I create Foo.java and foo.cpp and add build 10 javacpp.jar to the directory.

javac -cp javacpp.jar Foo.java works fine and creates Foo$Callback.class and Foo.class

java -jar javacpp.jar Foo -header causes this error:

Generating C:\Acronis\temp\jniFoo.cpp
Compiling C:\Acronis\temp\windows-x86\jniFoo.dll
cl "/IC:\Program Files (x86)\Java\jdk1.7.0_65\include" "/IC:\Program Files (x86)\Java\jdk1.7.0_65\include\win32" C:\Acronis\temp\jniFoo.cpp /W3 /Oi /O2 /EHsc /Gy /GL
/MD /LD /link /OUT:C:\Acronis\temp\windows-x86\jniFoo.dll "/LIBPATH:C:\Program Files (x86)\Java\jdk1.7.0_65\lib" "/LIBPATH:C:\Program Files (x86)\Java\jdk1.7.0_65\jre
\bin\server" jvm.lib
Exception in thread "main" java.io.IOException: Cannot run program "cl": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047)
at org.bytedeco.javacpp.tools.Builder.compile(Builder.java:302)
at org.bytedeco.javacpp.tools.Builder.generateAndCompile(Builder.java:356)
at org.bytedeco.javacpp.tools.Builder.build(Builder.java:627)
at org.bytedeco.javacpp.tools.Builder.main(Builder.java:760)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.(ProcessImpl.java:385)
at java.lang.ProcessImpl.start(ProcessImpl.java:136)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1028)
... 4 more

from javacpp.

saudet avatar saudet commented on June 2, 2024

How are you building for the class that contains your FunctionPointer declaration?

from javacpp.

spotell avatar spotell commented on June 2, 2024

Is there another instruction for building that class?

from javacpp.

saudet avatar saudet commented on June 2, 2024

from javacpp.

spotell avatar spotell commented on June 2, 2024

I am getting the same error as lalosoft so was following your instructions for building Foo.java and foo.cpp. You do not get an error and I do when executing your commands in order.

from javacpp.

saudet avatar saudet commented on June 2, 2024

You're on Windows, so you're going to need to install a C++ compiler separately to try this out:
https://github.com/bytedeco/javacpp#required-software

from javacpp.

saudet avatar saudet commented on June 2, 2024

Given the lack of feedback, I'm guessing this issue has been resolved, but please let me know if this is not the case! Thank you

from javacpp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.