linux - Getting awk return value with Java -
writing simple shell command our java application. have no experience in linux shell , need simple command. seek user's active window's process name. question found script follows:
ps -e | grep $(xdotool getwindowpid $(xdotool getwindowfocus)) | grep -v grep | awk '{print $4}'
source:http://unix.stackexchange.com/questions/38867/is-it-possible-to-retrieve-the-active-window-process-title-in-gnome
i able test , print result but. can't value java side. if run simple script like
echo asdasd
i can "asdasd" string fine. understand java can echo commands.
at java side utilize method: private string executecommand(string command) {
stringbuffer output = new stringbuffer(); process p; seek { p = runtime.getruntime().exec(command); p.waitfor(); bufferedreader reader = new bufferedreader(new inputstreamreader(p.getinputstream())); string line = ""; while ((line = reader.readline())!= null) { output.append(line + "\n"); } } grab (exception e) { e.printstacktrace(); } homecoming output.tostring(); }
source: http://www.mkyong.com/java/how-to-execute-shell-command-from-java/
------solved------
solved smasseman's answer, changed command string th next string array,
string[] args = {"/bin/sh","-c",command };
"command" script not filepath gave errors.
you can not utilize pipes when execute java. trick write linux commands , pipes script , execute script java. create script (lets phone call script activewin.sh):
#!/bin/bash ps -e | grep $(xdotool getwindowpid $(xdotool getwindowfocus)) | grep -v grep | awk '{print $4}'
make executable chmod +x activewin.sh
can execute script in java p = runtime.getruntime().exec("./activewin.sh");
you of course of study write script file java, execute , remove if want application more "stand alone".
java linux shell unix terminal
No comments:
Post a Comment