public inbox for java@gcc.gnu.org
 help / color / mirror / Atom feed
From: Bryce McKinlay <bmckinlay@gmail.com>
To: Jan Knezik <jan.kn@email.cz>
Cc: GCC Java <java@gcc.gnu.org>
Subject: Re: Using libraries - CNI and JNI
Date: Sat, 12 Apr 2014 17:46:00 -0000	[thread overview]
Message-ID: <CALUNu-qVTA8Paq9sM=8obbBsBdjvrb9cjX1JLWkKasVrT+pQgw@mail.gmail.com> (raw)
In-Reply-To: <5349091B.9080006@email.cz>

On Sat, Apr 12, 2014 at 10:36 AM, Jan Knezik <jan.kn@email.cz> wrote:

> 1. Is it possible to compile and get work together main programme and .so library (both written in Java) with GCJ using CNI or JNI?

It is certainly possible to have a Java program split between a main
binary and .so libraries.

However CNI and JNI are for native methods (implemented in C++ or C
respectively), not when both parts are written in Java.

> 2. I have the following code:
>
> public class Test {
>     static {
>         System.loadLibrary("Test");
>     }
>
>     public native void hello();
>
>     public static void main(String[] args) {
>         new Test().hello();
>     }
> }
>
> I tried to compile it with "gcj Test.java --main=Test -o Test.bin". I got:
>
> /tmp/ccWZkOjd.o:(.data+0x50): undefined reference to `hidden alias for void Test::hello()'
> /tmp/ccWZkOjd.o:(.data+0xc8): undefined reference to `hidden alias for void Test::hello()'
>
> I did not use "-fjni" so CNI should have been set. Where is the problem, please? Thank you.

For CNI, you need to tell the linker to link against the library
(static or shared) that contains the C++ implementation of hello().

For example, if hello() is in "libTest.so":

gcj Test.java --main=Test -o Test.bin -lTest

You can do the same thing is libTest.so was a library containing Java
code. However, you can't split a single class across different
libraries or implement a "native" method in Java.

Bryce

      reply	other threads:[~2014-04-12 17:46 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-12  9:37 Jan Knezik
2014-04-12 17:46 ` Bryce McKinlay [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CALUNu-qVTA8Paq9sM=8obbBsBdjvrb9cjX1JLWkKasVrT+pQgw@mail.gmail.com' \
    --to=bmckinlay@gmail.com \
    --cc=jan.kn@email.cz \
    --cc=java@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).