Copy to clipboard java

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


String testString = "Text that needs to be copied into clipboard";
StringSelection stringSelectionObj = new StringSelection(testString);
Clipboard clipboardObj = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboardObj.setContents(stringSelectionObj, null);