简体   繁体   English

Runtime.getRuntime()。exec()不执行某些命令

[英]Runtime.getRuntime().exec() doesn't execute some commands

I'm beginner at java and have some problems. 我是Java的初学者,遇到了一些问题。 I've read several topics about this theme but none of them worked for me. 我已经阅读了有关此主题的多个主题,但没有一个对我有用。 Here is my code: 这是我的代码:

try 
{

        Console console = System.console();

        String command;

        while(true)
        {
            command = console.readLine("Enter input:");
            Process proc = Runtime.getRuntime().exec(command);

            // Read the output

            BufferedReader reader =  
                  new BufferedReader(new InputStreamReader(proc.getInputStream()));

            String line = "";
            while((line = reader.readLine()) != null) {
                System.out.print(line + "\n");
            }

            proc.waitFor(); 
        }



}
    catch(Exception e) {} 

So what I'm trying is to make a java program and run terminal commands in it(I'm using linux). 所以我想做的是制作一个Java程序并在其中运行终端命令(我正在使用linux)。 This program works with commands like "ls" "ps ef" and others but it doesn't work when I type "cd". 该程序可与“ ls”,“ ps ef”等命令一起使用,但是当我键入“ cd”时它不起作用。 I know that cd makes different process and should be used this way: "Runtime.exec(String command, String[] envp, File dir)". 我知道cd会产生不同的过程,应该以这种方式使用:“ Runtime.exec(String command,String [] envp,File dir)”。 My questions is: 我的问题是:
How to make my program run all kinds of terminal commands? 如何使我的程序运行各种终端命令? Sorry if question sound silly. 抱歉,问题听起来很傻。 Thank you. 谢谢。

您实际上必须运行要使用的控制台(例如,sh,csh,bash等),然后使用进程OutputStream来输入命令

I think the Problem is not your Code, the command is the problem... 我认为问题不是您的代码,命令是问题...

what do you want to see if your command is cd ?? 您想看什么,如果您的命令是cd?

In Background it changes the path but you get nothing back. 在“后台”中,它更改了路径,但您一无所获。

Changing the Directory is not processing any output. 更改目录不会处理任何输出。

The cd command is a shell built-in command. cd命令是Shell内置命令。 There is no shell when you run a command via exec(...) . 通过exec(...)运行命令时没有外壳。 Indeed, if you try to find a cd command in any of your system's bin directories, you won't find one ... because it is impossible to implement as a regular command. 确实,如果您尝试在系统的任何bin目录中找到cd命令,您将找不到...,因为不可能将其作为常规命令来实现。

If you are trying to use cd to change the current directory for the JVM itself, that won't work because a command can only change the current directory of itself and (after that) commands that it launches itself. 如果尝试使用cd更改JVM本身的当前目录,则该命令将无效,因为命令只能更改其本身的当前目录,并且(此后)该命令将自行启动。 It can't change its parent processes current directory. 它不能更改其父进程的当前目录。

If you are trying to use cd to change the current directory for subsequent commands, that won't work either. 如果您尝试使用cd更改后续命令的当前目录,则该方法也不起作用。 The context in which you set the current directory ends when the command finishes. 命令完成后,设置当前目录的上下文结束。

In fact, the right way to change the directory for a command run using exec is to set it via the ProcessBuilder API itself. 实际上,更改使用exec运行的命令的目录的正确方法是通过ProcessBuilder API本身进行设置。


How to make my program run all kinds of terminal commands? 如何使我的程序运行各种终端命令?

You can't. 你不能 Some of the "terminal commands" only make sense as shell commands, and that means you need a shell. 某些“终端命令”仅作为shell命令有意义,这意味着您需要一个shell。

I suppose, you could consider emulating the required behaviour in your Java code. 我想,您可以考虑在Java代码中模拟所需的行为。 That would work for cd ... but other commands are likely to be more difficult to cope with. 这将对cd起作用...但是其他命令可能更难处理。

(For what it is worth, it is possible to implement a POSIX compatible shell in Java. It is just a LOT of work.) (就其价值而言,可以用Java实现POSIX兼容的外壳。这只是很多工作。)

这对我有用:

Runtime.getRuntime().exec(new String[]{ "/system/bin/sh", "-c", "ls -l" } );

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

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