/**
* 启动应用程序
*
* @param programName
* @return
* @throws IOException
*/
public static void startProgram(String programPath) throws IOException {
log.info("启动应用程序:" + programPath);
if (StringUtils.isNotBlank(programPath)) {
try {
String programName = programPath.substring(programPath.lastIndexOf("/") + 1, programPath.lastIndexOf("."));
List<String> list = new ArrayList<String>();
list.add("cmd.exe");
list.add("/c");
list.add("start");
list.add("\"" + programName + "\"");
list.add("\"" + programPath + "\"");
ProcessBuilder pBuilder = new ProcessBuilder(list);
pBuilder.start();
// Desktop.getDesktop().open(new File(programPath));
} catch (Exception e) {
e.printStackTrace();
log.error("应用程序:" + programPath + "不存在!");
}
}
}
本文介绍了一种使用Java程序启动外部应用程序的方法。通过构造ProcessBuilder并指定cmd.exe作为参数,可以实现跨平台的应用启动。该方法适用于Windows环境,并包含了错误处理机制。
1万+

被折叠的 条评论
为什么被折叠?



