English 中文(简体)
How to launch a website in Java Swing, in a way that works in Gnome and KDE
原标题:

As outline in a previous question I asked.

A website can be launched by doing this:

Desktop.getDesktop().browse(new java.net.URI("www.google.com"));

This works fine in Ubuntu(gnome), but it doesn t appear to work in OpenSUSE(KDE flavour). There is a bug lodged with Sun about this.

Any ideas on alternate ways to do this, that will work in both Gnome and KDE.

问题回答

As a workaround, you could run the standard command to open files or URLs on any Linux desktop: xdg-open.

http://portland.freedesktop.org/xdg-utils-1.0/xdg-open.html

While waiting for the bug fix from Sun/Oracle, you can find the user s default browser and invoke it yourself using the ProcessBuilder class. You can find the default browser in gnome using the gnonftool-2 utility. I m not sure how in KDE. Here s an example where I try to find if the user is running Clearlooks on gnome:

private boolean usingClearlooks() {
    try {
        File gconf = new File("/usr/bin/gconftool-2");
        if(gconf.exists() == false) {
            return false;
        }
        ProcessBuilder pb = new ProcessBuilder(gconf.getAbsolutePath(), "-g", "/desktop/gnome/interface/gtk_theme");
        Process psProc = pb.start();
        psProc.waitFor();
        BufferedReader br = new BufferedReader(new InputStreamReader(psProc.getInputStream()));
        boolean clearlooks = false;
        String line = null;
        while((line=br.readLine()) != null) {                                                       
            if ((line.toLowerCase().contains("clearlooks"))) {
                clearlooks = true;
                break;
            }
        }
        return clearlooks;
    }
    catch(Exception e) {
        e.printStackTrace();
        return false;
    }
}

strace -f it and see what gets executed. I wonder what java is thinking it s a good police for cross-desktop browser execution.

If you don t mind using an extra library you could try JDIC





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签