复制到全局剪贴板不会在Ubuntu与Java工作 [英] Copying to global clipboard does not work with Java in Ubuntu

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

问题描述

这是一个独立的应用程序下面的code工作在Ubuntu的:

 进口java.awt.Toolkit中;
进口java.awt.datatransfer.Clipboard中;
进口java.awt.datatransfer.DataFlavor中;
进口java.awt.datatransfer.StringSelection时;
进口java.awt.datatransfer.UnsupportedFlavorException;
进口java.io.IOException异常;公共类ClipboardTest {    公共静态无效的主要(字串[] args)抛出异常{
        剪贴板剪贴板= Toolkit.getDefaultToolkit()getSystemClipboard()。
        //打印最后复制的东西
        的System.out.println(clipBoard.getContents(空).getTransferData(选择DataFlavor.stringFlavor));
        StringSelection数据=新StringSelection(NOW);
        clipBoard.setContents(数据,数据);
        //输出NOW
        的System.out.println(clipBoard.getContents(空).getTransferData(选择DataFlavor.stringFlavor));
    }}

粘贴(Ctrl + V键)到任何一个不同的应用成果;我希望NOW。调用上面的code第二次给出了以下异常:

 异常线程mainjava.awt.datatransfer.UnsupportedFlavorException:UNI code字符串
    在sun.awt.datatransfer.ClipboardTransferable.getTransferData(ClipboardTransferable.java:160)

作为一个独立的应用程序,这应该工作,即使以后 2011安全更改。通过按Ctrl + C复制,从一个JTextField里面,然后粘贴在其他地方工作。

去过在Ubuntu 11.04与两个最新java7目录(jdk1.7.0_10)和jdk1.6.0_33不成功的;它应该工作并没有工作作为Windows 7的最新java7目录,并有望在Mac OSX上10.6 java6_37。也尝试过的Xubuntu 12.04与Java类,并没有在那里工作。这是一个Linux / Ubuntu的错误?

相关问题

我与我的工作申请同样的问题,这里是一个文章,我发现,解释了为什么和可能的解决方案。我希望它能帮助。

为什么会发生


  

剪贴板的持久性是影响Ubuntu和其他基于X11的操作系统下,许多程序的错误。修复它是code 2010项目的谷歌的夏天。维基百科有这个问题的一个很好的概述。如果你要修复作为一个用户,你可以安装Parcellite或其他剪贴板管理。如果你要修复它作为一个程序员,你可以修改你的程序符合ClipboardManager规范。


的X-Window维基

使用GNOME库你可以调用剪贴板上的存储方法和解决这个问题。这是迄今为止唯一的东西,这似乎是值得一试。也看到了类似的事情为GTK但只限在Eclipse的错误。

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));
    }

}

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)

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.

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?

Related question

解决方案

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.

Why it happens

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 wiki

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天全站免登陆