[Java example] Using the Bootstrap Connection Mechanism

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
hol.sten
Volunteer
Posts: 495
Joined: Mon Oct 08, 2007 1:31 am
Location: Hamburg, Germany

[Java example] Using the Bootstrap Connection Mechanism

Post by hol.sten »

This post contains a working example of the Bootstrap Connection Mechanism and how you get this working in NetBeans 5.5.1:
- Start NetBeans IDE
- Create a new project by calling "File" > "New Project..."
- Right-click with the mouse on the brand new project name and select "Properties" from the context menu
- Select in the Project Properties from Categories the categorie "Libraries" and there the tab "Compile"
- Press the button "Add JAR/Folder" and locate some of OOo's JAR files from OOo's installation directory which is on Windows for example "c:\program files\openoffice.org 2.3\program\classes". Add at least juh.jar, jurt.jar, ridl.jar and unoil.jar. Adding these JAR files exactly here in your Project Properties is very important!
- Create a new class containing the following code:

Code: Select all

package oootest;

import com.sun.star.beans.PropertyValue;
import com.sun.star.bridge.XUnoUrlResolver;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XStorable;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

public class BootstrapConnectionOdtToPdfQuickAndDirty {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String loadUrl="file:///c:/dev/netbeans/oootest/mydoc.odt";
        String storeUrl="file:///c:/dev/netbeans/oootest/mydoc.pdf";

        try {
            XComponentContext xContext = Bootstrap.bootstrap();
            XMultiComponentFactory xMultiComponentFactory = xContext.getServiceManager();
            XComponentLoader xcomponentloader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class,xMultiComponentFactory.createInstanceWithContext("com.sun.star.frame.Desktop", xContext));

            Object objectDocumentToStore = xcomponentloader.loadComponentFromURL(loadUrl, "_blank", 0, new PropertyValue[0]);

            PropertyValue[] conversionProperties = new PropertyValue[1];
            conversionProperties[0] = new PropertyValue();
            conversionProperties[0].Name = "FilterName";
            conversionProperties[0].Value = "writer_pdf_Export";

            XStorable xstorable = (XStorable) UnoRuntime.queryInterface(XStorable.class,objectDocumentToStore);
            xstorable.storeToURL(storeUrl,conversionProperties);
        }
        catch (java.lang.Exception e) {
            e.printStackTrace();
        }
        finally {
            System.exit(0);
        }
    }   
}
Save the class and let it run by calling "Run File" for example.

Besides establishing the connection to OOo the only useful task this code performs is converting a document from ODT to PDF.

If you want to convert the document into a different format, you have to change the FilterName of the conversionProperties. For example to convert a document to DOC change

Code: Select all

conversionProperties[0].Value = "writer_pdf_Export";
to

Code: Select all

conversionProperties[0].Value = "MS WinWord 6.0";
Or try the filter list, if you need another conversion: http://wiki.services.openoffice.org/wik ... st_OOo_2_1
OOo 3.2.0 on Ubuntu 10.04 • OOo 3.2.1 on Windows 7 64-bit and MS Windows XP
hol.sten
Volunteer
Posts: 495
Joined: Mon Oct 08, 2007 1:31 am
Location: Hamburg, Germany

Change log

Post by hol.sten »

19. Dec 2007: Creation of the Java example
OOo 3.2.0 on Ubuntu 10.04 • OOo 3.2.1 on Windows 7 64-bit and MS Windows XP
ptsenter
Posts: 22
Joined: Thu Feb 21, 2008 9:02 pm

Re: [Java example] Using the Bootstrap Connection Mechanism

Post by ptsenter »

Hi,

I have integrated OpenOffcie into my Java application a while ago, at least 3 months. I use NetBeans, ver 5.x and, now 6.0.1. So, I have some experience running and debugging my app from within NetBeans on Windows platform. I, actually, defined a library with components you mentioned and added this library to my project.
I even can transfer my app to UNIX (Solaris) and run it there successfully.

But when I run the app within NetBeans on UNIX it fails on

XComponentContext xContext = Bootstrap.bootstrap();

I even set OFFICE_HOME per Developer's Guide.
To simplify I tried an example from Dev Guide FirstUnoContact. In both cases I get

Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: com.sun.star.lib.connections.pipe.PipeConnection.createJNI(Ljava/lang/String;)I

In addition catch statement does not catch this exception, program crashes.

Could you direct me where to start looking?

And I'm not very proficient in UNIX.

Thank you.
hol.sten
Volunteer
Posts: 495
Joined: Mon Oct 08, 2007 1:31 am
Location: Hamburg, Germany

Re: [Java example] Using the Bootstrap Connection Mechanism

Post by hol.sten »

ptsenter wrote:But when I run the app within NetBeans on UNIX it fails on
XComponentContext xContext = Bootstrap.bootstrap();
Try this thread, although your problem is not "no office executable found!": http://user.services.openoffice.org/en/ ... =44&t=2520
ptsenter wrote:Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: com.sun.star.lib.connections.pipe.PipeConnection.createJNI(Ljava/lang/String;)I
"Bootstrap.bootstrap()" uses a named pipe connection. So use the "BootstrapSocketConnector" I recommended above.
ptsenter wrote:In addition catch statement does not catch this exception, program crashes.
Yes, this happens on Windows, too, if "sal3.dll" is not available from juh.jar. Instead of switching to the "BootstrapSocketConnector" you may solve your problem by providing the sal3 lib on UNIX. Read this http://www.oooforum.org/forum/viewtopic ... light=sal3 on how to make sal3 available:
OOo Developer's Guide - Chapter OfficeBean wrote:The library sal3 (Windows: sal3.dll, Unix: libsal3.so) is located in the <OfficePath>/program folder. It maybe necessary to add the <OfficePath>/program folder to the PATH environment variable if the bean cannot find sal3.
And read this about problems with a named pipe on Linux: http://www.oooforum.org/forum/viewtopic ... light=sal3. The crucial part there is:
mnasato wrote:Caveat: this uses a native library, sal3, that you need to make available in the java.library.path when starting the Java app, e.g.
OOo 3.2.0 on Ubuntu 10.04 • OOo 3.2.1 on Windows 7 64-bit and MS Windows XP
ptsenter
Posts: 22
Joined: Thu Feb 21, 2008 9:02 pm

Re: [Java example] Using the Bootstrap Connection Mechanism

Post by ptsenter »

Thanks for quick response.
I already tried BootstrapSocketConnector to no avail.
But let me work on sal3.
hol.sten
Volunteer
Posts: 495
Joined: Mon Oct 08, 2007 1:31 am
Location: Hamburg, Germany

Re: [Java example] Using the Bootstrap Connection Mechanism

Post by hol.sten »

ptsenter wrote:I already tried BootstrapSocketConnector to no avail.
But you got at least another error? Which one?
OOo 3.2.0 on Ubuntu 10.04 • OOo 3.2.1 on Windows 7 64-bit and MS Windows XP
ptsenter
Posts: 22
Joined: Thu Feb 21, 2008 9:02 pm

Re: [Java example] Using the Bootstrap Connection Mechanism

Post by ptsenter »

Yes, I got


com.sun.star.comp.helper.BootstrapException
at ooo.connector.BootstrapConnector.connect(BootstrapConnector.java:129)
at ooo.connector.BootstrapSocketConnector.connect(BootstrapSocketConnector.java:68)
at ooo.connector.BootstrapSocketConnector.connect(BootstrapSocketConnector.java:45)
at ooo.connector.BootstrapSocketConnector.bootstrap(BootstrapSocketConnector.java:82)
at firstunocontact.Main.main(Main.java:26)
ptsenter
Posts: 22
Joined: Thu Feb 21, 2008 9:02 pm

Re: [Java example] Using the Bootstrap Connection Mechanism

Post by ptsenter »

BootstrapSocketConnector works!

When I tried it first time I forgot to include "/program" in path - string variable.

There is no libsal3.so, but there is libsal.so and libsal.so.3. But it did not work anyway, even with "/program" included.

Thank you very much for your help,
Peter.
abhishah4444
Posts: 1
Joined: Tue Mar 01, 2011 5:14 pm

Re: [Java example] Using the Bootstrap Connection Mechanism

Post by abhishah4444 »

Hi OO people,

I finally figured out solution to this problem

Both below errors can be solved without any code change.

2 Steps I had to do make DocumentConversion.java to work on eclipse environment

1) // Storing and converting the document
xStorable.storeAsURL(sStoreUrl, propertyValues); was changed
// Storing and converting the document
xStorable.storeToURL(sStoreUrl, propertyValues);
2) I know your code works file after adding below 4 jar and adding C:\Program Files\OpenOffice.org 3\program to Eclipse path.jars -(unoil.jar;ridl.jar;jurt.jar;juh.jar)

BUT when you run from command line or in tomcat or in linux , we get these errors open office java.lang.UnsatisfiedLinkError: createJNI or "no office executable found!"

Solution
Add below 2 directories in PATH variable or on linux LD_LIBRARY_PATH
1) C:\Program Files\OpenOffice.org 3\program
2) C:\Program Files\OpenOffice.org 3\URE\bin

Everyone suggest only first one, Add second and it will save 1 day which I jsut spent to figure it out

I use C:\Workspace\eclipseHelios\WorkflowMethod\bin>java -cp "unoil.jar;ridl.jar;jurt.jar;juh.jar;C:\Program Files\OpenOffice.org 3\program" WF_DocumentConversion "c:\\test" "writer_pdf_Export" "pdf" "c:\\test"

Thanks
Abhishek Shah
OpenOffice 3.3 on Windows XP
Post Reply