gpt4 book ai didi

java - 我如何告诉 Java 代码以普通用户身份从命令行运行脚本时运行脚本?

转载 作者:太空宇宙 更新时间:2023-11-04 09:44:36 27 4
gpt4 key购买 nike

当我手动运行此脚本时,浏览器 chrome 在一个选项卡中打开网站(这正是我所需要的)

但是当我使用 Java 示例代码运行同一个脚本 10 次时,它会打开浏览器,但 10 次是相同的页面 10 个 TAB。

问。我如何告诉 Java 代码请运行它,因为它应该运行手动执行(这样我只有 1 个 TAB?)?

BASH:/var/tmp/runme.sh(运行了 1o 次,但仍按预期始终有 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: 启动该脚本的 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;
}

最佳答案

Java 有时很奇怪。解决了。​​

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

之前:(错误)

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" &

之后:

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"

关于java - 我如何告诉 Java 代码以普通用户身份从命令行运行脚本时运行脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17701666/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com