Why isn't there a protocol error on OSX trying to use the dataflavor that this portable file has?

Reporting this error when trying to copy and paste an image from amazon, although we are checking for the portable object support on a specific file, it fails when trying to use this data attribute to get the url. Ive not seen thsi errors before and recently switched to Java 1.8.0_20, so I assume the problems lie in Java 8?

 java.net.MalformedURLException: no protocol: <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    at java.net.URL.<init>(URL.java:586)
    at java.net.URL.<init>(URL.java:483)
    at java.net.URL.<init>(URL.java:432)
    at sun.lwawt.macosx.CDataTransferer.translateBytes(CDataTransferer.java:135)
    at sun.awt.datatransfer.ClipboardTransferable$DataFactory.getTransferData(ClipboardTransferable.java:71)
    at sun.awt.datatransfer.ClipboardTransferable.getTransferData(ClipboardTransferable.java:168)


Extracting the code

public static DataFlavor imageUrlFlavor =  new DataFlavor("application/x-java-url;class=java.net.URL");
public void getImage(Transferable trans)

                imageUrl  = (URL) trans.getTransferData(FileDropTarget.imageUrlFlavor);



source to share

2 answers

Unsatisfactory answer You can list everything DataFlavor

placed on the clipboard trans.getDataFlavors () ; in general, the copy places several varieties, such as text and text text.


public static DataFlavor imageUrlFlavor = 
    new DataFlavor(URL.class, "application/x-java-url");


probably doesn't work either.



OS X seems to wrap the copied data in plist / xml, so it breaks your parsing. Are you copying images directly from your browser or from another application?

I tried to copy images and urls from Chrome and Safari to a simple swing app on my mac (OS X 10.9.5, Java 8u20) and I always got text / plain or images / x-java images mimetypes, never app / x -java-url or xml-wrapped.

 Transferable clipData = clipboard.getContents(clipboard);
 if (clipData != null) {
     try {
           for (DataFlavor f : clipData.getTransferDataFlavors()) {
              System.out.println("f = " + f);

           if (clipData.isDataFlavorSupported(imageUrlFlavor)) {
               URL url = (URL) clipData.getTransferData(imageUrlFlavor);

               System.out.println("Got url:");

      } catch (UnsupportedFlavorException ufe) {
          System.err.println("Flavor unsupported: " + ufe);
      } catch (IOException ioe) {
          System.err.println("Data not available: " + ioe);


I also tried to do it using drag'n'drop and got the same result:

public void drop(DropTargetDropEvent dtde) {
    Transferable trans = dtde.getTransferable();
    boolean gotData = false;
    // try for application/x-java-url flavor
    try {
        if (trans.isDataFlavorSupported(urlFlavor)) {
            URL url = (URL) trans.getTransferData(urlFlavor);
            System.out.println ("got an url: " + url);
            gotData = true;
        } else if (trans.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            String s =
                    (String) trans.getTransferData(DataFlavor.stringFlavor);
            System.out.println("Got text: " + s);
            gotData = true;
        } else {

    } catch (Exception e) {
    } finally {
        System.out.println("gotData is " + gotData);




All Articles