2

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);
    }
}
Anptk
  • 1,125
  • 2
  • 17
  • 28
cimi3386284gt
  • 301
  • 1
  • 2
  • 8
  • If that does not work, consider using the Java clipboard integration to put stuff in the system clipboard: https://docs.oracle.com/javase/7/docs/api/java/awt/Toolkit.html#getSystemClipboard%28%29 – Thilo Nov 17 '14 at 03:16
  • 1
    @Thilo Both java clipboard and xclip didn't work – cimi3386284gt Nov 17 '14 at 03:21
  • 1
    I think the problem here may be with the pipe: http://stackoverflow.com/questions/5928225/how-to-make-pipes-work-with-runtime-exec – maksimov Nov 17 '14 at 11:59

0 Answers0