0

I'm beginner at java and have some problems. 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). This program works with commands like "ls" "ps ef" and others but it doesn't work when I type "cd". I know that cd makes different process and should be used this way: "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.

Ojs
  • 924
  • 1
  • 12
  • 26
  • 1
    One problem is: "cd" is not a command; it is a function of your shell! And when running a command from Java like you do; there is no shell! Thus it doesn't work! And one other thing: probably your project is really just about learning java concepts; but in general: allowing the user to provide any kind of command, and then just executing them ... is a very bad idea. – GhostCat May 18 '15 at 14:02
  • You may want to check this thread as well. https://stackoverflow.com/questions/31776546/why-does-runtime-execstring-work-for-some-but-not-all-commands/31776547 – emir Feb 11 '18 at 10:39

4 Answers4

0

you've actually got to run the console you want to use (ie sh, csh, bash, etc) and then use the process OutputStream to feed in commands

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • Isnt Console console = System.console(); this command by default making bash? – Ojs May 18 '15 at 14:00
  • No, that's not what it does. All this is is a reference to the console (if there is one) that ran your Java. You need to open another console, as I've advised – ControlAltDel May 18 '15 at 14:14
0

I think the Problem is not your Code, the command is the problem...

what do you want to see if your command is cd ??

In Background it changes the path but you get nothing back.

Changing the Directory is not processing any output.

Gordon
  • 69
  • 4
0

The cd command is a shell built-in command. There is no shell when you run a command via 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.

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. 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. 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.


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.

I suppose, you could consider emulating the required behaviour in your Java code. That would work for cd ... but other commands are likely to be more difficult to cope with.

(For what it is worth, it is possible to implement a POSIX compatible shell in Java. It is just a LOT of work.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • So there is no way to create program like ssh in java? Which executes every terminal command? – Ojs May 18 '15 at 14:26
  • Have you tried using `ssh` to run `cd`? Does it do anything useful? – Stephen C May 18 '15 at 15:05
  • Have you tried `exec("sh -c cd")`? Does it do anything useful? – Stephen C May 18 '15 at 15:06
  • As I said in my Answer, it is possible to implement a POSIX shell in Java. – Stephen C May 18 '15 at 15:07
  • Would it be easier to make this kind of program in C? – Ojs May 19 '15 at 09:43
  • Nope. You have more or less the same problems. – Stephen C May 19 '15 at 10:19
  • but how does the ssh(which is written in python) has remote access of shell? I mean does the programming language python has the ability to run shells built in commands? – Ojs May 19 '15 at 13:34
  • 1) the `ssh` command is not written in python. It is written in C. 2) `ssh` is running a shell script using a shell that is run on the remote machine. You can do the same thing with `exec(...)`. But executing individual shell commands like `cd` is pointless using `exec("sh -c cd")` because the shell is going to exit after running `cd`. – Stephen C May 19 '15 at 14:28
  • *"I mean does the programming language python has the ability to run shells built in commands?"* Yes, but the problems are the same as in Java using exec. – Stephen C May 19 '15 at 14:30
0

This worked for me:

Runtime.getRuntime().exec(new String[]{ "/system/bin/sh", "-c", "ls -l" } );
RonTLV
  • 2,376
  • 2
  • 24
  • 38