Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8270913

java.awt.datatransfer.Clipboard fails to transfer data in Ubuntu OS

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      linux

      Description

      ADDITIONAL SYSTEM INFORMATION :
      openjdk version "11.0.11" 2021-06-29
      OpenJDK Runtime Environment (build 11.0.11+9-google-release-382113311)
      OpenJDK 64-Bit Server VM (build 11.0.11+9-google-release-382113311, mixed mode, sharing)

      $ lsb_release -a
      No LSB modules are available.
      Distributor ID: Debian
      Description: Debian GNU/Linux rodete
      Release: rodete
      Codename: rodete
      mperou@mperou:~/tools/cli/Tools/src/com/neptunedreams/tools/cli

      $ uname -r
      5.10.40-1rodete1-amd64


      A DESCRIPTION OF THE PROBLEM :
      After copying text to the clipboard, then exiting the application and trying to paste the text into another non-java application, the paste menu item will not be available, so the data can't get transferred.

      It may seem like this is too trivial a test case to be worth fixing, but I need it for a tool I've written that starts with a shebang line, which reads the clipboard, replaces any instances of a literal \n with the new line character, then puts the filtered text back on the clipboard. I need this to effectively use the developer tools required for my job. The actual code looks like this. (But use the test case. it's simpler.)

      #!/usr/bin/java --source 11
      // use: brklns

      import java.awt.Toolkit;
      import java.awt.datatransfer.Clipboard;
      import java.awt.datatransfer.DataFlavor;
      import java.awt.datatransfer.StringSelection;
      import java.awt.datatransfer.UnsupportedFlavorException;
      import java.io.IOException;

      public enum BreakLines {
        ;

        public static void main(String[] args) throws IOException, UnsupportedFlavorException {
          Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
          String data = systemClipboard.getData(DataFlavor.stringFlavor).toString();
          StringSelection filteredContent = new StringSelection(filter(data));
          systemClipboard.setContents(filteredContent, filteredContent);
        }
        
        private static String filter(String data) {
          return data.replace("\\n", "\n");
        }
      }


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Run the test case.
      2. Go to a non-java application, such as a browser, and try to paste the text into any text field

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      You should be able to paste the text, which will be printed by the test case. You should not have to select the text or copy it. The test case should do that.
      ACTUAL -
      The paste option will not be available, and pasting will do nothing. You may try from a pop-up menu or the edit menu, depending on the application, but the paste menu item will be disabled.

      ---------- BEGIN SOURCE ----------
      import java.awt.Toolkit;
      import java.awt.datatransfer.Clipboard;
      import java.awt.datatransfer.DataFlavor;
      import java.awt.datatransfer.StringSelection;
      import java.awt.datatransfer.UnsupportedFlavorException;
      import java.io.IOException;

      public enum ClipboardBug {
        ;

        public static void main(String[] args) throws IOException, UnsupportedFlavorException {
          Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
          String input = "Input at " + System.currentTimeMillis();
          StringSelection filteredContent = new StringSelection(input);
          systemClipboard.setContents(filteredContent, filteredContent);
          String result = Toolkit.getDefaultToolkit()
              .getSystemClipboard()
              .getData(DataFlavor.stringFlavor)
              .toString();
          if (result.equals(input)) {
            System.out.println(result);
            System.out.println("(Text has been copied to the clipboard. Now try pasting into a non-Java application.)");
          } else {
            System.out.printf("Mismatch: %s vs %s%n", input, result);
          }
          System.exit(0); // this doesn't help
        }
      }

      ---------- END SOURCE ----------

      FREQUENCY : always


        Attachments

          Activity

            People

            Assignee:
            azvegint Alexander Zvegintsev
            Reporter:
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated: