Java Unix终端
我正在尝试用java编写unix终端仿真器。我有很多麻烦。我似乎无法更改程序的工作目录,因此“cd”之类的命令无法正常工作。我的问题是,如果我运行一个需要用户输入的命令,有没有办法将该输入发送到正在运行的进程 非常感谢,这帮了大忙。下面是一个例子: InputStream in=null; OutputStream outS=nullJava Unix终端,java,unix,terminal,Java,Unix,Terminal,我正在尝试用java编写unix终端仿真器。我有很多麻烦。我似乎无法更改程序的工作目录,因此“cd”之类的命令无法正常工作。我的问题是,如果我运行一个需要用户输入的命令,有没有办法将该输入发送到正在运行的进程 非常感谢,这帮了大忙。下面是一个例子: InputStream in=null; OutputStream outS=null StringBuffer commandResult = new StringBuffer(); String line = null;
StringBuffer commandResult = new StringBuffer();
String line = null;
int readInt;
p = Runtime.getRuntime().exec("gksudo apt-get install firefox");
int returnVal = p.waitFor();
in = p.getInputStream();
while ((readInt = in.read()) != -1)
commandResult.append((char)readInt);
outS = (BufferedOutputStream) p.getOutputStream();
outS.write("Y".getBytes());
outS.close();
System.out.println(commandResult.toString());
in.close();
这是输出:
Reading package lists...
Building dependency tree...
Reading state information...
The following packages were automatically installed and are no longer required:
libmono2.0-cil libmono-data-tds2.0-cil libmono-system-data2.0-cil
libdbus-glib1.0-cil librsvg2-2.18-cil libvncserver0 libsqlite0
libmono-messaging2.0-cil libmono-system-messaging2.0-cil
libmono-system-data-linq2.0-cil libmono-sqlite2.0-cil
libmono-system-web2.0-cil libwnck2.20-cil libgnome-keyring1.0-cil
libdbus1.0-cil libmono-wcf3.0-cil libgdiplus libgnomedesktop2.20-cil
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
firefox-globalmenu
Suggested packages:
firefox-gnome-support firefox-kde-support latex-xft-fonts
The following NEW packages will be installed:
firefox firefox-globalmenu
0 upgraded, 2 newly installed, 0 to remove and 5 not upgraded.
Need to get 15.2 MB of archives.
After this operation, 30.6 MB of additional disk space will be used.
Do you want to continue [Y/n]? Abort.
为什么在我可以插入“Y”之前它就中止了呢?是的;请参阅以获取对象的“标准输入”(stdin)流
至于更改目录的问题,我不相信JVM一旦启动就可以更改其工作目录。但是,您的程序可以将“当前工作目录”的概念建模为一个变量,在执行与该位置相关的操作(例如启动进程、列出目录内容等)时使用该变量。甚至还有一种方法可以为它生成的进程提供支持。您可以使用从进程获得的输出流,将用户的输入通过管道传输到进程。