jnilib.c File Reference

Implementation of the JNILIB functions. More...

#include "jnilib.h"
#include <stdarg.h>

Include dependency graph for jnilib.c:

Go to the source code of this file.

Defines

#define CLASSPATH_PARAM   "-Djava.class.path="
 Defines the name of the classpath parameter for the Jvm.
#define CLS_JNILIB   "at/fhv/sgr/jnilib/JNILIB"
 Defines the full name of the Java JNILIB-Class.
#define createJvm   createJvmC
 Defines which function to call when createJvm is called.
#define DEFAULT_CLASSPATH   "."
 Defines the default classpath.
#define JNILIB_ENV   (*(getJNIEnv()))
 Defines the acces to a JNIEnv structure by getJNIEnv().
#define JNILIB_GETJAVAVM   (*getJavaVM())
 Defines the acces to a JavaVM structure by getJavaVM().
#define JNILIB_GETJAVAVM_PARAM   getJavaVM(),
 Defines the getJavaVM() function parameter.
#define JNILIB_JAVAVM   (*(jvm->jvm.jvm))
 Defines the acces to a JavaVM structure.
#define JNILIB_PARAM_END_ENV   getJNIEnv()
 Defines the getJNIEnv() function parameter without comma.
#define JNILIB_PARAM_END_JVM   jvm
 Defines the jvm function parameter without comma.
#define JNILIB_PARAM_ENV   getJNIEnv(),
 Defines the getJNIEnv() function parameter.
#define JNILIB_PARAM_JAVAVM   jvm->jvm.jvm
 Defines the JavaVM function parameter.
#define JNILIB_PARAM_JVM   jvm,
 Defines the jvm function parameter.

Functions

jboolean callBooleanMethod (jobject obj, jmethodID mid,...)
 Calls a boolean method.
jboolean callBooleanMethodByName (jobject obj, const char *method, const char *signature,...)
 Calls a boolean method.
jbyte callByteMethod (jobject obj, jmethodID mid,...)
 Calls a byte method.
jbyte callByteMethodByName (jobject obj, const char *method, const char *signature,...)
 Calls a byte method.
jchar callCharMethod (jobject obj, jmethodID mid,...)
 Calls a char method.
jchar callCharMethodByName (jobject obj, const char *method, const char *signature,...)
 Calls a char method.
jdouble callDoubleMethod (jobject obj, jmethodID mid,...)
 Calls a double method.
jdouble callDoubleMethodByName (jobject obj, const char *method, const char *signature,...)
 Calls a double method.
jfloat callFloatMethod (jobject obj, jmethodID mid,...)
 Calls a float method.
jfloat callFloatMethodByName (jobject obj, const char *method, const char *signature,...)
 Calls a float method.
jint callIntMethod (jobject obj, jmethodID mid,...)
 Calls an int method.
jint callIntMethodByName (jobject obj, const char *method, const char *signature,...)
 Calls an int method.
jlong callLongMethod (jobject obj, jmethodID mid,...)
 Calls a long method.
jlong callLongMethodByName (jobject obj, const char *method, const char *signature,...)
 Calls a long method.
jobject callObjectMethod (jobject obj, jmethodID mid,...)
 Calls a object method.
jobject callObjectMethodByName (jobject obj, const char *method, const char *signature,...)
 Calls a object method.
jshort callShortMethod (jobject obj, jmethodID mid,...)
 Calls a short method.
jshort callShortMethodByName (jobject obj, const char *method, const char *signature,...)
 Calls a short method.
jint callStaticIntMethod (jclass cls, jmethodID mid,...)
 Calls a static int method.
jobject callStaticObjectMethod (jclass cls, jmethodID mid,...)
 Calls a static object method.
void callStaticVoidMethod (jclass cls, jmethodID mid,...)
 Calls a static void method.
void callVoidMethod (jobject obj, jmethodID mid,...)
 Calls a void method.
void callVoidMethodByName (jobject obj, const char *method, const char *signature,...)
 Calls a void method.
char * convertJString (jstring jstr)
 Creates a native char* from a jstring.
jobject createGlobalReference (jobject obj)
 Creates a global reference of a Java-Object and deletes the local reference.
jstring createJString (const char *str)
 Creates a jstring from a native char*.
void createJvm_ (Jvm_ *jvm)
 Initialize the Jvm_ structure by setting the function pointers.
void createJvmC (Jvm_ *jvm)
 Function is called when working with C and calling the function createJvm.
void createJvmCPP (JvmCPP_ *jvm)
 Function is called when working with C++ and calling the function createJvm.
void deleteGlobalReference (jobject obj)
 Deletes a global reference.
void deleteLocalReference (jobject obj)
 Deletes a local reference.
jint destroyJvm (Jvm *jvm)
 Unloads the Jvm.
jboolean exception ()
 Tests if an exception was thrown and prints the exception.
jclass findClass (const char *strclass)
 Finds the Java class specified by strclass.
jmethodID findMethod (jclass cls, const char *method, const char *signature)
 Finds a method of a Java class.
jfieldID findStaticField (jclass cls, const char *field, const char *signature)
 Finds a static field of a Java class.
jmethodID findStaticMethod (jclass cls, const char *method, const char *signature)
 Finds a static method of a Java class.
JavaVM * getJavaVM ()
 Gets a Pointer to the current JavaVM structure.
JNIEnv * getJNIEnv ()
 Gets a Pointer to the actual JNIEnv structure.
jclass getObjectClass (jobject obj)
 Gets the Class of the Java-Object.
jint getStaticIntFieldByName (const char *strclass, const char *strfield)
 Gets the value of a class field.
jobject getStaticObjectFieldByName (const char *strclass, const char *strfield, const char *signature)
 Gets the value of a class field.
void initJvm (Jvm *jvm, const char *classpath)
 Initializes and creates the Jvm.
jboolean isSameObject (jobject obj1, jobject obj2)
 Compares 2 objects if they are the same or not.
void jnisleep (jint ms)
 Sleeps for ms milliseconds.
jobject newObject (jclass cls, jmethodID mid,...)
 Creates a new Object of a Class.
jobject newObjectBySignature (jclass cls, const char *signature,...)
 Creates a new Object of a Class.
jint registerNatives (jclass cls, const JNINativeMethod *nm, jint nMethods)
 Resgisters a native function pointer as native function in a Java-Class.
void setJvmClasspath (Jvm *jvm, const char *cp)
 Sets the Classpath for the Jvm structure.
jint startJvm (Jvm *jvm)
 Initializes and creates the Virtual Machine.
void throwByName (const char *name, const char *msg)
 Throws an Throwable by name.

Variables

JavaVM * javavm = NULL
 Holds the created JavaVM.


Detailed Description

Implementation of the JNILIB functions.

These functions and structures provide access to a JVM. You can initialize a JVM instance and create objects and call methods by using these functions. Also some helper functions can be found here.

Author:
Stefan Gruber
Date:
11.01.2005

Definition in file jnilib.c.


Define Documentation

#define CLASSPATH_PARAM   "-Djava.class.path="
 

Defines the name of the classpath parameter for the Jvm.

Definition at line 25 of file jnilib.c.

#define CLS_JNILIB   "at/fhv/sgr/jnilib/JNILIB"
 

Defines the full name of the Java JNILIB-Class.

Definition at line 22 of file jnilib.c.

#define createJvm   createJvmC
 

Defines which function to call when createJvm is called.

Definition at line 45 of file jnilib.c.

#define DEFAULT_CLASSPATH   "."
 

Defines the default classpath.

Definition at line 28 of file jnilib.c.

#define JNILIB_ENV   (*(getJNIEnv()))
 

Defines the acces to a JNIEnv structure by getJNIEnv().

Definition at line 55 of file jnilib.c.

#define JNILIB_GETJAVAVM   (*getJavaVM())
 

Defines the acces to a JavaVM structure by getJavaVM().

Definition at line 52 of file jnilib.c.

