public inbox for java@gcc.gnu.org
 help / color / mirror / Atom feed
* Interface gcj-compiled library with Java programs in a runtime (other than gij)?
@ 2012-02-13  3:01 Yi Lin
  2012-02-13  4:01 ` Brian Jones
  0 siblings, 1 reply; 8+ messages in thread
From: Yi Lin @ 2012-02-13  3:01 UTC (permalink / raw)
  To: java

Hi,

I am wondering if we can use methods from a gcj-compiled library in 
uncompiled Java program(bytecode) executed on a VM.

According to gcj FAQ 1.7, there is no problem to do this with 'gij' 
interpreter/runtime. You can compile some Java code into library (native 
code), and run bytecode with 'gij' to invoke library methods. But this 
seems only capable for 'gij', and other runtimes like HotSpot cannot 
load and link the library properly.

I want to know if there is a general solution to allow you interface 
uncompiled Java program with gcj-compiled library. Any information or 
reference about this will be appreciated.

Many Thanks,
Yi

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Interface gcj-compiled library with Java programs in a runtime (other than gij)?
  2012-02-13  3:01 Interface gcj-compiled library with Java programs in a runtime (other than gij)? Yi Lin
@ 2012-02-13  4:01 ` Brian Jones
  2012-02-13  4:30   ` Yi Lin
  0 siblings, 1 reply; 8+ messages in thread
From: Brian Jones @ 2012-02-13  4:01 UTC (permalink / raw)
  To: Yi Lin; +Cc: java

I suggest you try jni to get to whatever native code you are interested in.

Brian

On Feb 12, 2012, at 10:01 PM, Yi Lin <qinsoon@gmail.com> wrote:

> Hi,
>
> I am wondering if we can use methods from a gcj-compiled library in uncompiled Java program(bytecode) executed on a VM.
>
> According to gcj FAQ 1.7, there is no problem to do this with 'gij' interpreter/runtime. You can compile some Java code into library (native code), and run bytecode with 'gij' to invoke library methods. But this seems only capable for 'gij', and other runtimes like HotSpot cannot load and link the library properly.
>
> I want to know if there is a general solution to allow you interface uncompiled Java program with gcj-compiled library. Any information or reference about this will be appreciated.
>
> Many Thanks,
> Yi

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Interface gcj-compiled library with Java programs in a runtime (other than gij)?
  2012-02-13  4:01 ` Brian Jones
@ 2012-02-13  4:30   ` Yi Lin
  2012-02-13 18:33     ` David Daney
  0 siblings, 1 reply; 8+ messages in thread
From: Yi Lin @ 2012-02-13  4:30 UTC (permalink / raw)
  To: Brian Jones; +Cc: java

Thank you. I think I may have missed a keyword here: CNI. I avoid using 
JNI for performance degradation.

The detailed scenario is like:
There are two parts of code.

-SomeJavaCode.java: containing a method call to NativeJavaLib.a().
This part of code remains in bytecode

-NativeJavaLib.java: declaring 'native void a()'
-NativeJavaLib.cpp: implementing a() via CNI
This part is compiled to libJavaLib.so

Now SomeJavaCode can be executed correctly with 'gij' interpreter; I 
think there is no JNI involved. But it cannot run on HotSpot or other 
VM. If I have to use JNI to do this, then the approach is much less 
attractive.

Thanks,
Yi

On 13/02/12 15:00 , Brian Jones wrote:
> I suggest you try jni to get to whatever native code you are interested in.
>
> Brian
>
> On Feb 12, 2012, at 10:01 PM, Yi Lin<qinsoon@gmail.com>  wrote:
>
>> Hi,
>>
>> I am wondering if we can use methods from a gcj-compiled library in uncompiled Java program(bytecode) executed on a VM.
>>
>> According to gcj FAQ 1.7, there is no problem to do this with 'gij' interpreter/runtime. You can compile some Java code into library (native code), and run bytecode with 'gij' to invoke library methods. But this seems only capable for 'gij', and other runtimes like HotSpot cannot load and link the library properly.
>>
>> I want to know if there is a general solution to allow you interface uncompiled Java program with gcj-compiled library. Any information or reference about this will be appreciated.
>>
>> Many Thanks,
>> Yi

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Interface gcj-compiled library with Java programs in a runtime (other than gij)?
  2012-02-13  4:30   ` Yi Lin
@ 2012-02-13 18:33     ` David Daney
  2012-02-14  9:48       ` Andrew Haley
  0 siblings, 1 reply; 8+ messages in thread
From: David Daney @ 2012-02-13 18:33 UTC (permalink / raw)
  To: Yi Lin; +Cc: Brian Jones, java

On 02/12/2012 08:31 PM, Yi Lin wrote:
> Thank you. I think I may have missed a keyword here: CNI. I avoid using
> JNI for performance degradation.
>
> The detailed scenario is like:
> There are two parts of code.
>
> -SomeJavaCode.java: containing a method call to NativeJavaLib.a().
> This part of code remains in bytecode

So really what you are talking about is SomeJavaCode.class running the 
the libgcj interpreter.

I am just going from memory here, but I think that the interpreter can 
only call native code via JNI.

For compiled code, CNI may be faster.  So...

>
> -NativeJavaLib.java: declaring 'native void a()'

... rename this bit like this:

private native void _a();

/* 'Trampoline' to native code. */
void a()
{
	_a();
}

> -NativeJavaLib.cpp: implementing a() via CNI

In here rename a() to _a().

> This part is compiled to libJavaLib.so
>

Now you have not changed SomeJavaCode.java in any way, but it is instead 
calling a normal java method in the NativeJavaLib.java, which takes care 
of doing the proper CNI calling.


> Now SomeJavaCode can be executed correctly with 'gij' interpreter; I
> think there is no JNI involved. But it cannot run on HotSpot or other
> VM. If I have to use JNI to do this, then the approach is much less
> attractive.
>
> Thanks,
> Yi
>
> On 13/02/12 15:00 , Brian Jones wrote:
>> I suggest you try jni to get to whatever native code you are
>> interested in.
>>
>> Brian
>>
>> On Feb 12, 2012, at 10:01 PM, Yi Lin<qinsoon@gmail.com> wrote:
>>
>>> Hi,
>>>
>>> I am wondering if we can use methods from a gcj-compiled library in
>>> uncompiled Java program(bytecode) executed on a VM.
>>>
>>> According to gcj FAQ 1.7, there is no problem to do this with 'gij'
>>> interpreter/runtime. You can compile some Java code into library
>>> (native code), and run bytecode with 'gij' to invoke library methods.
>>> But this seems only capable for 'gij', and other runtimes like
>>> HotSpot cannot load and link the library properly.
>>>
>>> I want to know if there is a general solution to allow you interface
>>> uncompiled Java program with gcj-compiled library. Any information or
>>> reference about this will be appreciated.
>>>
>>> Many Thanks,
>>> Yi
>
>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Interface gcj-compiled library with Java programs in a runtime (other than gij)?
  2012-02-13 18:33     ` David Daney
@ 2012-02-14  9:48       ` Andrew Haley
  2012-02-14 10:31         ` Bryce McKinlay
  0 siblings, 1 reply; 8+ messages in thread
From: Andrew Haley @ 2012-02-14  9:48 UTC (permalink / raw)
  To: David Daney; +Cc: Yi Lin, Brian Jones, java

On 02/13/2012 06:32 PM, David Daney wrote:
> I am just going from memory here, but I think that the interpreter can 
> only call native code via JNI.

Ahh, if that were really true, interpreted code couldn't call
Object.<init>() ...

Andrew.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Interface gcj-compiled library with Java programs in a runtime (other than gij)?
  2012-02-14  9:48       ` Andrew Haley
@ 2012-02-14 10:31         ` Bryce McKinlay
  2012-02-14 11:26           ` Yi Lin
  0 siblings, 1 reply; 8+ messages in thread
From: Bryce McKinlay @ 2012-02-14 10:31 UTC (permalink / raw)
  To: GCC Java

On Tue, Feb 14, 2012 at 9:47 AM, Andrew Haley <aph@redhat.com> wrote:
>
> On 02/13/2012 06:32 PM, David Daney wrote:
> > I am just going from memory here, but I think that the interpreter can
> > only call native code via JNI.
>
> Ahh, if that were really true, interpreted code couldn't call
> Object.<init>() ...


David is saying that "native" methods in interpreted classes can't be
implemented with CNI. Which is true, I think.

Obviously, the interpreter can call native CNI methods in other
(non-interpreted) classes, like Object. Hence the trampoline example.

Bryce

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Interface gcj-compiled library with Java programs in a runtime (other than gij)?
  2012-02-14 10:31         ` Bryce McKinlay
@ 2012-02-14 11:26           ` Yi Lin
  2012-02-14 11:44             ` Bryce McKinlay
  0 siblings, 1 reply; 8+ messages in thread
From: Yi Lin @ 2012-02-14 11:26 UTC (permalink / raw)
  To: java

Thank you all for the answers. I think I have some clue about CNI/JNI on 
the interface.

But I still have doubt about how a shared library compiled from Java can 
be used by a) c/c++, b) Java code running on other JVM (not libgcj 
interpreter). I think b) is straightforwad if a) is possible.

So for a), is calling a shared library of compiled java code any 
different than calling a shared library from normal c/c++ code? I have 
been spending two days exploring this and trying different approaches. 
My expectation is to load the compiled Java lib and use its method via 
dlopen, dlsym, etc. I guess another possibility is to use JvCreateVM(), 
and do everything in Java fashion. I don't have any success in either 
yet. Any hint would be very helpful so I could stop wasting time on a 
wrong track.

Thanks,
Yi

On 2012/2/14 21:31, Bryce McKinlay wrote:
> On Tue, Feb 14, 2012 at 9:47 AM, Andrew Haley<aph@redhat.com>  wrote:
>> On 02/13/2012 06:32 PM, David Daney wrote:
>>> I am just going from memory here, but I think that the interpreter can
>>> only call native code via JNI.
>> Ahh, if that were really true, interpreted code couldn't call
>> Object.<init>() ...
>
> David is saying that "native" methods in interpreted classes can't be
> implemented with CNI. Which is true, I think.
>
> Obviously, the interpreter can call native CNI methods in other
> (non-interpreted) classes, like Object. Hence the trampoline example.
>
> Bryce

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Interface gcj-compiled library with Java programs in a runtime (other than gij)?
  2012-02-14 11:26           ` Yi Lin
@ 2012-02-14 11:44             ` Bryce McKinlay
  0 siblings, 0 replies; 8+ messages in thread
From: Bryce McKinlay @ 2012-02-14 11:44 UTC (permalink / raw)
  To: Yi Lin; +Cc: java

On Tue, Feb 14, 2012 at 11:26 AM, Yi Lin <qinsoon@gmail.com> wrote:
>
> Thank you all for the answers. I think I have some clue about CNI/JNI on the interface.
>
> But I still have doubt about how a shared library compiled from Java can be used by a) c/c++, b) Java code running on other JVM (not libgcj interpreter). I think b) is straightforwad if a) is possible.
>
> So for a), is calling a shared library of compiled java code any different than calling a shared library from normal c/c++ code? I have been spending two days exploring this and trying different approaches. My expectation is to load the compiled Java lib and use its method via dlopen, dlsym, etc. I guess another possibility is to use JvCreateVM(), and do everything in Java fashion. I don't have any success in either yet. Any hint would be very helpful so I could stop wasting time on a wrong track.


a) is certainly possible, using either JNI or CNI. Whichever one you
use, you need to call a couple of functions to initialize the Java VM
before you start calling your Java classes. You just need to make sure
libgcj gets loaded somehow, either using -lgcj or dlopen.
See here for a CNI example:
http://gcc.gnu.org/onlinedocs/gcj/Invocation.html#Invocation

b) "Should" work, but maybe you'd get conflicts with things like
signal handlers since you effectively have 2 Java runtimes running in
the same process.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2012-02-14 11:44 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-02-13  3:01 Interface gcj-compiled library with Java programs in a runtime (other than gij)? Yi Lin
2012-02-13  4:01 ` Brian Jones
2012-02-13  4:30   ` Yi Lin
2012-02-13 18:33     ` David Daney
2012-02-14  9:48       ` Andrew Haley
2012-02-14 10:31         ` Bryce McKinlay
2012-02-14 11:26           ` Yi Lin
2012-02-14 11:44             ` Bryce McKinlay

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).