0

I am using JNA in order to call C functions from Java. Sometimes the C functions are crashing and they are also making the java application to crash - an unwanted effect.

If I run my program from the IDE I got the following message with exit code:

Process finished with exit code -1073740940 (0xC0000374)

If i run the compiled java application I get a general error message:

Java Error

Do you know how could I prevent these type of errors from crashing the java program?

All calls are already wrapped in try- catch throwable objects.

I should mention that I have no control over the C program or the jna wrapper, I am just calling the methods. (As a matter of fact I am using the wrapper tes4j for tesseract, and it crashes for certain images, but this did not seem relevant for finding a solution).

user3452075
  • 411
  • 1
  • 6
  • 17

1 Answers1

3

Not possible.

Your C code/libs are run natively inside your process, outside of the virtual machine. In other words, errors occurring in that code will not be handled by the VM.

You may be able to get some ugly hack together by setting up your own signal handlers, but your process' in-memory state may still be messed up from wild pointers. Doing this is a really bad idea™. I mean, you know that the code is mishandling pointers, so what if it also writes to a valid (but wrong) location? All kinds of terrible things could happen.

It may be possible to write a small stand-alone C program and call it through Runtime.exec(). That way, any crashes would just result in a failing return code from the child process. Of course, any complex return values or in-memory side effects will not be easy to achieve -- so whether this method is advisable or not depends on your use case.

Snild Dolkow
  • 6,669
  • 3
  • 20
  • 32
  • I already considered the alternative of using Runtime.exec(), but the return value is quite complex. Do you think that by using another java process to make native calls, I could send back complex results using RMI? – user3452075 Dec 26 '15 at 21:10
  • Maybe. But that other Java process would most likely crash, too... it's a really horrible way of going about things. :) – Snild Dolkow Dec 26 '15 at 21:32