This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes jni(urgent please) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "jni(urgent please)" Watch "jni(urgent please)" New topic
Author

jni(urgent please)

gopi krish
Greenhorn

Joined: May 28, 2001
Posts: 10
Hi,

i am calling a "C" function from java code.that c function initiates a process and a date base. when i call that function in
java main() function it is working perfectly well. but i need to call that function at runtime, means when i click "OK" button from GUI.so i called this c function inside the action performed method.But when i click the "OK" button then immediately core dumping.why is it happening like that.
here i have given some sample code.

class example implements ActionListener {
public native int cfunction();
static {
System.loadLibrary("hello");
}
public example(){
Container contentPane=getContentPane();
contentPane.setLayout(new FlowLayout());
JPanel panel = new JPanel();
JButton listButton = new JButton("ok");
ok.addActionListener(this);
panel.add(ok);
contentPane.add(panel);
}
public void actionperformed(ActionEvent a){
Strng s=a.getActionCommand();
if(s.equals("ok"))
{
cfunction(); // calling the cfunction
}
}
public static void main(String s[])
{
// ********* if u call cfunction here working well
example e = new example();
e.setVisible(true);
e.setSize(300,300);
}

this is the sample code . when i click on "ok" button core dumping. but when i call the same cfunction at the begining of main function (as marked in // ******** line) insteadof in action performed method it is working perfectly well.even when i call that function in the cunstructor it is working well.but i want to
call that cfunction at runtime.How do i do that.
even i used synchronised key word in begining of the actionperformed method. still it is core dumping.
in core dumping it is givining something like the following

Java version:
# HotSpot VM (1.0.1fcs, mixed mode, PA1.1 build 1.2.2.06-00/10/23-PA_RISC1.1)
#
# HotSpot Virtual Machine Error, Unexpected Signal 11
#
# occurred at pc=c0a52370

please anybody help me asearly as possible.
advanced thanks.

gopi.
Nura Horne
Ranch Hand

Joined: Jul 26, 2001
Posts: 40
This error usually occurs when you have not fully celaned up in your native method, i.e. you haven't dealloc'd all the memory you used or converted back to the correct java types i.e. jint.
Hence when you run cfunction() the firts time in main it will work fine, but if you run it again when the button is clicked, remnants (dangling ptrs etc) are left from its last execution that bring the JVM down.
I found when working the JNI that a very subtle chnage in the C code could remove this erro, ie. altering the order of non-dependent code in the file, ie.
if you have:
jint int1, int2;
int1 = 1;
int2 = 2;
Try replacing it with
int2 = 2;
int1 = 1;
The above is a very simple example but shows the concept.
Hope this helps, A
gopi krish
Greenhorn

Joined: May 28, 2001
Posts: 10
Hi arun,
thanks for ur response.
i need to call the cfunction first time itself at runtime ie when i click the button.More over i am not passing any arguments (since u suggested to interchange the assignment of arguments). inside that function i am calling only one c function thats all.

when i tested that function by calling in main() method it is working (this is only for testing).but actually i have to call that function at runtime.
please answer me.
onceagain very very thanks for ur help
gopi

Originally posted by Arun Horne:
This error usually occurs when you have not fully celaned up in your native method, i.e. you haven't dealloc'd all the memory you used or converted back to the correct java types i.e. jint.
Hence when you run cfunction() the firts time in main it will work fine, but if you run it again when the button is clicked, remnants (dangling ptrs etc) are left from its last execution that bring the JVM down.
I found when working the JNI that a very subtle chnage in the C code could remove this erro, ie. altering the order of non-dependent code in the file, ie.
if you have:
jint int1, int2;
int1 = 1;
int2 = 2;
Try replacing it with
int2 = 2;
int1 = 1;
The above is a very simple example but shows the concept.
Hope this helps, A

Nura Horne
Ranch Hand

Joined: Jul 26, 2001
Posts: 40
If you email your source code in a ZIP to hornearun@hotmail.com I will try and find time to have a look at it.
A
 
Don't get me started about those stupid light bulbs.
 
subject: jni(urgent please)