wood burning stoves 2.0*
The moose likes Java in General and the fly likes An unexpected error has been detected by HotSpot Virtual Machine: Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "An unexpected error has been detected by HotSpot Virtual Machine:" Watch "An unexpected error has been detected by HotSpot Virtual Machine:" New topic
Author

An unexpected error has been detected by HotSpot Virtual Machine:

Nandhini Venugopal
Greenhorn

Joined: May 31, 2012
Posts: 4
Hi,
I'm new to JNI programs.. I'm trying to do a JNI program.. In that i'm getting "An unexpected error has been detected by HotSpot Virtual Machine:" error while running the code..
Can any one figure out the problem.?

My Jave code
FieldAccess.java

public class FieldAccess {

public static void main(String args[]) {

next[] Nxt;
next n=new next();
System.out.println(n.g);
Nxt=n.arrayFields();
System.out.println("In Java");
}

static {
System.out.println("Hello");
System.loadLibrary("FieldAccess");



}

}

next.java

public class next{
public String g;
public String h="hi";

public native next[] arrayFields();
static {
System.out.println("Hello");
System.loadLibrary("FieldAccess");



}

}


FieldAccess.c

#include <stdio.h>

#include <jni.h>

#include "next.h"
JNIEXPORT jobjectArray JNICALL Java_next_arrayFields
(JNIEnv *env, jobject jobj){

jobjectArray ret;
int i,j;
printf("Hello");
jclass cls = (*env)->FindClass(env,"/usr/jnieg/next");
printf("Hello");
ret= (jobjectArray)(*env)->NewObjectArray(env,10,cls,NULL);
printf("Hello");
if(ret==NULL){
ret=NULL;
}
for(i=0;i<10;i++)
{
jobject remoteNode = (*env)->AllocObject(env,cls);
if (remoteNode == NULL)
{
ret = NULL;
}
jfieldID fid;
fid = (*env)->GetFieldID(env,cls, "g", "Ljava/lang/String;");
if (fid == NULL)
{
ret = NULL;
}
(*env)->SetObjectField(env,remoteNode, fid, (*env)->NewStringUTF(env,"Hello"));
(*env)->SetObjectArrayElement(env,ret, i, remoteNode);
printf("%s",remoteNode);
}
printf("Hello");
return ret;


}
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

I put your source into code tags. See how it is much easier to read? Also, can you tell us the EXACT and COMPLETE error message you get? Most folks here are not going to be able to compile and debug the C code, so without that information we're only kind of guessing. (this assume you didn't - I have no idea what an error like this would actually look like).

FieldAccess.java


next.java



FieldAccess.c



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Nandhini Venugopal wrote:

First of all, the class literal should not have a leading /. Secondly, this value indicates the class name is usr.jnieg.next, but I don't see a package declaration. If the class is not in a package, just use "next". Otherwise use "usr/jnieg/next".

Also, I would never use AllocObject. It "Allocates a new Java object without invoking any of the constructors for the object." I doubt that's what you need. Even if no constructor does anything, I would still use one of the NewObject functions.


Oh, and the last printf doesn't do what you think it does. It does not call toString() on the object. It instead takes the pointer value and treats it as a pointer to a string (a char*). The result is garbage.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Nandhini Venugopal
Greenhorn

Joined: May 31, 2012
Posts: 4
Hi,
The problem is resolved after I changed Findclass as said.
Thanks for your help.
Nandhini Venugopal
Greenhorn

Joined: May 31, 2012
Posts: 4
Hi
Again i'm getting same error while trying out to get fieldid of an string array.
I modified the code as follows

FieldAccess.java



next.java




FieldAccess.c




Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Field h is not a String, it's a String[] (array of String). Your native code treats it as a String though.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: An unexpected error has been detected by HotSpot Virtual Machine:
 
Similar Threads
JNI ( Java Native Interface)
JNI with C++ structures
Undefined Reference Trying to invoke Java from C++
Return a Structure Object from C to Java Through JNI
what's wrong with my program about JNI callback