解決思路,既然上一個libreoffice沒關閉,導致8100端口被占用,那我們就去關閉它。
在OfficeManager.start();方法之前,使用Linux進程操作工具類,找到之前的libreoffice進程,然后殺掉進程。
Linux進程操作工具類請看https://www.cnblogs.com/RivenLw/p/10477488.html
/** * 連接LibreOffice.org 並且啟動LibreOffice.org * * @return * @throws Exception */ public static OfficeManager getOfficeManager() throws Exception { String osName = System.getProperty("os.name"); if (Pattern.matches("Linux.*", osName)) {//如果是Linux系統則嘗試先結束上次啟動的Libreoffice String pid = LinuxProcessUtil.getPID("libreoffice"); logger.info("找到pid="+pid); if (StringUtils.isNotEmpty(pid)) { LinuxProcessUtil.closeLinuxProcess(pid); } } DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration(); // 設置LibreOffice.org 3的安裝目錄 config.setOfficeHome(getOfficeHome()); // 端口號 config.setPortNumber(8100); config.setTaskExecutionTimeout(1000 * 60 * 25L); // 設置任務執行超時為10分鍾 config.setTaskQueueTimeout(1000 * 60 * 60 * 24L); // 設置任務隊列超時為24小時 // 啟動LibreOffice的服務 OfficeManager getOfficeManager = config.buildOfficeManager(); getOfficeManager.start(); return getOfficeManager; }