Skip to content

Commit

Permalink
Updated documentation, fixed exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
xevor11 committed Sep 15, 2024
1 parent bb76d66 commit c1944d1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DEPENDENCIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,4 @@ Support for JNI (Java Native Interface) is provided by:

The JDK is distributed under various open-source licenses depending on the vendor and version. Common licenses include the GNU General Public License (GPL) and the Oracle Binary Code License Agreement.

To enable JNI support, ensure that the JDK is installed on your system, and set the appropriate environment variables (e.g., JAVA_HOME) to point to the JDK installation directory.
To enable JNI support, ensure that the JDK is installed on your system, and set the appropriate environment variables (e.g., JAVA_HOME) to point to the JDK installation directory.
4 changes: 3 additions & 1 deletion NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ NEWS - user visible changes -*- outline -*-


* New GnuCOBOL features
** Initial support for Java interoperability through JNI (new optional dependency JDK)
** Initial support for Java interoperability through JNI (optional dependency JDK): Java Interoperability: Added initial support for calling
Java methods from COBOL programs via the Java Native Interface (JNI). This feature currently supports static Java methods with no parameters, while handling parameters and return values will be introduced in future updates. The system detects missing Java classes and malformed method names and provides proper error handling. COBOL developers can now use ON EXCEPTION and NOT ON EXCEPTION clauses with Java calls. JNI support is an optional feature that requires the Java Development Kit (JDK) to be installed on the system. Use the --with-java configure option to enable it, and ensure that the JAVA_HOME environment variable is set at runtime to locate the JDK. Without JNI, the runtime will function as usual but Java-related COBOL calls (e.g., CALL "Java.<Class>.<Method>") will not be available

** file handling: added backends for ODBC (so far PostgrSQL, MySQL, SQLite,
MSSQL) and OCI, along with new directory COB_SCHEMA_DIR containing the
necessary internal schema files to match the file definition to the
Expand Down
36 changes: 33 additions & 3 deletions libcob/java.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,39 @@ call_java (const cob_java_handle *method_handle)
(*env)->CallStaticVoidMethod(env,
method_handle->cls,
method_handle->mid, NULL);
if ((*env)->ExceptionCheck(env)) {
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);
jthrowable exception = (*env)->ExceptionOccurred(env);
if(exception) {
jclass throwable = (*env)->FindClass(env, "java/lang/Throwable");
jmethodID getMessage = (*env)->GetMethodID(env,
throwable, "getMessage", "()Ljava/lang/String;");
if(getMessage != NULL) {
jstring message = (jstring)(*env)->CallObjectMethod(env, exception, getMessage);
const char *messageChars = (*env)->GetStringUTFChars(env, message, NULL);
cob_runtime_error(_("Java exception: %s"), messageChars);
(*env)->ReleaseStringUTFChars(env, message, messageChars);
(*env)->DeleteLocalRef(env, message);
}
jclass stringWriter = (*env)->FindClass(env, "java/io/StringWriter");
jclass printWriter = (*env)->FindClass(env, "java/io/PrintWriter");
jobject stringWriterObj = (*env)->NewObject(env,
stringWriter,
(*env)->GetMethodID(env, stringWriter, "<init>", "()V"));
jobject printWriterObj = (*env)->NewObject(env,
printWriter,
(*env)->GetMethodID(env, printWriter, "<init>", "(Ljava/io/Writer;)V"),
stringWriterObj);
jmethodID printStackTrace = (*env)->GetMethodID(env, throwable, "printStackTrace", "(Ljava/io/PrintWriter;)V");
(*env)->CallVoidMethod(env, exception, printStackTrace, printWriter);
jmethodID toString = (*env)->GetMethodID(env, stringWriter, "toString", "()Ljava/lang/String;");
jstring stackTrace = (jstring)(*env)->CallObjectMethod(env, stringWriterObj, toString);
const char *stackTraceChars = (*env)->GetStringUTFChars(env, stackTrace, NULL);
cob_runtime_error(_("Java stack trace: %s"), stackTraceChars);

(*env)->ReleaseStringUTFChars(env, stackTrace, stackTraceChars);
(*env)->DeleteLocalRef(env, stackTrace);
(*env)->DeleteLocalRef(env, stringWriterObj);
(*env)->DeleteLocalRef(env, printWriterObj);
(*env)->DeleteLocalRef(env, exception);
}
}

Expand Down

0 comments on commit c1944d1

Please sign in to comment.