Friday, January 04, 2008

Set Clipboard contents from command line using java

Following code(setClipboardContents.java) sets the Clipboard contents for 10 seconds:


import java.awt.datatransfer.StringSelection;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;

public class setClipboardContents{
public static void main(String[] args) throws Exception {
StringSelection stringSelection = new StringSelection( args[0] );
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents( stringSelection, null);
try{
Thread.sleep(10000);
} catch(InterruptedException e){
System.out.println("Sleep Interrupted");
}
}
}


Compile: javac setClipboardContents.java
Run: java -cp . setClipboardContents "add this content to the Clipboard"

1 comment:

Anonymous said...

I would like too take time too thank the active members for doing what you do and make this community great im a long time reader and first time poster so i just wanted to say thanks.