# Java-Applikation und OpenOffice/UNO



## MrGe (20. Mrz 2011)

Hallo,

ich möchte eine Java-Application schreiben, die odt-Dateien schreibt.
Es sollen mathematische Formeln (so mit Wuzelzeichn und Brüchen) in eine Texttabelle geschrieben werden.

Ich möchte das nur mit Java und OpenOffice/UNO machen.

Das haut aber noch nicht hin.
Ich habe ein Programm geschrieben, dass ich zwar kompilieren, aber nicht ausführen kann.


```
Exception in thread "main" java.lang.UnsatisfiedLinkError: com.sun.star.lib.connections.pipe.PipeConnection.createJNI(Ljava/lang/String;)I
	at com.sun.star.lib.connections.pipe.PipeConnection.createJNI(Native Method)
	at com.sun.star.lib.connections.pipe.PipeConnection.<init>(PipeConnection.java:128)
	at com.sun.star.lib.connections.pipe.pipeConnector.connect(pipeConnector.java:137)
	at com.sun.star.comp.connections.Connector.connect(Connector.java:141)
	at com.sun.star.comp.urlresolver.UrlResolver$_UrlResolver.resolve(UrlResolver.java:123)
	at com.sun.star.comp.helper.Bootstrap.bootstrap(Bootstrap.java:285)
	at TestOOAPI.<init>(TestOOAPI.java:20)
	at TestOOAPI.main(TestOOAPI.java:15)
```
Leider habe ich noch keine Lösung gefunden.

Kann mir jemand einen Tipp geben?
Kennt jemand eine Schritt für Schritt-Anleitung?

Vielen Dank und liebe Grüße!!



```
################################################################################################
TestOOAPI.java
################################################################################################
//In Anlehnung an: [url=http://openofficejava.blogspot.com/2009/05/openofficeorg-api.html]OpenOffice API Programming in Java: Openoffice.org UNO API[/url]
import com.sun.star.beans.PropertyValue;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.comp.helper.BootstrapException;
import com.sun.star.frame.XComponentLoader;
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.UnoRuntime;
import com.sun.star.uno.XComponentContext;
public class TestOOAPI {
	public static void main(String[] args) {
		new TestOOAPI();
	}
	public TestOOAPI(){
		try {
			// Get the remote office component context
			XComponentContext xContext = Bootstrap.bootstrap();
			// Get the remote office service manager
			XMultiComponentFactory xMCF = xContext.getServiceManager();
			try{
				// Get the root frame (i.e. desktop) of openoffice framework.
				Object oDesktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xContext);
				// Desktop has 3 interfaces. The XComponentLoader interface provides ability to load components.
				XComponentLoader xCLoader = 
					( XComponentLoader ) UnoRuntime.queryInterface(XComponentLoader.class, oDesktop);
				
				
				// Create a document
				XComponent document = 
					xCLoader.loadComponentFromURL("private:factory/swriter", "_blank", 0, new PropertyValue[0]);

				// Get the textdocument
				XTextDocument aTextDocument = 
					( XTextDocument )UnoRuntime.queryInterface(com.sun.star.text.XTextDocument.class, document);

				// Get its text
				XText xText = aTextDocument.getText();
				
				// Adding text to document
				xText.insertString(xText.getEnd(), "My First OpenOffice Document", false);

			}catch(Exception ex){ex.printStackTrace();}
		} catch (BootstrapException e) {e.printStackTrace();}
	}
}
```


```
################################################################################################
compile-run.sh
################################################################################################
#!/bin/sh
JAVAC=javac
JAVA=java

export CLASSPATH=$CLASSPATH:.:/usr/share/java/openoffice/juh.jar:/usr/share/java/openoffice/jurt.jar:/usr/share/java/openoffice/ridl.jar:/usr/share/java/openoffice/unoil.jar:/usr/lib/openoffice/program/soffice

if [ $1 -eq 0 ]; then
	echo "compile"
	$JAVAC TestOOAPI.java
elif [ $1 -eq 1 ]; then
	echo "run"
	#test 1: ----------------------------------------------------------------------
		export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/openoffice/program/
		$JAVA TestOOAPI
	#test 2: ----------------------------------------------------------------------	
	#$JAVA -Djava.library.path=/usr/lib/openoffice/program/ TestOOAPI
else
	echo ""
	echo "give me a 0 or 1 please"
fi
```
################################################################################################
diverse pfade
################################################################################################
ls /usr/share/java/openoffice/
agenda.jar com commonwizards.jar fax.jar form.jar java_uno.jar JREProperties.class js.jar juh.jar juh_src.zip jurt.jar jurt_src.zip letter.jar lucene-analyzers-2.3.jar lucene-core-2.3.jar LuceneHelpWrapper.jar query.jar report.jar ridl.jar ridl_src.zip sandbox.jar saxon9.jar ScriptFramework.jar ScriptProviderForBeanShell.jar ScriptProviderForJava.jar ScriptProviderForJavaScript.jar table.jar unoil.jar unoloader.jar unoloader_src.zip web.jar win XMergeBridge.jar xmerge.jar XSLTFilter.jar XSLTValidate.jar

which soffice
/usr/bin/soffice

ls -l /usr/bin/soffice
lrwxrwxrwx 1 root root 33 2011-02-03 14:17 /usr/bin/soffice -> ../lib/openoffice/program/soffice


----------



## I-Doetzken (14. Jul 2011)

so gehts:


```
import com.sun.star.beans.PropertyValue;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XDesktop;
import com.sun.star.io.IOException;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.text.XText;
import com.sun.star.text.XTextCursor;
import com.sun.star.text.XTextDocument;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

public class FirstUnoContact {
	public static void main(String[] args) {
		new FirstUnoContact();
	}

	public FirstUnoContact(){
		XDesktop xDesktop = null;
		xDesktop = getDesktop();
		
		XComponent xComponent = null;
		XTextDocument xTextDocument = null;
		
		XComponentLoader xComponentLoader = 
			(XComponentLoader)UnoRuntime.queryInterface(
					XComponentLoader.class, xDesktop
		);
		try {
			xComponent = xComponentLoader.loadComponentFromURL(
				"private:factory/swriter", "_blank", 0, new PropertyValue[0]
			);
			xTextDocument = (XTextDocument)UnoRuntime.queryInterface(
				XTextDocument.class, xComponent
			);
			XText xText = xTextDocument.getText();
			XTextCursor xTextCursor = xText.createTextCursor();
			xText.insertString(xTextCursor, "Hello World!", false);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}		
	}

	public static XDesktop getDesktop() {
		XDesktop xDesktop = null;
		XMultiComponentFactory xMCF = null;
		
		try {
			XComponentContext xContext = null;
			xContext = Bootstrap.bootstrap();
			xMCF = xContext.getServiceManager();
			if( xMCF != null ) {
				System.out.println("Connected to a running office ...");
				Object oDesktop = xMCF.createInstanceWithContext(
						"com.sun.star.frame.Desktop", xContext
				);
				xDesktop = (XDesktop) UnoRuntime.queryInterface(
						XDesktop.class, oDesktop
				);
			}else{
				System.out.println(
						"Can't create a desktop." +
						"No connection, no remote office servicemanager available!"
				);
			}
		}catch( Exception e) {
			e.printStackTrace(System.err);
			System.exit(1);
		}
		return xDesktop;
	}
	
	public static XTextDocument createTextdocument(XDesktop xDesktop ){
		
		XTextDocument aTextDocument = null;
		
		try {
			XComponent xComponent = CreateNewDocument(xDesktop, "swriter");
			aTextDocument = (XTextDocument)UnoRuntime.queryInterface(
					XTextDocument.class, xComponent
			);
		}catch( Exception e) {
			e.printStackTrace(System.err);
		}
		return aTextDocument;
	}
	
	protected static XComponent CreateNewDocument(XDesktop xDesktop,String sDocumentType ){

		String sURL = "private:factory/" + sDocumentType;
		
		XComponent xComponent = null;
		XComponentLoader xComponentLoader = null;
		PropertyValue xEmptyArgs[] = new PropertyValue[0];
		
		try {
			xComponentLoader = (XComponentLoader)UnoRuntime.queryInterface(
					XComponentLoader.class, xDesktop
			);
			xComponent = xComponentLoader.loadComponentFromURL(
			sURL, "_blank", 0, xEmptyArgs);
		}catch( Exception e) {
			e.printStackTrace(System.err);
		}
		return xComponent ;
	}
	
}
```

startscript:


```
#!/bin/sh

UNO=/opt/libreoffice3.4/ure/share/java/
UNOIL=/opt/libreoffice3.4/basis3.4/program/classes/
LO=/opt/libreoffice3.4/program/

CLASSPATH=$CLASSPATH:.:"$UNO"java_uno.jar:"$UNO"juh.jar:"$UNO"jurt.jar:"$UNO"ridl.jar:"$UNO"unoloader.jar:"$UNOIL"unoil.jar:"$LO"soffice

#javac -cp "$CLASSPATH" FirstUnoContact.java
java -cp "$CLASSPATH" FirstUnoContact
```


----------



## I-Doetzken (15. Jul 2011)

der vollständigkeit halber:

First Contact - OpenOffice.org Wiki

Getting started with the OpenOffice.org API part I : connecting to OpenOffice.org  AMIS Technology blog

beste grüße


----------

