简体   繁体   English

如何依次从Java执行wsl命令?

[英]How do I sequentially execute wsl commands from java?

I want to execute wsl commands from java . 我想从java执行wsl命令。

I'm trying to do this using Process and ProcessBuilder . 我正在尝试使用ProcessProcessBuilder做到这一点。

As I understand, there are two ways to do this: 据我了解,有两种方法可以做到这一点:

  1. Run wsl along with command as argument (for example: wsl ls -l ) (do this per-command). wsl与命令一起作为参数运行(例如: wsl ls -l )(按命令执行此操作)。
  2. Run wsl , and then execute the commands one by one. 运行wsl ,然后一个接一个地执行命令。

But there are some problems with 1 and 2 . 但是12存在一些问题。

With point 1 : 与第1点:

  1. When the command terminates, the process does not stop. 当命令终止时,该过程不会停止。 So even if i run wsl ls , I can not determine the moment when I can call next command. 因此,即使我运行wsl ls ,我也无法确定何时可以调用下一个命令。
  2. wsl does not save the state between such calls, so it's not very convenient wsl不会在wsl调用之间保存状态,所以不是很方便

With point 2 : 与第2点:

Since wsl does not show bash prompt , I can not track when the command stopped displaying information. 由于wsl不显示bash prompt ,因此我无法跟踪命令何时停止显示信息。 For example: 例如:

ProcessBuilder pb = new ProcessBuilder("wsl");
pb.redirectErrorStream(true);
Process p = pb.start();
Thread.sleep(1000);
OutputStreamWriter osw = new OutputStreamWriter(p.getOutputStream());
osw.write("ls\n");
osw.flush();

And all I can read is: 我所能读到的是:

build.gradle
gradle
gradlew
gradlew.bat
out
settings.gradle
src 

No selya@selya-pc:/mnt/c/Users/selya$ read. 没有selya@selya-pc:/mnt/c/Users/selya$读。 So I can't use it as a separator between commands. 因此,我不能将其用作命令之间的分隔符。 I think that wsl somehow tracks, in what environment it was launched, for example, through isatty() , and therefore the output is different. 我认为wsl以某种方式跟踪它在什么环境中启动,例如通过isatty() ,因此输出是不同的。

With both: 既:

Аs far as I know, for programs that are running outside the terminal, stdout is not buffered. 据我所知,对于在终端外部运行的程序,不会缓冲stdout So there are some problems with, for example, sudo , because it asks fro a password without newline / flush ( [sudo] password for selya: ), therefore I can't read this line... 因此,存在一些问题,例如sudo ,因为它在没有newline / flush情况下要求输入密码( [sudo] password for selya: ),因此我无法读取此行...

I found a solution - pass command as argument to unbuffer util, for example: 我找到了解决方案-将命令作为unbuffer util的参数传递,例如:

wsl unbuffer -p sudo apt-get update 

( -p stands for pipeline ). -p代表pipeline )。

But other problems still remain. 但是仍然存在其他问题。 Is there any way to do this? 有什么办法吗? Or maybe there is lib for it? 也许有它的库? Even c or c++ lib will suit my needs... 甚至cc++ lib也可以满足我的需求...

PS I tried to find a solution for several days. 附言:我试图寻找解决方案几天。 Russian-speaking SO didn't help me, that's why I'm here. 会说俄语的SO并没有帮助我,这就是为什么我在这里。 Sorry for my English... 对不起我的英语不好...

The problem was solved with the help of pty4j . 这个问题在pty4j的帮助下得以解决。 It works on linux, mac and windows. 它适用于linux,mac和Windows。 (It works like pseudo terminal on linux, but have nice java interface). (它的工作方式类似于Linux上的伪终端,但具有不错的Java接口)。

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

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