Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Use native methods to expand the Java environment

Learn how to interact with libraries and applications written in other languages

  • Print
  • Feedback

Page 5 of 7

/* Call the method we want to use and raise an exception */
printf("Second call to Java returns:%d\n", (*env)->CallStaticIntMethod(env, cls, mid, 2) );
/* Check for an exception */ 
if ( hasExceptionOccurred ( env ) != (char *)0 ) {
  printf("Exception has occurred.\n");
}


The C function hasExceptionOccurred( env ) returns 0 if no exception was raised. If there is an error while attempting to process the exception response, the function hasExceptionOccurred will stop execution and will terminate itself with an exit call.

Note: Do not confuse this process with processing the exception from an application's point of view. Before the application can process an exception response, it must have the exception type, string, and, possibly, the stack at the time of the exception. If the exception is valid and has been converted to C format, the application can process it.

The fatal errors hasExceptionOccurred will exit for are:

  • Unable to find the object class for Java.lang.Throwable -- We must have this class to take the exception apart.

  • Unable to find the method id for getMessage in Java.lang.Throwable -- We found the class, but it does not have the method we are looking for.

  • Unable to call the getMessage method in Java.lang.Throwable -- We have the instance, but there is no valid message.

  • Unable to convert UTF (the string format used by the Java VM when mapping to C strings) characters from the String object returned from getMessage -- We are unable to complete the conversion due to mapping difficulties.


Processing exception information raised in a Java application called from C

If an exception has occurred, it is important to find out what type of exception it is and the error string it produces, if any. Many badly written programs do not go the extra distance to get exception information. The tcl2JavaVM.c program includes a simple function for dealing with exceptions more completely. The function defines some automatics, or variables that are allocated on the stack at runtime and disappear when the function returns. Automatics are used to store information used in the program, as well as to allocate space for data to be fetched from the Java VM. The following automatics, some of which you are now familiar with, are defined by the function:

  • jthr is a reference to an instance of Java.lang.Throwable.

  • jThrowableClass is the class definition for Java.lang.Throwable.

  • mid is a reference to a method.

  • errorString points to a Java UTF string.

  • jbyte maps C constant byte strings to Java immutable byte strings.


We have demonstrated how to launch a Java application from a C program, and how to pass arguments to the Java application, how to get a return argument from the Java method, and how to deal with exceptions. By mastering these techniques you can reuse Java applications from C programs. Calling C code from Java for communicating to serial ports
Note: The examples used in the remainder of this article are for serial device RS232 drivers for Solaris and Windows 95/NT. Because of potential security risks, Java does not support dynamically installable device drivers. One of the reasons my Windows 95 is unstable is due to device drivers that are routinely updated on my machine. I have no choice regarding the updates because many programs don't tell you they are changing your device drivers. Few languages have included device support directly in the language, as it was considered part of the operating system's responsibility. (The only language I am aware of that provided direct support for devices was Ada.)

  • Print
  • Feedback

Resources