#define JNILIB_GETJAVAVM_PARAM   getJavaVM(),
 

Defines the getJavaVM() function parameter.

Definition at line 53 of file jnilib.c.

#define JNILIB_JAVAVM   (*(jvm->jvm.jvm))
 

Defines the acces to a JavaVM structure.

Definition at line 49 of file jnilib.c.

#define JNILIB_PARAM_END_ENV   getJNIEnv()
 

Defines the getJNIEnv() function parameter without comma.

Definition at line 57 of file jnilib.c.

#define JNILIB_PARAM_END_JVM   jvm
 

Defines the jvm function parameter without comma.

Definition at line 48 of file jnilib.c.

#define JNILIB_PARAM_ENV   getJNIEnv(),
 

Defines the getJNIEnv() function parameter.

Definition at line 56 of file jnilib.c.

#define JNILIB_PARAM_JAVAVM   jvm->jvm.jvm
 

Defines the JavaVM function parameter.

Definition at line 50 of file jnilib.c.

#define JNILIB_PARAM_JVM   jvm,
 

Defines the jvm function parameter.

Definition at line 47 of file jnilib.c.


Function Documentation

jboolean callBooleanMethod jobject  obj,
jmethodID  mid,
  ...
 

Calls a boolean method.

Parameters:
obj - A Java-Object.
mid - The methodID of the method.
... - The parameters the method needs.
Returns:
the result of the called method.

Definition at line 614 of file jnilib.c.

jboolean callBooleanMethodByName jobject  obj,
const char *  method,
const char *  signature,
  ...
 

Calls a boolean method.

Parameters:
obj - A Java-Object.
method - The name of the method.
signature - The signature of the method.
... - The parameters the method needs.
Returns:
the result of the called method.

Definition at line 870 of file jnilib.c.

jbyte callByteMethod jobject  obj,
jmethodID  mid,
  ...
 

Calls a byte method.

Parameters:
obj - A Java-Object.
mid - The methodID of the method.
... - The parameters the method needs.
Returns:
the result of the called method.

Definition at line 635 of file jnilib.c.

jbyte callByteMethodByName jobject  obj,
const char *  method,
const char *  signature,
  ...
 

Calls a byte method.

Parameters:
obj - A Java-Object.
method - The name of the method.
signature - The signature of the method.
... - The parameters the method needs.
Returns:
the result of the called method.

Definition at line 898 of file jnilib.c.

jchar callCharMethod jobject  obj,
jmethodID  mid,
  ...
 

Calls a char method.

Parameters:
obj - A Java-Object.
mid - The methodID of the method.
... - The parameters the method needs.
Returns:
the result of the called method.

Definition at line 656 of file jnilib.c.

jchar callCharMethodByName jobject  obj,
const char *  method,
const char *  signature,
  ...
 

Calls a char method.

Parameters:
obj - A Java-Object.
method - The name of the method.
signature - The signature of the method.
... - The parameters the method needs.
Returns:
the result of the called method.

Definition at line 926 of file jnilib.c.

jdouble callDoubleMethod jobject  obj,
jmethodID  mid,
  ...
 

Calls a double method.

Parameters:
obj - A Java-Object.
mid - The methodID of the method.
... - The parameters the method needs.
Returns:
the result of the called method.

Definition at line 782 of file jnilib.c.

jdouble callDoubleMethodByName jobject  obj,
const char *  method,
const char *  signature,
  ...
 

Calls a double method.

Parameters:
obj - A Java-Object.
method - The name of the method.
signature - The signature of the method.
... - The parameters the method needs.
Returns:
the result of the called method.

Definition at line 1066 of file jnilib.c.

jfloat callFloatMethod jobject  obj,
jmethodID  mid,
  ...
 

Calls a float method.

Parameters:
obj - A Java-Object.
mid - The methodID of the method.
... - The parameters the method needs.
Returns:
the result of the called method.

Definition at line 761 of file jnilib.c.

jfloat callFloatMethodByName jobject  obj,
const char *  method,
const char *  signature,
  ...
 

Calls a float method.

