Copying to the clipboard in Java

JavaStringClipboardAwt

Java Problem Overview


I want to set the user's clipboard to a string in a Java console application. Any ideas?

Java Solutions


Solution 1 - Java

Use the [Toolkit][1] to get the [system clipboard][2]. Create a [StringSelection][3] with the String and add it to the Clipboard.

Simplified:

StringSelection selection = new StringSelection(theString);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(selection, selection);

[1]: http://docs.oracle.com/javase/8/docs/api/java/awt/Toolkit.html "class in java.awt" [2]: http://docs.oracle.com/javase/8/docs/api/java/awt/Toolkit.html#getSystemClipboard "getSystemClipboard()" [3]: http://docs.oracle.com/javase/8/docs/api/java/awt/datatransfer/StringSelection.html "class in java.awt.datatransfer"

Solution 2 - Java

Here is a simple SSCCE to accomplish this:

import java.awt.*;
import java.awt.datatransfer.*;
import java.io.*;

class ClipboardTest
{
	public static void main(String[] args)
		throws UnsupportedFlavorException, IOException
	{
		Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
		StringSelection testData;

		//  Add some test data

		if (args.length > 0)
			testData = new StringSelection( args[0] );
		else
			testData = new StringSelection( "Test Data" );

		c.setContents(testData, testData);

		//  Get clipboard contents, as a String

		Transferable t = c.getContents( null );

		if ( t.isDataFlavorSupported(DataFlavor.stringFlavor) )
		{
			Object o = t.getTransferData( DataFlavor.stringFlavor );
			String data = (String)t.getTransferData( DataFlavor.stringFlavor );
			System.out.println( "Clipboard contents: " + data );
		}

		System.exit(0);
	}
}

Solution 3 - Java

For anyone still stumbling upon this post searching for the JavaFX way to accomplish this, here you go:

ClipboardContent content = new ClipboardContent();
content.putString("Some text");
content.putHtml("<b>Bold</b> text");
Clipboard.getSystemClipboard().setContent(content);

For further information, read the documentation.

Solution 4 - Java

If you are on Linux and using OpenJDK, it will not work. You must use the Sun JDK on Linux for it to work.

Solution 5 - Java

In Linux with xclip:

Runtime run = Runtime.getRuntime();
Process p = null;
String str = "hello";
try {
        p = run.exec(new String[]{"sh", "-c", "echo " + str + " | xclip -selection clipboard"});
}
catch (Exception e) {
    System.out.println(e);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionclone1018View Question on Stackoverflow
Solution 1 - Javauser85421View Answer on Stackoverflow
Solution 2 - JavacamickrView Answer on Stackoverflow
Solution 3 - JavaxerufView Answer on Stackoverflow
Solution 4 - JavaradoView Answer on Stackoverflow
Solution 5 - JavaIovanny Olguín ÁvilaView Answer on Stackoverflow