Libre Office Conection (Java)

Hi , I am trying to develop an LibreOffice integrations with an Java Application and trying to use LibreOffice Java Api. I am struggling with some problem with connection. I used bootsrap connector but it is deprecated. what is the best way of connection. I follow steps in LibreOffice Developer's Guide: Chapter 1 - First Steps - The Document Foundation Wiki. Am i missing something?

This is now a dead link.
I have been working on coverting the Java Library into a python library for the last year in the OOO Development Tools (OooDev).
The OooDev project is also converting the book, Python LibreOffice Programming.

For Python Version you might want to see Chapter 2. Starting and Stopping

@flywire also captured the Java version of the book (thankfully) and it an be found at LibreOffice Programming, again see Starting and stopping LibreOffice (java).

I get the page, a little bit laggy though.
02. Starting and Stopping.pdf (471.3 KB)

I am working on Mac and i could not achieve this. I use bootsrap connector as a workaround solution for devlopment purposes but it causes more problem. I get no executable found error and the solution on stackoverflow did not work for me. any help will be appreciated

@turhan are you using Java or Python?

What does you connection code look like? Can you share a little?

I use Java and get this error. :frowning:

I have not done this in java.

How did you run soffice? If your not starting it in your code then it must be started manually before running your code.
/usr/bin/soffice --accept="socket,host=localhost,port=2002,tcpNoDelay=1;urp;" type of connections string.
See soffice --help for more.

I put together a simple python script.
First I started Office as shown above.
I run the script and it just prints Connected to LibreOffice

import time
import uno
from com.sun.star.connection import NoConnectException

CONN_STR = "uno:socket,host=localhost,port=2002;urp;StarOffice.ServiceManager"
CONN_TIMEOUT = 30.0


def connect():
    end_time = time.time() + CONN_TIMEOUT
    last_ex = None
    while end_time > time.time():
        try:
            local_context = uno.getComponentContext()
            local_factory = local_context.getServiceManager()
            resolver = local_context.getServiceManager().createInstanceWithContext(
                "com.sun.star.bridge.UnoUrlResolver", local_context
            )
            smgr = resolver.resolve(CONN_STR)

            props = smgr.queryInterface(uno.getTypeByName("com.sun.star.beans.XPropertySet"))
            # ctx = props.DefaultContext

            last_ex = None
            print("Connected to LibreOffice")
            break
        except NoConnectException as e:
            last_ex = e
            time.sleep(self._conn_try_sleep)

    if last_ex is not None:
        raise last_ex


if __name__ == "__main__":
    connect()

My connect class for OooDev is here for reference.

Lo.java by Andrew Davison demonstrates how he died it in Java.
See the loadOffice() method.

I start LibreOffice manually. It works behind and i can connect over tcp socket. My problem may be at envoriment variables. I tried a lot of thinks but there is no %100 solution yet

If it helps I just added an alternative connection method to OooDev.
Internally there is the _connect() method and the _connect_alternative() method.

The _connect_alternative() method is not currently being used but I have tested it and it works great a well.
Examine the code to see if it can get you what you need.

Thank you for your interest. This gave me a hope to solve my problem. :slight_smile:
Could you please tell me how to use this solution with Java? Should i run your app than connect LibreOffice over your application?

As I have stated, I have not done this in Java. For Java take a look at Starting and Stopping (Java).

If you want to do this in python then Yes, I would recommend using OooDev and then I can offer some assistance. If you are not already tied to Java for you project then I would recommend using python.

Class-path modification has solved my problem. I hope this can help anyone. It should be considered that this solution is valid for only Mac environment