Copy to and Paste from Clipboard: A cross-platform solution in Java

By Angsuman Chakraborty, Gaea News Network
Monday, November 1, 2004

Have you ever felt the need for copying an output generated on command line to clipboard, or wanted to paste from clipboard to command line?

The following java utilities (cross-platform) will help you do just that.

Usage:
=> To copy the output of a program to clipboard
echo Hello World | java ClipCopy
dir | java ClipCopy

=> To paste the clipboard contents to a file
java ClipPaste > clip.txt

This can also be used to remove the requirement to create temporary files in a complex batch/shell script.

To type clipboard contents directly from command line just type: java ClipCopy
End with content with end character of your OS, Ctrl-Z for windows & Ctrl-D for Unix.

Download:
ClipCopy.class
ClipPaste.class

For line counters, ClipCopy is 4 lines of code and ClipPaste is 2 lines of code. This doesn’t take into account the import, class & main declarations and opening & closing braces.

Let me know if you find this useful.

Note: For non-Java users you might find it easier to download the jar file and run the modified commands as shown below:
Usage:
=> To copy the output of a program to clipboard
echo Hello World | java -cp clip.jar ClipCopy
dir | java -cp clip.jar ClipCopy

=> To paste the clipboard contents to a file
java -cp clip.jar ClipPaste > clip.txt

This takes care of any classpath setting issue. You still need to have jre installed in your system.

Limitation: Not tested with binary content. This is meant for text transfers only.

Filed under: Java Software
Discussion
April 10, 2009: 3:11 am

Fetch the page using URLConnection in Java and then post the code using any CVS (who uses cvs anymore?) Java API.


mahesh
April 9, 2009: 4:42 pm

i want to fetch output from jsp page and paste it to the file in cvs repository automatically ( using some code ie. not manual copy-paste.) this all is going to be done in eclipse ide. how can i get this code??

Thank u.

March 20, 2009: 3:10 pm

[...] it in Internet Explorer and copy it to clipboard using Ctrl-A Ctrl-C after first clicking within the explorer. Now the HTML is copied in proper [...]


Pete
October 2, 2006: 12:23 am

Sorry, im using standard MS-DOS (WinXP)


Pete
October 2, 2006: 12:21 am

I am having a problem with this application… it works well with “| java -cp clip.jar ClipCopy” piped from another command, but the problem is that it inserts a carriage return after the clipboard contents.
For instance: “echo Hello World | java -cp clip.jar ClipCopy”
displays as: “Hello World

This is a problem for me as i need to be able to paste the string (Hello World) into another program and it wont let me.

Any thoughts on how to get around this?

Cheers,

Pete

July 29, 2005: 9:57 am

Try
cat cclcl.pl | java -cp clip.jar ClipCopy


Anjan Bacchu
July 28, 2005: 1:24 pm

hi there,

thank you.

I’ve previously managed something like this using perl :-)

My perl script suffers though from not being able to copy multi-line output properly. I use the clipboard copy script using something like this
“cat blahblah.txt | xargs -0 clipcopy.pl”

But… coming back to your program, on Windows XP,
“dir | java -cp clip.jar ClipCopy” works just fine.
But when I try “cat cclcl.pl | xargs -0 java -cp clip.jar ClipCopy”, the program gets launched(which I observe by the delay) but I don’t see anything in the clipboard ??

BTW, I am using cygwin :-)

Thank you,

BR,
~A

March 15, 2005: 4:38 pm

[...] ve it. Remember to set Save as Type to HTML . Open it in Internet Explorer and copy it to clipboard using Ctrl-A Ctrl-C afte [...]

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :