when i use xclip in terminal like following and hit ctrl+shift+v, cliboard will be written current date, and i get following
roroco@roroco ~/Dropbox/jvs/ro-idea $ echo -n $(date)|xclip -selection clipboard
roroco@roroco ~/Dropbox/jvs/ro-idea $ Mon Nov 17 10:54:21 HKT 2014
but when i try following code:
class Ex {
public static void main(String[] args) {
try {
Runtime.getRuntime().exec("echo -n $(date)|xclip -selection clipboard");
} catch (IOException e) {
e.printStackTrace();
}
}
}
clipboard is not changed, **my question is **how to make Runtime.getRuntime().exec
work like terminal
also i try java clipboard:
package ro.ex;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
public class Ex {
public static void main(String[] args) {
String myString = "This text will be copied into clipboard when running this code!";
StringSelection stringSelection = new StringSelection(myString);
Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
clpbrd.setContents(stringSelection, null);
}
}
it also doesn't work
this is my os:
No LSB modules are available.
Distributor ID: LinuxMint
Description: Linux Mint 17 Qiana
Release: 17
Codename: qiana
update following code work for me(just add System.in.read()
) but it must keep java process running
package ro.ex;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.io.IOException;
/**
* Created by roroco on 11/17/14.
*/
public class Ex2 {
public static void main(String[] args) throws IOException {
String myString = "This text will be copied into clipboard when running this code!";
StringSelection stringSelection = new StringSelection(myString);
Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
clpbrd.setContents(stringSelection, stringSelection);
System.in.read();
}
}
update the most simple way is, save content to tmp file, and use xclip to read it
public class Util {
public static void toClip(Object o) {
String f = "/tmp/clipboard";
File.write(f, o.toString());
bash("xclip -selection clipboard", f);
}
}