Skip to main content
This topic applies to FRE for Windows.

Adding FineReader Engine library to a Java project

ABBYY FineReader Engine includes a com.abbyy.FREngine-%BUILD_ID%.jar file, which contains the Java class library for FineReader Engine. You can find it in the Inc\Java folder. The path to this file can be specified in the classpath parameter in the command line and in project settings in different Java development environments. Example:
%JDK% is the path to Java Development Kit.
See the list of supported Java Development Kits in System Requirements.

Loading and unloading FineReader Engine

You can use the static Engine class to load the Engine object. The Engine class provides methods that correspond to the ABBYY FineReader Engine functions for loading and unloading the Engine:
To load Engine by means of COM, use its GetEngineInprocLoader or GetEngineOutprocLoader methods. See the description of the IEngineLoader interface for details. If you use the GetEngineOutprocLoader method for loading, you do not need to call the IHostProcessControl::SetClientProcessId method as the parent process will be set automatically.
If you develop your application on Windows but intend running it on Linux, do not use these methods for loading Engine. Use only the InitializeEngine method described above. This limitation is due to the fact that objects implementing IEngineLoader are not available on Linux.
For working with the Engine object created via an engine loader, the Engine class provides the methods which call the corresponding COM functions to marshal the interface pointer across threads:
The com.abbyy.FREngine-%BUILD_ID%.jar file is a self-unpacking archive which is unpacked on your machine the first time you use FineReader Engine Java API. The default folder where the contents are unpacked is Inc\Java for Windows. If you need to use another folder, call the Engine.SetJNIDllFolder method before loading the Engine by any of the methods described above. To find out which folder is currently set for unpacking the archive, call the Engine.GetJNIDllFolder.

Using Engine in multi-threaded Java applications

For multi-threaded Java applications you can use the EnginesPool class, which provides a complete solution for creating and managing a pool of FineReader Engine objects. This class implements the java.lang.Runnable interface.
Methods of the EnginesPool class are listed below. See the EnginesPool code sample for an example of using the EnginesPool class.

Handling errors

ABBYY FineReader Engine can throw the exceptions of the following types:
  • java.lang.OutOfMemoryError
  • com.abbyy.FREngine.EngineException
The com.abbyy.FREngine.EngineException exception inherits from the java.lang.Exception and contains one additional method int getHResult, which returns the HRESULT code of the error that occurred. For an exception of this type, you can not only retrieve the error message using the getMessage() method but also retrieve the error code.

Calling methods with out-parameters

There are several methods in ABBYY FineReader Engine API which have out-parameters that receive a new value after the method call and must be passed by reference. These parameters are marked in the type library and in descriptions of methods in this Developer’s Help as [out] or [in, out]. When working with ABBYY FineReader Engine in Java, you have to use a special Ref class to pass a parameter by reference. See examples below. Example in which the out-parameters are passed by reference to the IFRPage::FindPageSplitPosition method:
Example in which the in/out parameters are passed by reference to the ICoordinatesConverter::ConvertCoordinates method:
The following example shows the array out-parameters passed by reference to the IPlainText::GetCharacterData method:

Collecting garbage

FineReader Engine supports working with the AutoCloseable interface, which allows you to use the try statement to access the resources allocated for objects. It means that once the try block reaches the end, all allocated resources will be closed automatically, without explicitly calling closure methods. We recommend that you use the try statement for all objects in your code (see the example below):
Working with HGLOBAL Some of the methods in ABBYY FineReader Engine API take as an input parameter an HGLOBAL handle to a memory block (passed as __int64). As HGLOBAL is a Windows-specific entity, in Java wrapper these methods receive instead the contents of the memory block as byte[]. See the list of these methods: On the other hand, the methods which return a Handle object return it in Java wrapper as well.

Working with enumerations

Methods and properties which accept a combination of enumeration constants require the int value to be passed to the method/property. To obtain the int value of an enumeration constant, use the getValue method which is supported by all enumerations. Here is sample code showing how to set the BwPictureFormats property of the PDFPictureCompressionParams object:
See also Different Ways to Load the Engine Object for Windows Cross-platform development in Java for Windows