package org.liufei.jweb.util;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* 桌面启动器工兄1�7
*
* @author 刘飞
*
*/
public class DesktopUtil {
private static final Logger log = Logger.getLogger(DesktopUtil.class
.getName());
private static final String OS_MACOS = "Mac OS";
private static final String OS_WINDOWS = "Windows";
private static final String[] UNIX_BROWSE_CMDS = { "www-browser",
"firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape",
"w3m", "lynx" };
private static final String[] UNIX_OPEN_CMDS = { "run-mailcap", "pager",
"less", "more" };
private DesktopUtil() {
}
/**
* 使用系统默认浏览器启动指定URL指向的地坄1�7〄1�7
*
* @param url
* the URL to open
* @throws IOException
* if a browser couldn't be found or if the URL failed to launch
*/
public static void browse(final URL url) throws IOException {
// Try Java 1.6 Desktop class if supported
if (browseDesktop(url))
return;
final String osName = System.getProperty("os.name");
log.finer("Launching " + url + " for OS " + osName);
if (osName.startsWith(OS_MACOS)) {
browseMac(url);
} else if (osName.startsWith(OS_WINDOWS)) {
browseWindows(url);
} else {
// assume Unix or Linux
browseUnix(url);
}
}
/**
* 使用系统默认浏览器启动指定URL指向的地坄1�7, 错误时给出提示对话框〄1�7
*
* @param url
* the URL to open
* @param parentComponent
* The parent Component over which the error dialog should be
* shown
*/
public static void browseAndWarn(final URL url,
final Component parentComponent) {
try {
browse(url);
} catch (final IOException e) {
log.log(Level.SEVERE, "Unable to browse to " + url, e);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(parentComponent,
"Couldn't open a web browser:\n"
+ e.getLocalizedMessage(),
"Unable to launch web browser",
JOptionPane.ERROR_MESSAGE);
}
});
}
}
/**
* 使用系统默认浏览器启动指定URL指向的地坄1�7, 错误时给出提示对话框〄1�7
*
* @param url
* the URL to open
* @param parentComponent
* The parent Component over which the error dialog should be
* shown
*/
public static void browseAndWarn(final String url,
final Component parentComponent) {
try {
browse(new URL(url));
} catch (final IOException e) {
log.log(Level.SEVERE, "Unable to browse to " + url, e);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(parentComponent,
"Couldn't open a web browser:\n"
+ e.getLocalizedMessage(),
"Unable to launch web browser",
JOptionPane.ERROR_MESSAGE);
}
});
}
}
/**
* 使用系统默认程序启动指定文件〄1�7
*
* @param file
* the File to open
* @throws IOException
* if an application couldn't be found or if the File failed to
* launch
*/
public static void open(final File file) throws IOException {
// Try Java 1.6 Desktop class if supported
if (openDesktop(file))
return;
final String osName = System.getProperty("os.name");
log.finer("Opening " + file + " for OS " + osName);
if (osName.startsWith(OS_MACOS)) {
openMac(file);
} else if (osName.startsWith(OS_WINDOWS)) {
openWindows(file);
} else {
// assume Unix or Linux
openUnix(file);
}
}
/**
* 打开指定文件, 失败时在parentComponent指定的AWT组件前面弹出消息对话框�1�7�1�7
*
* @param file
* the File to open
* @param parentComponent
* The parent Component over which the error dialog should be
* shown
*/
public static void openAndWarn(final File file,
final Component parentComponent) {
try {
open(file);
} catch (final IOException e) {
log.log(Level.SEVERE, "Unable to open " + file, e);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(parentComponent, "不能打开文件 "
+ file + ":\n" + e.getLocalizedMessage(), "不能打开文件",
JOptionPane.ERROR_MESSAGE);
}
});
}
}
/**
* 不直接调用java.awt.Desktop籄1�7,
* 而是使用反射去加载java.awt.Desktop类,这样当jre版本小于1.6时就不会执行, true表示执行成功,
* false表示执行不成劄1�7, 即jre版本小于1.6
*
* @param url
* the URL to launch
* @return true if launch successful, false if we should fall back to other
* methods
* @throws IOException
* if Desktop was found, but the browse() call failed.
*/
private static boolean browseDesktop(final URL url) throws IOException {
final Class<?> desktopClass = getDesktopClass();
if (desktopClass == null)
return false;
final Object desktopInstance = getDesktopInstance(desktopClass);
if (desktopInstance == null)
return false;
log.finer("Launching " + url + " using Desktop.browse()");
try {
final Method browseMethod = desktopClass.getDeclaredMethod(
"browse", URI.class);
browseMethod.invoke(desktopInstance, new URI(url.toExternalForm()));
return true;
} catch (InvocationTargetException e) {
if (e.getCause() instanceof IOException) {
throw (IOException) e.getCause();
} else {
log.log(Level.FINE, "Exception in Desktop operation", e);
return false;
}
} catch (Exception e) {
log.log(Level.FINE, "Exception in Desktop operation", e);
return false;
}
}
/**
* 在windows操作系统下使用url.dll打开指定URL地址〄1�7
*
* @param url
* the URL to launch
* @throws IOException
* if the launch failed
*/
private static void browseWindows(final URL url) throws IOException {
log.finer("Windows invoking rundll32");
Runtime.getRuntime().exec(
new String[] { "rundll32", "url.dll,FileProtocolHandler",
url.toString() });
}
/**
* 在Unix系统下打弄1�7指定URL地址〄1�7
*
* @param url
* the URL to launch
* @throws IOException
* if the launch failed
*/
private static void browseUnix(final URL url) throws IOException {
for (final String cmd : UNIX_BROWSE_CMDS) {
log.finest("Unix looking for " + cmd);
if (unixCommandExists(cmd)) {
log.finer("Unix found " + cmd);
Runtime.getRuntime().exec(new String[] { cmd, url.toString() });
return;
}
}
throw new IOException("Could not find a suitable web browser");
}
/**
*
* 通过反射使用com.apple.eio.FileManager籄1�7
*
* @param url
* the URL to launch
* @throws IOException
* if the launch failed
*/
private static void browseMac(final URL url) throws IOException {
try {
final Class<?> fileMgr = getAppleFileManagerClass();
final Method openURL = fileMgr.getDeclaredMethod("openURL",
String.class);
log.finer("Mac invoking");
openURL.invoke(null, url.toString());
} catch (Exception e) {
log.log(Level.WARNING, "Couldn't launch Mac URL", e);
throw new IOException("Could not launch Mac URL: "
+ e.getLocalizedMessage());
}
}
/**
* 不直接调用java.awt.Desktop籄1�7,
* 而是使用反射去加载java.awt.Desktop类,这样当jre版本小于1.6时就不会执行, true表示执行成功,
* false表示执行不成劄1�7, 即jre版本小于1.6
*
* @param file
* the File to open
* @return true if open successful, false if we should fall back to other
* methods
* @throws IOException
* if Desktop was found, but the open() call failed.
*/
private static boolean openDesktop(final File file) throws IOException {
final Class<?> desktopClass = getDesktopClass();
if (desktopClass == null)
return false;
final Object desktopInstance = getDesktopInstance(desktopClass);
if (desktopInstance == null)
return false;
log.finer("Opening " + file + " using Desktop.open()");
try {
final Method browseMethod = desktopClass.getDeclaredMethod("open",
File.class);
browseMethod.invoke(desktopInstance, file);
return true;
} catch (InvocationTargetException e) {
if (e.getCause() instanceof IOException) {
throw (IOException) e.getCause();
} else if (e.getCause() instanceof IllegalArgumentException) {
throw new FileNotFoundException(e.getCause()
.getLocalizedMessage());
} else {
log.log(Level.FINE, "Exception in Desktop operation", e);
return false;
}
} catch (Exception e) {
log.log(Level.FINE, "Exception in Desktop operation", e);
return false;
}
}
/**
* 在windows操作系统下使用shell32.dll打开指定文件〄1�7
*
* @param file
* the File to open
* @throws IOException
* if the open failed
*/
private static void openWindows(final File file) throws IOException {
log.finer("Windows invoking rundll32");
Runtime.getRuntime().exec(
new String[] { "rundll32", "shell32.dll,ShellExec_RunDLL",
file.getAbsolutePath() });
}
/**
* 通过反射使用com.apple.eio.FileManager籄1�7
*
* @param file
* the File to open
* @throws IOException
* if the open failed
*/
@SuppressWarnings("deprecation")
private static void openMac(final File file) throws IOException {
// we use openURL() on the file's URL form since openURL supports
// file:// protocol
browseMac(file.getAbsoluteFile().toURL());
}
/**
* 在Unix系统下打弄1�7指定文件〄1�7
*
* @param file
* the File to open
* @throws IOException
* if the open failed
*/
private static void openUnix(final File file) throws IOException {
for (final String cmd : UNIX_OPEN_CMDS) {
log.finest("Unix looking for " + cmd);
if (unixCommandExists(cmd)) {
log.finer("Unix found " + cmd);
Runtime.getRuntime().exec(
new String[] { cmd, file.getAbsolutePath() });
return;
}
}
throw new IOException("Could not find a suitable viewer");
}
/**
* 判断当前系统的JRE是否有java.awt.Desktop类库, 即是否是jre1.6或其以上版本〄1�7
*
* @return the Desktop class object, or null if it could not be found
*/
private static Class<?> getDesktopClass() {
// NB The following String is intentionally not inlined to prevent
// ProGuard trying to locate the unknown class.
final String desktopClassName = "java.awt.Desktop";
try {
return Class.forName(desktopClassName);
} catch (ClassNotFoundException e) {
log.fine("Desktop class not found");
return null;
}
}
/**
*
* 获取java.awt.Desktop类的实例,如果支持�1�7�我们检查isDesktopSupported()
* 但为了方便,我们也懒得去棄1�7查isSupported(方法),�1�7�是
* 调用者处理任何UnsupportedOperationExceptions
*
* @param desktopClass
* the Desktop Class object
* @return the Desktop instance, or null if it is not supported
*/
private static Object getDesktopInstance(final Class<?> desktopClass) {
try {
final Method isDesktopSupportedMethod = desktopClass
.getDeclaredMethod("isDesktopSupported");
log.finest("invoking isDesktopSupported");
final boolean isDesktopSupported = (Boolean) isDesktopSupportedMethod
.invoke(null);
if (!isDesktopSupported) {
log.finer("isDesktopSupported: no");
return null;
}
final Method getDesktopMethod = desktopClass
.getDeclaredMethod("getDesktop");
return getDesktopMethod.invoke(null);
} catch (Exception e) {
log.log(Level.FINE, "Exception in Desktop operation", e);
return null;
}
}
/**
* 在MAC系统下找到com.apple.eio.FileManager籄1�7
*
* @return the FileManager instance
* @throws ClassNotFoundException
* if FileManager was not found
*/
private static Class<?> getAppleFileManagerClass()
throws ClassNotFoundException {
log.finest("Mac looking for com.apple.eio.FileManager");
// NB The following String is intentionally not inlined to prevent
// ProGuard trying to locate the unknown class.
final String appleClass = "com.apple.eio.FileManager";
return Class.forName(appleClass);
}
/**
* 棄1�7查是否存在一个给定的可执行文件,通过"which"指命令�1�7�1�7
*
* @param cmd
* the executable to locate
* @return true if the executable was found
* @throws IOException
* if Runtime.exec() throws an IOException
*/
private static boolean unixCommandExists(final String cmd)
throws IOException {
final Process whichProcess = Runtime.getRuntime().exec(
new String[] { "which", cmd });
boolean finished = false;
do {
try {
whichProcess.waitFor();
finished = true;
} catch (InterruptedException e) {
log.log(Level.WARNING,
"Interrupted waiting for which to complete", e);
}
} while (!finished);
return whichProcess.exitValue() == 0;
}
}
分享到:
相关推荐
"Java Desktop_java_javaDesktop_desktop_"这个标题暗示了我们即将探讨的是关于使用Java进行桌面应用开发的知识点。描述中的"java develop for desktop"进一步确认了这一主题,即如何使用Java语言来构建桌面应用程序...
本项目名为"Java-Desktop.rar",是一个基于Java的桌面精灵,尽管其设计可能较为简陋,不那么美观,但仍然可以作为学习Java桌面应用开发的基础。 Java Desktop API是Java提供的一组用于开发桌面应用的类和接口,主要...
JDesktopIntegrationLibrary是一个开源项目,它提供了一种在Java应用程序中集成本地桌面环境的功能,包括与本地剪贴板同步、拖放支持以及远程桌面功能。这个库可能使得Java应用能更自然地与用户操作系统交互,提供...
6. **国际化与本地化**:对于多语言支持的应用,Java Desktop Util可能包含了资源包管理,便于开发者添加和管理不同语言的文本资源。 7. **日志记录**:提供统一的日志记录工具,帮助开发者追踪和调试应用程序,...
3. **多线程处理**:Java对多线程支持良好,桌面应用可能需要利用多线程来实现后台任务的异步执行,提高用户体验。 4. **I/O操作**:桌面应用可能需要读写文件、数据库或其他外部资源,Java的标准库提供了丰富的I/O...
【标题】"java源码 安卓自动化获取界面元素(appium-desktop) 2018127"涉及的核心知识点是安卓自动化测试和Appium Desktop的使用,这是一套基于Java语言实现的自动化测试框架。在本文中,我们将深入探讨这两个关键...
8. **事件驱动编程**: GUI应用通常采用事件驱动模型,用户在界面上的每次操作都会触发相应的事件,Java的事件监听器模式可以很好地支持这种编程模型。 9. **状态管理**:保持会话状态,如连接状态、鼠标和键盘的...
### Java Desktop System 电子邮件和日历用户指南 #### 电子邮件和日历概述 **Java Desktop System**中的**电子邮件和日历**是一款高级的个人信息管理系统,它整合了多种实用的应用程序,帮助用户高效地管理和组织...
Java是一种广泛使用的编程语言,以其跨平台的特性、丰富的库和强大的社区支持而著名。本压缩包中的资源主要涵盖了使用Java进行三种不同类型的项目开发:命令行界面(CLI)、桌面应用(通过JavaFX)以及Web应用(使用...
Java Remote Desktop是一款基于Java开发的远程桌面控制软件,它允许用户通过网络访问并控制其他计算机。这个项目的主要目的是实现跨平台的远程桌面解决方案,因为Java的“一次编写,到处运行”特性使得它在多操作...
首先,Java提供了`Desktop`类,它是`java.awt.Desktop`包的一部分,可以用来与用户的桌面环境进行交互,包括打开浏览器、编辑文件、打印文档等。以下是一个简单的示例,展示如何使用`Desktop`类打开URL: ```java ...
在本案例中,提及的"远程桌面 Java Remote Desktop.7z"是一个使用Java编程语言实现的远程桌面解决方案。Java是一种广泛使用的、跨平台的编程语言,它的“编写一次,到处运行”(Write Once, Run Anywhere)特性使得...
方法二: 代码如下://判断当前系统是否支持Java AWT Desktop扩展 if(java.awt.Desktop.isDesktopSupported()){ try{ //创建一个URI实例,注意不是URL java.net.URI uri=java.net.URI.create(“//www.j
Java Desktop System(JDS)就是Java针对桌面应用提供的一套环境,包括Java运行时环境(JRE)、桌面应用框架、集成开发环境(IDE)等组件,使得开发者可以方便地创建、部署和运行桌面应用。 在医院管理应用程序中,...
总之,Java调用本地的远程桌面连接是一种结合操作系统功能和Java编程的技术,它在自动化运维、远程支持等领域有着广泛的应用。然而,使用时需要注意安全性和稳定性,确保数据的保密性,并考虑在不同环境下的兼容性。
标题“Desktop.zip”提示我们关注的是一个包含与桌面环境相关的文件的压缩包,而描述中的“jad-反编译”关键词则将焦点引向了Java应用程序的反编译过程。在IT领域,反编译是一种技术,用于将已编译的计算机程序(如...
Spring Desktop通常与JavaFX或Swing等Java桌面应用框架结合使用,提供更高级的用户界面和交互能力。通过Spring的DI(Dependency Injection)机制,开发者可以将业务逻辑与视图分离,使代码更易于维护和扩展。同时,...
JD4X是面向Linux x86 X Window用户的完全面向Java的桌面。 它面向希望拥有既支持本机应用程序又支持Java应用程序的Java友好桌面的Java用户和开发人员。 请访问我们的项目网站以获取更多详细信息。
Java图片加水印,支持旋转和透明度设置 摘要:Java源码,文件操作,图片水印 util实现Java图片水印添加功能,有添加图片水印和文字水印,可以设置水印位置,透明度、设置对线段锯齿状边缘处理、水印图片的路径,水印...