java代码实现开启openoffice服务和关闭sffice.exe进程

最近这段时间,在研究lucene实现网页预览文档java

使用Openoffic+Swftools+flexpaper实现预览windows

首先要开启openoffice服务 ,每次都要手动启动,非常麻烦,因此研究了一下windows命令,启动openoffice服务。less

思想:将启动openoffice服务的命令封装在.bat文件中,使用java代码调用该.bat文件,实现启动openoffice服务。socket

package ytu.botao.util;
import java.io.File;
import java.io.FileWriter;
/**
 * 功能:开启openoffice服务
 * 使用方法:直接生成该类对象
 * *****因为本机openoffice安装路径不一样,须要更改openoffice的安装路径
 * @author botao
 *
 */
public class JavaCallOpenoffice {
    /**
     * 将启动程序定义在构造函数中,直接生成该类对象,便可启动openoffice服务
     */
    public JavaCallOpenoffice() {
        Runtime rn = Runtime.getRuntime();
          Process p = null;
          try {
              File file=new File("d:\\openoprenoffice.bat");
              if (false==file.exists()) {
                  System.out.println("。。。。。。。。。。");
                  FileWriter writer = new FileWriter("d:\\openoprenoffice.bat ");
                    writer.write("@echo   off ");
                    writer.write("\r\n ");
                    writer.write("D:");
                    writer.write("\r\n ");
                    //D:\\Program Files\\OpenOffice 4\\program: openoffice的安装路径路径
                    writer.write("cd D:\\Program Files\\OpenOffice 4\\program");
                    writer.write("\r\n ");
                    writer.write("soffice -headless -accept="+"socket,host=127.0.0.1,port=8100;urp;"+" -nofirststartwizard");
                    writer.write("\r\n ");
                    writer.write("@echo   on ");
                    writer.close();
            }
                p = rn.exec("cmd.exe /C d:\\openoprenoffice.bat");
            }
            catch (Exception e1) {
                   e1.printStackTrace();
            }
    }
}

每次启动openoffice服务,soffice进程都占用100M内存,非常浪费,因此在文档转换完以后,要关闭soffice进程ide

package ytu.botao.util;
import java.io.IOException;
import java.util.Scanner;
/**
 * 功能:关闭openoffice服务
 * 使用方法:直接生成该类对象便可
 * @author botao
 *
 */
public class DistorySoffice {
    private Process process=null;
    /**
     * 构造方法,实现关闭soffice进程
     */
    public DistorySoffice() {
        try {
            //显示进程
            process=Runtime.getRuntime().exec("tasklist");
            Scanner in=new Scanner(process.getInputStream());
            while (in.hasNextLine()) {
                String processString=in.nextLine();
                if (processString.contains("soffice.exe")) {
                    //关闭soffice进程的命令
                    String cmd="taskkill /f /im soffice.exe";
                    process=Runtime.getRuntime().exec(cmd);
                    System.out.println("openoffice正常关闭.......");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
相关文章
相关标签/搜索