user285594
user285594

Reputation:

How can i tell Java code run the script as it was running from command line as normal user?

When i run this script manually then Browser chrome open the site in one tab (which is PERFECT exactly how i needed)

But when i run the same script using Java sample code 10 times, it open Browser but 10 times same page 10 TABs.

Q. How can i tell Java code please run it as it was suppose to be running manual execution (so that i have 1 TAB only?) ?

BASH: /var/tmp/runme.sh (ran 1o times and still have always 1 tab as expected)

export DISPLAY=:0.0
ps aux | grep chromium-browser | awk '{ print $2 }' | xargs kill -9;
sleep 8;
chromium-browser --process-per-site --no-discard-tabs --ash-disable-tab-scrubbing -disable-translate "http://www.oracle.com" &

Java: launch 10 times that script

  system("/var/tmp/runme.sh &");

  public static String system(String cmds) {
    String value = "";
    try {
      String cmd[] = { "/bin/sh", "-c", cmds};
      Process p = Runtime.getRuntime().exec(cmd);
      p.waitFor();
      BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
      String line = reader.readLine();
      while (line != null) {
        value += line + "\n\r";
        line = reader.readLine();
      }
    } 
    catch (IOException ioe) {
      ioe.printStackTrace();
    } 
    catch (InterruptedException ie) {
      ie.printStackTrace();
    }
    return value;
  }

Upvotes: 0

Views: 273

Answers (2)

user285594
user285594

Reputation:

Java is weired sometimes. Its solved.

1( kill chromium browser before killing the java

2( after killing chromium browser then launch the java application

3( now the tab is 1 and browser is 1

BEFORE: (wrong)

export DISPLAY=:0.0
pkill java;
java -cp SystemV.jar Main.Start "boot chromium now with 1 tab and 1 browser" &
ps aux | grep chromium-browser | awk '{ print $2 }' | xargs kill -9;
chromium-browser --process-per-site --no-discard-tabs --ash-disable-tab-scrubbing -disable-translate "http://www.oracle.com" &

AFTER:

export DISPLAY=:0.0
ps aux | grep chromium-browser | awk '{ print $2 }' | xargs kill -9;
chromium-browser --process-per-site --no-discard-tabs --ash-disable-tab-scrubbing -disable-translate "http://www.oracle.com" &
pkill java;
java -cp SystemV.jar Main.Start "boot chromium now with 1 tab and 1 browser" &
echo "it works now"

Upvotes: 1

SSaikia_JtheRocker
SSaikia_JtheRocker

Reputation: 5063

Fist remove & from this line system("/var/tmp/runme.sh &");

Second, maybe since, you are using this: "/bin/sh", Java is running the script as different shell every time you invoke using Runtime?

and you are executing /var/tmp/runme.sh from the same shell everytime.

Note: /bin/sh is an interpreter and with Java Runtime you are invoking multiple instances of it to execute your script every time.

Upvotes: 0

Related Questions