2013-08-01 14 views
14

ho incontrato questo errore:Desktop API non è supportata nella piattaforma corrente

java.lang.UnsupportedOperationException: Desktop API is not supported on the current platform 

vorrei aprire un file dal mio applicazione java. Io uso questo metodo:

   Desktop.getDesktop().open(new File(report.html")); 

Come posso risolvere questo problema?

+0

quale piattaforma stai usando? – null

+0

Io uso Ubuntu 13.04 .. – user2520969

+0

controlla questa domanda: http://stackoverflow.com/questions/102325/not-supported-platforms-for-java-awt-desktop-getdesktop – null

risposta

37

Fondamentalmente, il problema è che l'integrazione di Java Desktop non funziona bene su Linux.

È stato progettato per funzionare correttamente con Windows; qualcosa funziona su altri sistemi, ma a nessuno interessa veramente aggiungere un supporto adeguato per quelli. Anche se installi le "librerie gnome" richieste, i risultati saranno scarsi.

Ho affrontato lo stesso problema un po 'di tempo fa e ho trovato la classe di seguito.

L'obiettivo viene raggiunto utilizzando comandi specifici del sistema:

KDE:  kde-open 
GNOME: gnome-open 
Any X-server system: xdg-open 
MAC:  open 
Windows: explorer 

Se nessuna di queste opere, tenta l'attuazione fornito da Java Desktop.
Poiché questo di solito non funziona, viene provato come ultima risorsa.


classe DesktopApi

Questa classe fornisce metodi statici open, browse e edit.
È testato per funzionare su Linux (Kde e Gnome), Windows e Mac.

Se lo usi, per favore mi dia credito.

package net.mightypork.rpack.utils; 

import java.awt.Desktop; 
import java.io.File; 
import java.io.IOException; 
import java.net.URI; 
import java.util.ArrayList; 
import java.util.List; 


public class DesktopApi { 

    public static boolean browse(URI uri) { 

     if (openSystemSpecific(uri.toString())) return true; 

     if (browseDESKTOP(uri)) return true; 

     return false; 
    } 


    public static boolean open(File file) { 

     if (openSystemSpecific(file.getPath())) return true; 

     if (openDESKTOP(file)) return true; 

     return false; 
    } 


    public static boolean edit(File file) { 

     // you can try something like 
     // runCommand("gimp", "%s", file.getPath()) 
     // based on user preferences. 

     if (openSystemSpecific(file.getPath())) return true; 

     if (editDESKTOP(file)) return true; 

     return false; 
    } 


    private static boolean openSystemSpecific(String what) { 

     EnumOS os = getOs(); 

     if (os.isLinux()) { 
      if (runCommand("kde-open", "%s", what)) return true; 
      if (runCommand("gnome-open", "%s", what)) return true; 
      if (runCommand("xdg-open", "%s", what)) return true; 
     } 

     if (os.isMac()) { 
      if (runCommand("open", "%s", what)) return true; 
     } 

     if (os.isWindows()) { 
      if (runCommand("explorer", "%s", what)) return true; 
     } 

     return false; 
    } 


    private static boolean browseDESKTOP(URI uri) { 

     logOut("Trying to use Desktop.getDesktop().browse() with " + uri.toString()); 
     try { 
      if (!Desktop.isDesktopSupported()) { 
       logErr("Platform is not supported."); 
       return false; 
      } 

      if (!Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) { 
       logErr("BROWSE is not supported."); 
       return false; 
      } 

      Desktop.getDesktop().browse(uri); 

      return true; 
     } catch (Throwable t) { 
      logErr("Error using desktop browse.", t); 
      return false; 
     } 
    } 


    private static boolean openDESKTOP(File file) { 

     logOut("Trying to use Desktop.getDesktop().open() with " + file.toString()); 
     try { 
      if (!Desktop.isDesktopSupported()) { 
       logErr("Platform is not supported."); 
       return false; 
      } 

      if (!Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) { 
       logErr("OPEN is not supported."); 
       return false; 
      } 

      Desktop.getDesktop().open(file); 

      return true; 
     } catch (Throwable t) { 
      logErr("Error using desktop open.", t); 
      return false; 
     } 
    } 


    private static boolean editDESKTOP(File file) { 

     logOut("Trying to use Desktop.getDesktop().edit() with " + file); 
     try { 
      if (!Desktop.isDesktopSupported()) { 
       logErr("Platform is not supported."); 
       return false; 
      } 

      if (!Desktop.getDesktop().isSupported(Desktop.Action.EDIT)) { 
       logErr("EDIT is not supported."); 
       return false; 
      } 

      Desktop.getDesktop().edit(file); 

      return true; 
     } catch (Throwable t) { 
      logErr("Error using desktop edit.", t); 
      return false; 
     } 
    } 


    private static boolean runCommand(String command, String args, String file) { 

     logOut("Trying to exec:\n cmd = " + command + "\n args = " + args + "\n %s = " + file); 

     String[] parts = prepareCommand(command, args, file); 

     try { 
      Process p = Runtime.getRuntime().exec(parts); 
      if (p == null) return false; 

      try { 
       int retval = p.exitValue(); 
       if (retval == 0) { 
        logErr("Process ended immediately."); 
        return false; 
       } else { 
        logErr("Process crashed."); 
        return false; 
       } 
      } catch (IllegalThreadStateException itse) { 
       logErr("Process is running."); 
       return true; 
      } 
     } catch (IOException e) { 
      logErr("Error running command.", e); 
      return false; 
     } 
    } 


    private static String[] prepareCommand(String command, String args, String file) { 

     List<String> parts = new ArrayList<String>(); 
     parts.add(command); 

     if (args != null) { 
      for (String s : args.split(" ")) { 
       s = String.format(s, file); // put in the filename thing 

       parts.add(s.trim()); 
      } 
     } 

     return parts.toArray(new String[parts.size()]); 
    } 

    private static void logErr(String msg, Throwable t) { 
     System.err.println(msg); 
     t.printStackTrace(); 
    } 

    private static void logErr(String msg) { 
     System.err.println(msg); 
    } 

    private static void logOut(String msg) { 
     System.out.println(msg); 
    } 

    public static enum EnumOS { 
     linux, macos, solaris, unknown, windows; 

     public boolean isLinux() { 

      return this == linux || this == solaris; 
     } 


     public boolean isMac() { 

      return this == macos; 
     } 


     public boolean isWindows() { 

      return this == windows; 
     } 
    } 


    public static EnumOS getOs() { 

     String s = System.getProperty("os.name").toLowerCase(); 

     if (s.contains("win")) { 
      return EnumOS.windows; 
     } 

     if (s.contains("mac")) { 
      return EnumOS.macos; 
     } 

     if (s.contains("solaris")) { 
      return EnumOS.solaris; 
     } 

     if (s.contains("sunos")) { 
      return EnumOS.solaris; 
     } 

     if (s.contains("linux")) { 
      return EnumOS.linux; 
     } 

     if (s.contains("unix")) { 
      return EnumOS.linux; 
     } else { 
      return EnumOS.unknown; 
     } 
    } 
} 
+0

Grazie! Funziona con la tua classe. – user2520969

+1

Anche se l'installazione di libgnome2-0 è stata risolta, noi, come sviluppatori, dobbiamo risolvere questi problemi. Grazie! :-) –

+0

@ MightyPork Buoni sforzi. Continuate così. +1 per * L'obiettivo viene raggiunto utilizzando i comandi specifici del sistema *. – OO7

6

La classe Desktop non è supportato su tutti i sistemi.

Dal Java Swing esercitazione How to Integrate with the Desktop Class:

Use the isDesktopSupported() method to determine whether the Desktop API is available. On the Solaris Operating System and the Linux platform, this API is dependent on Gnome libraries. If those libraries are unavailable, this method will return false. After determining that the Desktop API is supported, that is, the isDesktopSupported() returns true, the application can retrieve a Desktop instance using the static method getDesktop().

In ogni caso, sarebbe meglio per fornire un modo alternativo per aprire un file se non c'è il supporto per desktop.

0

Il supporto varia tra le implementazioni sui vari JDK. Ho riscontrato "UnsupportedOperationException" usando OpenJDK 1.7.0. Passare a Oracle JDK 1.7 ha funzionato.

Dove possibile, è possibile cambiare i JDK o suggerire agli utenti di attivare i JDK per abilitare una determinata funzione.

15

Sto usando Ubuntu 12.04 LTS 64-bit con Oracle jdk1.6.0_45 e ho riscontrato lo stesso problema. Sto eseguendo gnome-classic come desktop invece di Unity. Questo è ciò che ha funzionato per me:

sudo apt-get install libgnome2-0 

Dopo aver installato questo pacchetto ho riavviato il mio Java Swing app e Desktop.getDesktop().open(new File("myfile")); funzionato bene.

+0

Su CentOS 7 con XFCE, 'sudo yum install libgnome' ha fatto il trucco! – schneida

Problemi correlati