复制到全局剪贴板不适用于 Ubuntu 中的 Java [英] Copying to global clipboard does not work with Java in Ubuntu

查看:25
本文介绍了复制到全局剪贴板不适用于 Ubuntu 中的 Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自独立应用程序的以下代码适用于 ubuntu:

The following code from a standalone application works in ubuntu:

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 class ClipboardTest {

    public static void main(String[] args) throws Exception {
        Clipboard clipBoard = Toolkit.getDefaultToolkit().getSystemClipboard();        
        // print the last copied thing
        System.out.println(clipBoard.getContents(null).getTransferData(DataFlavor.stringFlavor));
        StringSelection data = new StringSelection("NOW");
        clipBoard.setContents(data, data);
        // prints NOW
        System.out.println(clipBoard.getContents(null).getTransferData(DataFlavor.stringFlavor));
    }

}

粘贴 (Ctrl+V) 到不同的应用程序不会产生任何结果;我期待现在".第二次调用上面的代码会出现以下异常:

Pasting (Ctrl+V) into a different application results in nothing; I expect "NOW". Calling the above code a second time gives the following exception:

Exception in thread "main" java.awt.datatransfer.UnsupportedFlavorException: Unicode String
    at sun.awt.datatransfer.ClipboardTransferable.getTransferData(ClipboardTransferable.java:160)

作为一个独立的应用程序,即使在 2011 年安全更改之后,这也应该可以工作.通过 Ctrl+C 从 JTextField 内部复制,然后粘贴到其他地方.

As a standalone application, this should work even after 2011 security changes. Copying via Ctrl+C from inside of a JTextField and then pasting elsewhere works.

在 ubuntu 11.04 上使用最新的 java7 (jdk1.7.0_10) 和 jdk1.6.0_33 都不成功;它应该可以工作并且在使用最新 java7 的 windows 7 和使用 java6_37 的 mac osx 10.6 上按预期工作.还用这些 java 尝试了 xubuntu 12.04,但在那里不起作用.这是一个 linux/ubuntu 错误吗?

Have been unsuccessful on ubuntu 11.04 with both the latest java7 (jdk1.7.0_10) and jdk1.6.0_33; It should work and does work as expected on windows 7 with the latest java7 and on mac osx 10.6 with java6_37. Also tried xubuntu 12.04 with those javas and it doesn't work there. Is this a linux/ubuntu bug?

相关问题

推荐答案

我在工作中遇到了与应用程序相同的问题,这是我找到的一篇文章,解释了原因和可能的解决方案.希望能帮到你.

I got the same problem with the application at my work and here's an article I've found that explain why and possible solutions. I hope it helps.

为什么会发生

剪贴板持久性是一个会影响 Ubuntu 和其他基于 X11 的操作系统下的许多程序的错误.修复它是 Google Summer of Code 2010 项目.维基百科对这个问题有一个很好的概述.如果您想以用户身份修复,您可以安装 Parcellite 或其他剪贴板管理器.如果您想作为程序员修复它,您可以修改您的程序以符合 ClipboardManager 规范.

Clipboard persistence is a bug that affects many programs under Ubuntu and other X11-based operating systems. Fixing it is a Google Summer of Code 2010 project. Wikipedia has a good overview of the issue. If you want to fix as a user, you can install Parcellite or another clipboard manager. If you want to fix it as a programmer, you can modify your program to conform to the ClipboardManager specification.

X-Window 维基

使用gnome库你可以调用剪贴板上的 store 方法并修复此问题.到目前为止,这是唯一值得尝试的事情.在 GTK 中也看到了类似的事情,但仅在 Eclipse 的错误中.

Using gnome library you could call the store method on the clipboard and fix this. That's the only thing so far that seems to be worth trying. Also saw a similar thing for GTK but only in an Eclipse's bug.

这篇关于复制到全局剪贴板不适用于 Ubuntu 中的 Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