nguyenq / tess4j

Java JNA wrapper for Tesseract OCR API
Apache License 2.0
1.58k stars 372 forks source link

JVM crash when run TessAPITest#testTessBaseAPIDetectOrientationScript with non-existent datapath #265

Closed fredmafu closed 3 months ago

fredmafu commented 3 months ago

Hello, After I change "datapath" to a non-existent directory, image

run TessAPITest#testTessBaseAPIDetectOrientationScript and JVM crash after output these lines in console image

I tried to wrap the method body with try{...}catch(Throwable ex){...} block or call Native.setProtected(true) on the top but neither worked.

This may be a potential risk if I embed the orientation detection code in my project and get "datapath" not properly configured after deployed. May I know how to catch the exception? Thank you so much for your response.

nguyenq commented 3 months ago

It's my understanding that a Java client program can't catch or handle exceptions thrown by the native code; it would crash the JVM.

https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/crashes001.html

That said, you can code defensively by verifying the datapath, or its content, after setting it to make sure it's valid. Check the documentation for GetAvailableLanguagesAsVector or GetLoadedLanguagesAsVector API method.

fredmafu commented 3 months ago

Seems all methods in the Tesseract library that throw the assert exception by ASSERT_HOST(x) may cause the crash and no fatal error log file generated by JVM...