Parameters:
obj - A Java-Object.
method - The name of the method.
signature - The signature of the method.
... - The parameters the method needs.
Returns:
the result of the called method.

Definition at line 1038 of file jnilib.c.

jint callIntMethod jobject  obj,
jmethodID  mid,
  ...
 

Calls an int method.

Parameters:
obj - A Java-Object.
mid - The methodID of the method.
... - The parameters the method needs.
Returns:
the result of the called method.

Definition at line 719 of file jnilib.c.

jint callIntMethodByName jobject  obj,
const char *  method,
const char *  signature,
  ...
 

Calls an int method.

Parameters:
obj - A Java-Object.
method - The name of the method.
signature - The signature of the method.
... - The parameters the method needs.
Returns:
the result of the called method.

Definition at line 982 of file jnilib.c.

jlong callLongMethod jobject  obj,
jmethodID  mid,
  ...
 

Calls a long method.

Parameters:
obj - A Java-Object.
mid - The methodID of the method.
... - The parameters the method needs.
Returns:
the result of the called method.

Definition at line 740 of file jnilib.c.

jlong callLongMethodByName jobject  obj,
const char *  method,
const char *  signature,
  ...
 

Calls a long method.

Parameters:
obj - A Java-Object.
method - The name of the method.
signature - The signature of the method.
... - The parameters the method needs.
Returns:
the result of the called method.

Definition at line 1010 of file jnilib.c.

jobject callObjectMethod jobject  obj,
jmethodID  mid,
  ...
 

Calls a object method.

Parameters:
obj - A Java-Object.
mid - The methodID of the method.
... - The parameters the method needs.
Returns:
the result of the called method.

Definition at line 824 of file jnilib.c.

jobject callObjectMethodByName jobject  obj,
const char *  method,
const char *  signature,
  ...
 

Calls a object method.

Parameters:
obj - A Java-Object.
method - The name of the method.
signature - The signature of the method.
... - The parameters the method needs.
Returns:
the result of the called method.

Definition at line 1094 of file jnilib.c.

jshort callShortMethod jobject  obj,
jmethodID  mid,
  ...
 

Calls a short method.

Parameters:
obj - A Java-Object.
mid - The methodID of the method.
... - The parameters the method needs.
Returns:
the result of the called method.

Definition at line 677 of file jnilib.c.

jshort callShortMethodByName jobject  obj,
const char *  method,
const char *  signature,
  ...
 

Calls a short method.

Parameters:
obj - A Java-Object.
method - The name of the method.
signature - The signature of the method.
... - The parameters the method needs.
Returns:
the result of the called method.

Definition at line 954 of file jnilib.c.

jint callStaticIntMethod jclass  cls,
jmethodID  mid,
  ...
 

Calls a static int method.

Parameters:
cls - A Java-Class.
mid - The methodID of the method.
... - The parameters the method needs.
Returns:
the result of the called method.

Definition at line 698 of file jnilib.c.

jobject callStaticObjectMethod jclass  cls,
jmethodID  mid,
  ...
 

Calls a static object method.

Parameters:
cls - A Java-Class.
mid - The methodID of the method.
... - The parameters the method needs.
Returns:
the result of the called method.

Definition at line 803 of file jnilib.c.

void callStaticVoidMethod jclass  cls,
jmethodID  mid,
  ...
 

Calls a static void method.

Parameters:
cls - A Java-Class.
mid - The methodID of the method.
... - The parameters the method needs.

Definition at line 578 of file jnilib.c.

void callVoidMethod jobject  obj,
jmethodID  mid,
  ...
 

Calls a void method.

Parameters:
obj - A Java-Object.
mid - The methodID of the method.
... - The parameters the method needs.

Definition at line 595 of file jnilib.c.

void callVoidMethodByName jobject  obj,
const char *  method,
const char *  signature,
  ...
 

Calls a void method.

Parameters:
obj - A Java-Object.
method - The name of the method.
signature - The signature of the method.
... - The parameters the method needs.

Definition at line 844 of file jnilib.c.

