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: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:| Function | Engine class method | The signature of the Engine class method |
|---|---|---|
| InitializeEngine | InitializeEngine | csharp static public IEngine InitializeEngine( String dllFolder, String customerProjectId, String licensePath, String licensePassword, String dataFolder, String tempFolder, boolean isSharedCPUCoresMode ) throws Exception; |
| DeinitializeEngine | DeinitializeEngine | static public void DeinitializeEngine() throws Exception; |
Sample code
Sample code
Sample code using InprocLoader
Sample code using InprocLoader
Sample code using OutprocLoader
Sample code using OutprocLoader
| Method | Signature | Comment |
|---|---|---|
| MarshalInterface | long MarshalInterface(); | Calls the CoMarshalInterface COM function to marshal the IEngine interface. This method should be called on the thread where the engine was created, to ensure that the data needed for creating a proxy object on another thread will be available. Returns the handle to the marshaling data. |
| UnmarshalInterface | csharp IEngine UnmarshalInterface( long handle ); | Calls the CoUnmarshalInterface COM function to unmarshal the IEngine interface, i.e., create a proxy object with which the client process will be able to interact in the same way as with the engine itself. Takes the handle to the marshaling data (returned by the MarshalInterface method) as the input parameter and returns a pointer to the IEngine interface. |
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.| Method | Signature | Comment |
|---|---|---|
| constructor | public EnginesPool( int enginesCount, int waitingEngineTimeout, String customerProjectId, String licensePath, String licensePassword, String dataFolder, String tempFolder, boolean isSharedCPUCoresMode ) throws Exception; | Creates a new pool of enginesCount Engines. The waitingEngineTimeout sets the timeout for EnginesPool.GetEngine. Other parameters are the same as in InitializeEngine |
| GetEngine | csharp public IEngine GetEngine() throws Exception; | Gets an Engine instance from the pool. Throws an exception if waitingEngineTimeout is exceeded. |
| ReleaseEngine | csharp public void ReleaseEngine( IEngine engine, boolean isRecycleRequired ) throws Exception; | Returns an Engine instance to the pool. If isRecycleRequired is true, deletes this instance and replaces it with a new one (even if the usage limit is not reached, or automatic recycle is disabled). |
| SetAutoRecycleUsageCount | public void SetAutoRecycleUsageCount( int value ); | Set how many times a single Engine instance in this pool can be re-used until the instance is recycled: deleted and replaced with a new one automatically. The count can be set to 0 to never recycle automatically (only manual recycling will be possible, via ReleaseEngine). Default is 0 (never recycle automatically). |
| GetAutoRecycleUsageCount | public int GetAutoRecycleUsageCount(); | Get the Engine instance re-use limit for this pool. |
| UnloadEngines | public void UnloadEngines() throws Exception; | Unload all Engines and deinitialize the pool. |
Handling errors
ABBYY FineReader Engine can throw the exceptions of the following types:- java.lang.OutOfMemoryError
- com.abbyy.FREngine.EngineException
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:Sample code for out-parameters
Sample code for out-parameters
Sample code for in/out parameters
Sample code for in/out parameters
Sample code for array out-parameters
Sample code for array out-parameters
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):Sample code
Sample code
- LoadImageDocFromMemory, OpenImageFileFromMemory methods of the Engine object
- LoadFromMemory method provided by many objects
- AddImageFileFromMemory method of the FRDocument object
- AddFromMemory method of the PDFAttachments object
