2015-12-15 32 views
1

我正在使用Java創建和編輯Open Office文檔。但是,在修改文檔之後,Java不會退出並仍然運行多個線程,例如Thread [Pipe:CO>],Thread [Pipe:CE>]和Thread [MessageDispatcher]。我希望能夠關閉這些線程,並允許在代碼完成時關閉Java。處理Open Office文檔後如何停止Java

這裏是我的打開和關閉文檔代碼:

private static Object oDesktop; 
private static XStorable xStorable; 
private static XComponent xSpreadsheetComponent; 
private static XComponentContext xContext; 
private static XSpreadsheetDocument xSpreadsheetDocument; 
private static XSpreadsheets xSpreadsheets; 

    public static void openDocument(String filename) 
    { 
    try 
    {   
     // Get the remote office component context 
     xContext = Bootstrap.bootstrap(); 

     // Get the remote office service manager 
     XMultiComponentFactory xMCF = xContext.getServiceManager(); 

     // Get the root frame (i.e. desktop) of openoffice framework. 
     oDesktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xContext); 

     // Desktop has 3 interfaces. The XComponentLoader interface provides ability to load components. 
     XComponentLoader xCompLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, 
       oDesktop); 

     PropertyValue[] loadProps = new PropertyValue[0]; 

     xSpreadsheetComponent = xCompLoader.loadComponentFromURL(getUpdatedPath(filename), "_blank", 0, loadProps); 

     xStorable = (XStorable) UnoRuntime.queryInterface(XStorable.class, xSpreadsheetComponent); 

     xSpreadsheetDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, 
       xSpreadsheetComponent); 

     xSpreadsheets = xSpreadsheetDocument.getSheets(); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 

    public static void closeDocument() 
{ 
    try 
    { 
     XCloseable xCloseable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, xStorable); 

     if (xCloseable != null) 
     { 
      xCloseable.close(false); 
     } 
     else 
     { 
      XComponent xComp = (XComponent) UnoRuntime.queryInterface(XComponent.class, xStorable); 

      xComp.dispose(); 
     } 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 

提前感謝!

+0

它是一個獨立的程序嗎? – Rao

+0

我使用Eclipse來運行代碼,如果這是你問的:)。 –

+1

我的意思是,它有一個主要方法嗎? – Rao

回答

1

正如我在評論中指出的,在https://wiki.openoffice.org/wiki/API/Samples/Java的BookmarkInsertion示例中,代碼以System.exit(0);結束。

另外,是否有一個原因,爲什麼所有的變量需要是靜態的?看起來好像不會讓它們被正確銷燬。