Of course. Here is a comprehensive guide to using Java with OpenOffice (and its successor, Apache OpenOffice).

Important Clarification: OpenOffice vs. Apache OpenOffice
- OpenOffice.org: The original project, which was forked in 2010.
- Apache OpenOffice: The current, official version of the software maintained by the Apache Software Foundation. It's what you download today.
- LibreOffice: Another popular fork of OpenOffice. While similar, it has its own development path and Java integration.
This guide focuses on Apache OpenOffice, as it's the direct continuation of the original project. The core Java API remains the same.
The Core Java API: UNO (Universal Network Objects)
Apache OpenOffice exposes its functionality through a component model called UNO (Universal Network Objects). This is a cross-language, cross-platform component architecture that allows external applications (like a Java program) to control OpenOffice.
You interact with OpenOffice by connecting to a running "UNO service" and then calling its methods.
Prerequisites: Setting Up Your Environment
Before you write any code, you need to configure your system.

A. Install Apache OpenOffice
- Download and install the latest version of Apache OpenOffice.
- During installation, make sure the Java option is checked. OpenOffice uses a Java Runtime Environment (JRE) to run its UNO bridge. If you don't have a JRE installed, the installer might offer to download one for you.
B. Locate the UNO Java JARs
The Java libraries you need are inside the OpenOffice installation directory. The path varies by operating system:
- Windows:
C:\Program Files (x86)\Apache OpenOffice 4\program\ - macOS:
/Applications/OpenOffice.app/Contents/ - Linux:
/opt/openoffice4/program/(or similar, depending on your installation)
Inside this program folder, you will find several JAR files. The most important one is:
juh.jar(Java UNO Helper)jurt.jar(Java UNO Runtime)ridl.jar(Remote Interface Definition Language)unoil.jar(UNO Interoperability Layer) - This is the main one you'll use in your code.
You will need to add these JAR files to your Java project's classpath.
Step-by-Step: A Java Example to Create a Document
This example will demonstrate how to:

- Start a hidden OpenOffice instance.
- Connect to it from Java.
- Create a new text document.
- Write some text to it.
- Save the document and close it.
Project Setup (using an IDE like IntelliJ or Eclipse)
- Create a new Java project.
- Find the
juh.jar,jurt.jar,ridl.jar, andunoil.jarfiles in your OpenOfficeprogramdirectory. - Add these JAR files to your project's libraries/dependencies.
The Java Code
import com.sun.star.beans.PropertyValue;
import com.sun.star.frame.XStorable;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.text.XText;
import com.sun.star.text.XTextDocument;
import com.sun.star.uno.XComponentContext;
public class OpenOfficeJavaExample {
// Path to your OpenOffice installation
private static final String OPEN_OFFICE_PATH = "C:\\Program Files (x86)\\Apache OpenOffice 4\\program";
public static void main(String[] args) {
try {
// 1. Get the component context
XComponentContext xComponentContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
if (xComponentContext == null) {
System.err.println("Failed to bootstrap the OpenOffice component context.");
return;
}
// 2. Get the service manager
XMultiComponentFactory xServiceManager = xComponentContext.getServiceManager();
if (xServiceManager == null) {
System.err.println("Failed to get the service manager.");
return;
}
// 3. Create a new text document
Object oDesktop = xServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", xComponentContext);
com.sun.star.frame.XDesktop xDesktop = (com.sun.star.frame.XDesktop) com.sun.star.uno.UnoRuntime.queryInterface(com.sun.star.frame.XDesktop.class, oDesktop);
// Create a new document by dispatching a URL
PropertyValue[] loadArgs = new PropertyValue[0];
XComponent xTextComponent = xDesktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, loadArgs);
XTextDocument xTextDocument = (XTextDocument) com.sun.star.uno.UnoRuntime.queryInterface(XTextDocument.class, xTextComponent);
// 4. Get the text and insert content
XText xText = xTextDocument.getText();
xText.setString("Hello from Java!\n\nThis document was created programmatically using the UNO API.");
// 5. Save the document
XStorable xStorable = (XStorable) com.sun.star.uno.UnoRuntime.queryInterface(XStorable.class, xTextComponent);
PropertyValue[] storeArgs = new PropertyValue[1];
storeArgs[0] = new PropertyValue();
storeArgs[0].Name = "FilterName";
storeArgs[0].Value = "writer8"; // Use "writer8" for .odt, "MS Word 97" for .doc
xStorable.storeToURL("file:///C:/temp/MyJavaDoc.odt", storeArgs);
// 6. Close the document
xTextDocument.close(true);
System.out.println("Document created and saved successfully!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
How to Run the Code
- Make sure OpenOffice is not running. The code will start its own hidden instance.
- Run the
mainmethod in your Java application. - After the program finishes, check the
C:\tempdirectory (or your specified path) forMyJavaDoc.odt.
Important Concepts and Best Practices
A. The UnoRuntime.queryInterface Pattern
You'll see this pattern everywhere:
XSomeInterface xInterface = (XSomeInterface) UnoRuntime.queryInterface(XSomeInterface.class, someObject);
This is how UNO works. You get a generic Object from a service, and then you "query" it for a specific interface that you know it implements. This is similar to COM in Windows.
B. Starting a Headless (Invisible) OpenOffice Server
For server applications, you don't want a visible OpenOffice window. You can start it as a background service.
-
From the command line: Navigate to the OpenOffice
programdirectory and run:soffice "-accept=socket,host=localhost,port=2002;urp;StarOffice.ServiceManager"
This command starts OpenOffice and makes it listen for Java connections on
localhost:2002. -
Connecting from Java: You then modify your Java code to connect to this running instance instead of starting a new one.
// Replace the bootstrap line with this: XComponentContext xComponentContext = com.sun.star.comp.helper BootstrapSocket.bootstrap("localhost", 2002);
C. Handling Office Lifecycle
When you start OpenOffice from Java, your Java application is responsible for its lifecycle. If your Java application crashes or is killed, the OpenOffice process might remain running as a "zombie". You should always try to shut it down cleanly.
// Add this before your program exits
if (xDesktop != null) {
xDesktop.terminate(); // This closes all documents and quits OpenOffice
}
Modern Alternatives and Considerations
While the UNO API is powerful, it has some drawbacks for new projects:
- Complexity: The API is verbose and requires a deep understanding of UNO concepts.
- Performance: Starting the OpenOffice process can be slow. It's not designed for high-performance, short-lived tasks.
- Dependency: Your application depends on a specific version of OpenOffice being installed on the target machine.
Alternative 1: Apache POI (for Microsoft Office formats)
If your goal is simply to read/write .docx, .xlsx, .pptx files, Apache POI is a much better choice. It's a pure Java library that doesn't require Microsoft Office or OpenOffice to be installed.
<!-- Maven dependency for POI -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
Alternative 2: LibreOffice with a Docker Container
For server-side automation, a modern approach is to use LibreOffice in a Docker container. This provides a clean, isolated, and reproducible environment without polluting your host system. The UNO API works the same way, but you manage the lifecycle with Docker commands.
Summary
| Feature | Java + UNO (OpenOffice) | Apache POI |
|---|---|---|
| Purpose | Full control over OpenOffice (GUI, macros, complex layouts). | Read/write MS Office file formats programmatically. |
| Dependency | Requires OpenOffice to be installed on the machine. | Pure Java library, no external dependencies. |
| Complexity | High (steep learning curve, verbose API). | Low (straightforward API for common tasks). |
| Performance | Slow to start (process creation). | Fast (in-memory operations). |
| Best For | Complex document generation, automating user tasks, integrating with existing OpenOffice macros. | Server-side batch processing of Excel, Word, or PowerPoint files. |
