2013-08-01 32 views
14

tôi đã gặp phải lỗi này:Desktop API không được hỗ trợ trên nền tảng hiện tại

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

tôi sẽ mở một tập tin từ một ứng dụng java của tôi. Tôi sử dụng phương pháp này:

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

Tôi có thể giải quyết vấn đề này bằng cách nào?

+0

bạn đang sử dụng nền tảng nào? – null

+0

Tôi sử dụng ubuntu 13.04 .. – user2520969

+0

kiểm tra câu hỏi này: http://stackoverflow.com/questions/102325/not-supported-platforms-for-java-awt-desktop-getdesktop – null

Trả lời

37

Về cơ bản, vấn đề là tích hợp Java Desktop không hoạt động tốt trên Linux.

Nó được thiết kế để hoạt động tốt với Windows; một cái gì đó hoạt động trên các hệ thống khác, nhưng không ai thực sự quan tâm để thêm hỗ trợ thích hợp cho những hệ thống đó. Ngay cả khi bạn cài đặt 'thư viện gnome' bắt buộc, kết quả sẽ kém.

Tôi đã gặp phải vấn đề tương tự một thời gian trước đây và đã đến với lớp bên dưới.

Mục tiêu đạt được bằng cách sử dụng lệnh hệ thống cụ thể:

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

Nếu không ai trong số những tác phẩm, nó sẽ cố gắng thực hiện được cung cấp bởi Java Desktop.
Bởi vì điều này thường thất bại, nó đã cố gắng như là phương sách cuối cùng.


lớp DesktopApi

Lớp này cung cấp phương pháp tĩnh open, browseedit.
Nó được thử nghiệm để hoạt động trên Linux (Kde và Gnome), Windows và Mac.

Nếu bạn sử dụng, vui lòng cho tôi tín dụng.

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

Cảm ơn! Nó hoạt động với lớp học của bạn. – user2520969

+1

Cài đặt đầy đủ libgnome2-0 sửa chữa nó, chúng tôi, là nhà phát triển, phải giải quyết những điều đó. Cảm ơn bạn! :-) –

+0

@MightyPork Những nỗ lực tốt. Giữ nó lên. +1 cho * Mục tiêu đạt được bằng cách sử dụng các lệnh hệ thống cụ thể *. – OO7

6

Lớp Desktop không được hỗ trợ trên tất cả các hệ thống.

Từ Swing Java hướng dẫn 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().

Trong mọi trường hợp, nó sẽ là tốt nhất để cung cấp một cách khác để mở một tập tin nếu không có sự hỗ trợ cho máy tính để bàn.

0

Hỗ trợ thay đổi giữa các lần triển khai trên các JDK khác nhau. Tôi đã gặp phải "UnsupportedOperationException" bằng cách sử dụng OpenJDK 1.7.0. Chuyển sang Oracle JDK 1.7 đã làm việc.

Trường hợp thực tế, bạn có thể chuyển đổi JDK hoặc đề xuất người dùng của bạn chuyển JDK để bật một tính năng nhất định.

15

Tôi đang sử dụng Ubuntu 12.04 LTS 64-bit với Oracle jdk1.6.0_45 và gặp sự cố tương tự. Tôi đang chạy gnome-classic như máy tính để bàn thay vì Unity. Đây là những gì làm việc cho tôi:

sudo apt-get install libgnome2-0 

Sau khi cài đặt gói này, tôi khởi động lại ứng dụng Java Swing và Desktop.getDesktop().open(new File("myfile")); chỉ hoạt động tốt.

+0

Trên CentOS 7 chạy XFCE, 'sudo yum install libgnome' đã làm thủ thuật! – schneida

Các vấn đề liên quan