首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过Java关闭MS Office应用程序中打开的文件?

如何通过Java关闭MS Office应用程序中打开的文件?
EN

Stack Overflow用户
提问于 2016-11-11 09:51:11
回答 1查看 81关注 0票数 0

例如,我在Windows10上的微软PowerPoint 2013中打开了test.pptx,我想在不关闭微软PowerPoint本身的情况下关闭它。我如何使用Java 1.8来做到这一点?

EN

回答 1

Stack Overflow用户

发布于 2016-11-11 11:12:13

因为它是外部程序,所以您需要对运行power point taskPC执行kill操作。

您可以使用以下Java代码来实现:

代码语言:javascript
复制
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ClosePowerPoint {

    private static final String TASKLIST = "tasklist";
    private static final String KILL = "taskkill /IM ";

    public static void main(String args[]) throws Exception {    
        System.out.print(isProcessRunging("POWERPNT.EXE"));

        if (isProcessRunging(processName)) {

            killProcess(processName);
        }
    }

    public static boolean isProcessRunging(String serviceName) throws Exception {

        Process p = Runtime.getRuntime().exec(TASKLIST);
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;

        while ((line = reader.readLine()) != null) {

            System.out.println(line);
            if (line.contains(serviceName)) {
                return true;
            }
        }
        return false;
    }

    public static void killProcess(String serviceName) throws Exception {

        Runtime.getRuntime().exec(KILL + serviceName);

    }   
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40539982

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档