在 java中 ,最近使用了openoffice,如果是windows下 ,可以调用bat去启动服务 ,或者注册表的方式让服务器重新启动后就启动openoffice,记得要关闭进程,代码如下:
引用 http://botao900422.blog.51cto.com/4747129/1258901
引用 http://botao900422.blog.51cto.com/4747129/1258901
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();
}
}
}
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();
}
}
}