简体   繁体   English

我怎么能告诉Java代码运行脚本,因为它是从普通用户的命令行运行的?

[英]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) 当我手动运行此脚本时,浏览器chrome在一个选项卡中打开网站(这完全是我需要的完美)

But when i run the same script using Java sample code 10 times, it open Browser but 10 times same page 10 TABs. 但是当我使用Java示例代码运行相同的脚本10次时,它会打开浏览器但是10次相同的页面10 TAB。

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?) ? 问:如何告诉Java代码请运行它,因为它假设是在运行手动执行(因此我只有1个TAB?)?

BASH: /var/tmp/runme.sh (ran 1o times and still have always 1 tab as expected) BASH: /var/tmp/runme.sh(运行了1次,仍然总是有1个标签符合预期)

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 Java:启动该脚本的10倍

  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;
  }

Fist remove & from this line system("/var/tmp/runme.sh &"); 拳头删除从这个线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? 第二,也许从那以后,你正在使用这个: “/ bin / sh” ,每次使用Runtime调用时,Java都会将脚本作为不同的shell运行?

and you are executing /var/tmp/runme.sh from the same shell everytime. 并且您每次都从同一个shell执行/var/tmp/runme.sh

Note: /bin/sh is an interpreter and with Java Runtime you are invoking multiple instances of it to execute your script every time. 注意:/ bin / sh是一个解释器,使用Java Runtime,您每次都要调用它的多个实例来执行脚本。

Java is weired sometimes. 有时会使用Java。 Its solved. 它解决了。

1( kill chromium browser before killing the java 1(在杀死java之前杀死chrome浏览器

2( after killing chromium browser then launch the java application 2(杀死chrome浏览器后再启动java应用程序

3( now the tab is 1 and browser is 1 3(现在标签为1,浏览器为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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM