Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling Java method from C? #235

Closed
paolobolzoni opened this issue May 24, 2013 · 1 comment
Closed

Calling Java method from C? #235

paolobolzoni opened this issue May 24, 2013 · 1 comment

Comments

@paolobolzoni
Copy link

Is it possible to call a Java method from C code?
In the project I am working on, some calculation are made in C code and I would like to call the Java logger to give info about how it is going.

I sought around and I read this:

""You can.[Call a Java method from C] With a callback class that corresponds to a function pointer on the C side. void register_callback(void (*)(const int)); would be mapped to public static native void register_callback(MyCallback arg1); where MyCallback is an interface extending com.sun.jna.Callback with a single method void apply(int value);""

Unfortunately I do not have much experience with Java (I am the one writing in C) and I did not understand much. It seems there is way to treat a Java method as a function pointer, it would be great.
Can anyone explain how-to, or point me to the manual?
Thanks

@twall
Copy link
Contributor

twall commented May 25, 2013

Please take this discussion to the mailing list, not the issues list.

Presumably you're running some Java code already in order to have a "logger". Have that call make a call to your C code to provide it a function, say "void register_callback(void (logger)(const char))". Map that function using JNA, e.g.

interface LoggerCallback extends Callback {
     public void invoke(String msg);
}
void register_callback(LoggerCallback cb);

LoggerCallback c = new LoggerCallback() {
    public void invoke(String msg) {
        // TODO send message on to java logger
    }
};
lib.register_callback(c);

Closes #235.

On May 24, 2013, at 11:55 AM, Paolo Bolzoni wrote:

Is it possible to call a Java method from C code?
In the project I am working on, some calculation are made in C code and I would like to call the Java logger to give info about how it is going.

I sought around and I read this:

""You can.[Call a Java method from C] With a callback class that corresponds to a function pointer on the C side. void register_callback(void (*)(const int)); would be mapped to public static native void register_callback(MyCallback arg1); where MyCallback is an interface extending com.sun.jna.Callback with a single method void apply(int value);""

Unfortunately I do not have much experience with Java (I am the one writing in C) and I did not understand much. It seems there is way to treat a Java method as a function pointer, it would be great.
Can anyone explain how-to, or point me to the manual?
Thanks


Reply to this email directly or view it on GitHub.

@twall twall closed this as completed May 30, 2013
mstyura pushed a commit to mstyura/jna that referenced this issue Sep 9, 2024
Motifivation:

netty-jni-util 0.0.2.Final is incompatible with static linking. netty-jni-util 0.0.3.Final adds static linking compatibility.

Modifications:

Bump netty-jni-util to version 0.0.3.Final and update to its new API
which requires the caller to manage packagePrefix.

Result:

Using latest version of netty-jni-util and support static linking
compatibility.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants