Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Recommended way to launch JVM from JNI (Again)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Recommended way to launch JVM from JNI (Again)



I am still looking at some code, not mine - X-platform language interface kind of a deal (ooRexx to java), that attempts to launch a JVM from JNI. This code may do AWT or Swing so I am trying to do it off the main thread as recommended in the simpleJavaLauncher sample.
As I noted before the existing sample seems to either exit the code entirely or hang. So I have been trying to familiarize myself with CFRunLoop and related to come up with a better way of handling this.
Eventually I seemed able to serialize the processing somewhat correctly...


________________________________________________________________________ _
CFRunLoopRun 3146400
BsfLoadJava(): AFTER create a JVM, res=[0], JNI=[2]
jniEnv=[0x5014d4], JAVA_4_REXX=[org/rexxla/bsf/engines/rexx/ Java4Rexx], cls=[0x5015e4]
globObj=0x0, globVm=0xac875ee8, globORef=0x0, globJavaVM=0x0
BsfLoadJava(): about to leave...


completion callback
callback stopping runloop 3146400
return
________________________________________________________________________ ___


So the JVM is started along the lines of the sample code and signal posting the completion callback stops the runloop and the code returns.
Unfortunately a little farther on it gets a...
Program received signal EXC_BAD_ACCESS, Could not access memory.


Looking into the flow it appeared a callback from the java into the JNI code was not being done. Looking at the java I noticed there was some logger functionality so I added the appropriate logging jar to the classpath

export CLASSPATH="/opt/ooRexx/lib/commons-logging-1.1.jar:/opt/ ooRexx/lib/bsf-rexx-engine.jar:/opt/ooRexx/lib/bsf-v240-20061006.jar"

Surprisingly the JVM callback into the JNI code then started working, so apparently missing the logging jar was causing something to fail quietly?

However, now again the serialized processing has disappeared, the 'return' message above is never reached.

____________________________________________________________
BsfLoadJava(): about to leave...

current 3146400 not waiting
completion callback
callback stopping runloop 3146400
______________________________________________________________

and nothing after that. So with that working it almost seems a deadlock or something has been introduced? (Also why does the runLoop seem to not be waiting? Although the completion callback is still hit when it's source is added and the runloop stop issued).

gdb partial trace
#0 0x9000b348 in mach_msg_trap ()
#1 0x9000b29c in mach_msg ()
#2 0x907ddad8 in __CFRunLoopRun ()
#3 0x907dd3dc in CFRunLoopRunSpecific ()
#4 0x93299b20 in RunCurrentEventLoopInMode ()
#5 0x932991b4 in ReceiveNextEventCommon ()
#6 0x93299020 in BlockUntilNextEventMatchingListInMode ()
#7 0x937c4bc4 in _DPSNextEvent ()
#8 0x937c4888 in -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] ()
#9 0x937c0dcc in -[NSApplication run] ()
#10 0x9fe4c808 in +[AWTStarter startAWT:] ()
#11 0x92bef92c in __NSFireMainThreadPerform ()
#12 0x9080ea3c in __CFRunLoopPerformPerform ()
#13 0x907de42c in __CFRunLoopDoSources0 ()
#14 0x907dd95c in __CFRunLoopRun ()
#15 0x907dd3dc in CFRunLoopRunSpecific ()
#16 0x907ec83c in CFRunLoopRun ()
#17 0x000e58cc in BsfLoadJava ()


+[AWTSTarter startAWT:] comes up about 150 times on Google but nothing real revealing to me.

This past list thread seems somewhat similar...
Can I launch and keep the JavaVM around?

http://lists.apple.com/archives/java-dev/2006/Jan/msg00054.html

From that...
<quote>
As far as I can tell, in order to call Java from C or C++, it is required that the JavaVM be instantiated on a secondary thread. Further, it appears that calls from the secondary thread to Java JNI functions will cause a transfer of control to the main thread. If the main thread is blocked (for example, "MPWaitOnQueue") waiting for the Java task to complete, the app hangs. If the main thread has any event processing initiated (for example "RunApplicationEventLoop") the app crashes.
</quote>


Seems to describe the rock and hard place you are between with hangs and crashes.

So, roughly what I would like to do is start a JVM I can do AWT on, then keep it around for reuse.
How should I approach doing this? Has anyone been successful with code I can see that does similar? Are there any recommendations beyond the sample code for this situation?


Mike Hall        hallmike at att dot net
http://www.geocities.com/mik3hall
http://sourceforge.net/projects/macnative



_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden




Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.