char* convertJString jstring  jstr  ) 
 

Creates a native char* from a jstring.

Parameters:
jstr - A JString.
Returns:
a native character string.
Warning:
You have to free the memory of the native character string yourself with free()!
See also:
createJString(const char* str)

Definition at line 455 of file jnilib.c.

jobject createGlobalReference jobject  obj  ) 
 

Creates a global reference of a Java-Object and deletes the local reference.

Parameters:
obj - The object reference to create the global reference of.
Returns:
The global reference.

Definition at line 371 of file jnilib.c.

jstring createJString const char *  str  ) 
 

Creates a jstring from a native char*.

Parameters:
str - A native character string.
Returns:
a JString.
See also:
convertJString(jstring jstr)

Definition at line 418 of file jnilib.c.

void createJvm_ Jvm_ jvm  ) 
 

Initialize the Jvm_ structure by setting the function pointers.

Parameters:
jvm - Pointer to a Jvm_ struct.

Definition at line 200 of file jnilib.c.

void createJvmC Jvm_ jvm  ) 
 

Function is called when working with C and calling the function createJvm.

It initializes the Jvm_ structure.

Parameters:
jvm - Pointer to a Jvm_ struct.

Definition at line 223 of file jnilib.c.

void createJvmCPP JvmCPP_ jvm  ) 
 

Function is called when working with C++ and calling the function createJvm.

It initializes the JvmCPP_ structure.

Parameters:
jvm - Pointer to a JvmCPP_ struct.

Definition at line 212 of file jnilib.c.

void deleteGlobalReference jobject  obj  ) 
 

Deletes a global reference.

Parameters:
obj - The reference to be deleted.

Definition at line 360 of file jnilib.c.

void deleteLocalReference jobject  obj  ) 
 

Deletes a local reference.

Parameters:
obj - The reference to be deleted.

Definition at line 351 of file jnilib.c.

jint destroyJvm Jvm jvm  ) 
 

Unloads the Jvm.

Warning:
This function hangs until all threads have ended.
Parameters:
jvm - Pointer to a Jvm struct.
Returns:
0 on success, otherwise a negative number.

Definition at line 191 of file jnilib.c.

jboolean exception  ) 
 

Tests if an exception was thrown and prints the exception.

Returns:
JNI_TRUE if an exception was thrown, otherwise JNI_FALSE.

Definition at line 395 of file jnilib.c.

jclass findClass const char *  strclass  ) 
 

Finds the Java class specified by strclass.

Parameters:
strclass - Full name of the class.
Returns:
A global reference to the class.
See also:
getObjectClass(jobject obj)

Definition at line 236 of file jnilib.c.

jmethodID findMethod jclass  cls,
const char *  method,
const char *  signature
 

Finds a method of a Java class.

Parameters:
cls - A Java-Class.
method - The name of the method.
signature - The signature of the method.
Returns:
The methodID of the function.
See also:
findStaticMethod(jclass cls, const char* method, const char* signature)

Definition at line 319 of file jnilib.c.

jfieldID findStaticField jclass  cls,
const char *  field,
const char *  signature
 

Finds a static field of a Java class.

Parameters:
cls - A Java-Class.
field - The name of the field.
signature - The signature of the field.
Returns:
The methodID of the function.

Definition at line 299 of file jnilib.c.

jmethodID findStaticMethod jclass  cls,
const char *  method,
const char *  signature
 

Finds a static method of a Java class.

Note:
To get the signature of a function run the javap tool with the Parameters -s and -p. The "-s" flag tells javap to output signatures. The "-p" flag lets javap include private members.

Usage: javap -s -p {PACKAGE}.{CLASSNAME}
Parameters:
cls - A Java-Class.
method - The name of the method.
signature - The signature of the method.
Returns:
The methodID of the function.
See also:
findMethod(jclass cls, const char* method, const char* signature)

Definition at line 281 of file jnilib.c.

JavaVM* getJavaVM  ) 
 

Gets a Pointer to the current JavaVM structure.

Returns:
Pointer to a JavaVM structure.

Definition at line 86 of file jnilib.c.

JNIEnv* getJNIEnv  ) 
 

Gets a Pointer to the actual JNIEnv structure.

Attention:
To get the right JNIEnv call AttachCurrentThread. It returns the JNIEnv of the current thread. If the Thread is already attached, it only returns the JNIEnv.
Returns:
Pointer to a JNIEnv structure.

Definition at line 104 of file jnilib.c.

jclass getObjectClass jobject  obj  ) 
 

Gets the Class of the Java-Object.

Parameters:
obj - A Java object.
Returns:
A global reference to the class.
See also:
findClass(const char* strclass)

Definition at line 260 of file jnilib.c.

jint getStaticIntFieldByName const char *  strclass,
const char *  strfield
 

Gets the value of a class field.

Parameters:
strclass - The Name of the Java Class.
strfield - The name of the field.
Returns:
the value of the class field.

Definition at line 1120 of file jnilib.c.

jobject getStaticObjectFieldByName const char *  strclass,
const char *  strfield,
const char *  signature
 

Gets the value of a class field.

Parameters:
strclass - The Name of the Java Class.
strfield - The name of the field.
signature - The signature of the field.
Returns:
the value of the class field.

Definition at line 1144 of file jnilib.c.

void initJvm Jvm jvm,
const char *  classpath
 

Initializes and creates the Jvm.

Parameters:
jvm - Pointer to a Jvm struct.
classpath - The classpath for the Jvm.

Definition at line 75 of file jnilib.c.

jboolean isSameObject jobject  obj1,
jobject  obj2
 

Compares 2 objects if they are the same or not.

Parameters:
obj1 - The first object to compare.
obj2 - The second object to ccompare.
Returns:
JNI_TRUE if they are the same, otherwise JNI_FALSE;

Definition at line 386 of file jnilib.c.

void jnisleep jint  ms  ) 
 

Sleeps for ms milliseconds.

Parameters:
ms - Milliseconds to sleep for.

Definition at line 512 of file jnilib.c.

jobject newObject jclass  cls,
jmethodID  mid,
  ...
 

Creates a new Object of a Class.

Parameters:
cls - A Java-Class.
mid - The methodID of the Constructor.
... - The parameters the Constructor needs.
Returns:
a global reference of the object.

Definition at line 532 of file jnilib.c.

jobject newObjectBySignature jclass  cls,
const char *  signature,
  ...
 

Creates a new Object of a Class.

Parameters:
cls - A Java-Class.
signature - The signature of the method.
... - The parameters the Constructor needs.
Returns:
a global reference of the object.

Definition at line 555 of file jnilib.c.

jint registerNatives jclass  cls,
const JNINativeMethod *  nm,
jint  nMethods
 

Resgisters a native function pointer as native function in a Java-Class.

Parameters:
cls - A Java-Class.
nm - Pointer to the JNINativeMethod structs.
nMethods - Number of methods to register.
Returns:
0 on success, otherwise a negative number.

Definition at line 337 of file jnilib.c.

void setJvmClasspath Jvm jvm,
const char *  cp
 

Sets the Classpath for the Jvm structure.

Parameters:
jvm - Pointer to a Jvm struct.
cp - The classpath for the Jvm.

Definition at line 121 of file jnilib.c.

jint startJvm Jvm jvm  ) 
 

Initializes and creates the Virtual Machine.

Parameters:
jvm - Pointer to a Jvm struct.
Returns:
A number smaller 0 if the creation of the Jvm failed, otherwise it returns 0.

Definition at line 132 of file jnilib.c.

void throwByName const char *  name,
const char *  msg
 

Throws an Throwable by name.

Parameters:
name - Name of the Throwable.
msg - The Message for the Throwable.

Definition at line 497 of file jnilib.c.


Variable Documentation

JavaVM* javavm = NULL
 

Holds the created JavaVM.

Definition at line 61 of file jnilib.c.


Generated on Sat Nov 19 14:11:13 2005 for GrubC by  doxygen 1.4.4