0

I've searched for similar questions, but despite there where a lot with a similar title all have asked something different.

In my case I want that my java program interact with the terminal (Windows) by writing command and reading the response.

I've found the class ProcessBuilder, but does not seem that do what I want

What I' m searching for is something so:

  1. start the cmd at some given position (like C:\Users\federico)
  2. issue a command (dir or cd desktop); this should not open a command prompt window
  3. read any output that command may result in

and so on, up to the user's exit from the program.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
fedi98
  • 1
  • 2
  • 2
    This is what a batch file is for. Where does Java's capabilities play into all of this. – Nick Ziebert Apr 02 '17 at 21:45
  • 3
    You want to "digit" a command? What does that mean? – ajb Apr 02 '17 at 21:48
  • @NickZiebert i do not want only to execute a cmd command but olse read what cmd answered (ex: i put in my batch these instruction: 1: cd C:/Users/federico 2:dir. What i want is that my program read which are the subdirectory, i do not want that a cmd is open with that cubDirectory. – fedi98 Apr 02 '17 at 22:02
  • @ajb i want that the user can write in a text commands and my program execute that commands on cmd and print the result in another textfields: I want to do what i normally do on cmd, easily running my program – fedi98 Apr 02 '17 at 22:08
  • It sounds like you want to write a program that works like a Command Prompt window? This won't be easy. I think that most of the commands can be handled with `ProcessBuilder`, but you'll need to handle special commands like `cd` yourself. You'll need to parse it, figure out what to set the current directory to, and set up future `ProcessBuilder` processes so that they start in the new current directory. There will probably be other commands that your program will have to handle specially. Voting to close as "too broad" because this is too big a question to answer here. – ajb Apr 02 '17 at 22:27
  • @fedi98 http://stackoverflow.com/questions/5694385/getting-the-filenames-of-all-files-in-a-folder is all I got. – Nick Ziebert Apr 02 '17 at 22:44

1 Answers1

1

I suspect you simply don't know the English terms for this: what you're looking for is a way to execute commands in the OS, which is called "exec" in every programming language I know of, including Java: https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec(java.lang.String)

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
  • I don't think this is right. He mentions `cd desktop` as one of the commands he'd like to give the user, and you can't simply exec that. Sounds to me like he wants to write his own equivalent to cmd.exe. – ajb Apr 02 '17 at 22:22
  • quite, but issuing the command "cd desktop" doesn't do anything on its own, and is *not* the same as "changing the current working directory", so in lieu of additional details, the question "how do I run a command" is still "you use exec" – Mike 'Pomax' Kamermans Apr 03 '17 at 00:13
  • But he didn't ask "how do I run a command". – ajb Apr 03 '17 at 00:36
  • No, he (or she) used a few more words: "I want that my java program interact with the terminal(windows) by writing command and reading the response" is the description of every `exec` out there. – Mike 'Pomax' Kamermans Apr 03 '17 at 04:51
  • Sorry, but I interpret it differently. And we're both just guessing, so we'll have to agree to disagree. – ajb Apr 03 '17 at 05:50
  • @Mike'Pomax'Kamermans you are right. I knew the method exec but i always thought that it does something different. I searched and i discovered this: Process process = Runtime.getRuntime().exec("cmd /c dir", null, new File("C:\\Users\\federico")); and this, up to now, work great for me. thank you – fedi98 Apr 03 '17 at 10:40
  • @ajb, i meant that the user can walk through directories. i handled this doing Runtime.getRuntime().exec("cmd /c dir", null, new File("C:\\Users\\federico\\Desktop") when the user writes cd Desktop (i control which is the input, and if the command is cd, i change the working directory) – fedi98 Apr 03 '17 at 10:46