我正在尝试从Java启动链接。我这样做的方式是调用firefox,Internet explorer或safari,如下所示:
public class LinkLauncher implements Runnable {
static String Link;
public void launchLink(String link){
Link = " \""+link+"\"";
Runnable runnable = new LinkLauncher();
Thread thread = new Thread(runnable);
thread.start();
}
public void run() {
if (Desktop.isDesktopSupported()) {
Desktop desktop;
desktop = Desktop.getDesktop();
URI uri = null;
try {
uri = new URI(Link);
desktop.browse(uri);
} catch (IOException ioe) {
} catch (URISyntaxException use) {
}
} else {
Shell Shell = new Shell();
String Cmd[]={"firefox", Link};
String LaunchRes=Shell.sendShellCommand(Cmd);
if (LaunchRes.contains("CritERROR!!!")){
String MCmd[]={"open" , Link};
String MLaunchRes=Shell.sendShellCommand(MCmd);
if (MLaunchRes.contains("CritERROR!!!")){
String WCmd[]={"explorer", Link};
Shell.sendShellCommand(WCmd);
}
}
}
}
}
此方法在NetBeans中运行良好,但是一旦我创建了一个java jar文件,它就会停止工作。
当我从netbeans转到jar时,它不会遗漏任何库。它只是将%U显示为Firefox或其他浏览器中的链接。
我能解决这个问题吗?
提供完整代码答案 0 :(得分:1)
桌面不受支持,但Java声称它是。我修改了这样的代码,所以它尝试的最后一件事就是启动“支持”的浏览器。
public class LinkLauncher implements Runnable {
static String Link;
public void launchLink(String link){
Link = link;
Runnable runnable = new LinkLauncher();
Thread thread = new Thread(runnable);
thread.start();
}
public void run() {
Shell Shell = new Shell();
String Cmd[]={"firefox", Link};
String LaunchRes=Shell.sendShellCommand(Cmd);
if (LaunchRes.contains("CritERROR!!!")){
String MCmd[]={"open" , Link};
String MLaunchRes=Shell.sendShellCommand(MCmd);
if (MLaunchRes.contains("CritERROR!!!")){
String WCmd[]={"explorer", Link};
String WLaunchRes=Shell.sendShellCommand(WCmd);
if (WLaunchRes.contains("CritERROR!!!")){
if (Desktop.isDesktopSupported()) {
Desktop desktop;
desktop = Desktop.getDesktop();
URI uri = null;
try {
uri = new URI(Link);
desktop.browse(uri);
} catch (IOException ioe) {
} catch (URISyntaxException use) {
}
}
}
}
}
}
}