1 2 Previous Next 15 Replies Latest reply: Apr 21, 2012 3:42 AM by DarrylBurke RSS

    dos cli in java

    932142
      Greetings,
      I am told that I have a tendency to provide too much information, so I will try not to. I have search these forums and the internet in general for what I am looking for and have not found it. I am looking for a dos cli in a java application.This will be part of a larger program. I do not want to emulate dos, nor do I want java to run some commands for me, nor do I want to open the program written in C++ that is distributed with widows (cmd.exe). I want to have the cli in java as an object. It should be able to run any thing that a regular command prompt is able to. Has anyone made or seen some code like this? Is it possible? If possible could someone provide some source code that would atleast get me started?
        • 1. Re: dos cli in java
          baftos
          To better understand what you are looking for, can you give some examples of methods that you would expect to find in such a class, eventually with explanations of how they should behave? Especially how they should be different from what you can do with ProcessBuiilder.start()/Runtime.exec()?
          • 2. Re: dos cli in java
            932142
            Greetings,
            I would only be looking for one public method maybe three if it were complex. The first method would be the name of the class, lets say cli, that would initalize the the object. The second could be some kind of input for scripting but that would be optional. The third would be a large text output so it could be saved to a file or such, but also optional. The main purpose of this class and method set would be to provide the user an interface with the dos prompt through the object. The ProcessBuiilder.start()/Runtime.exec() is nice if I wanted to script out the commands but my goal is to provide the user the ability to type anything into the prompt and have it processed as if it were the windows program.
            • 3. Re: dos cli in java
              DrClap
              Despite the fact that you said
              I do not want to emulate dos
              it seems to me that your description is a pretty accurate description of something which emulates DOS.

              So if you're looking for some Java code which emulates DOS, I don't think you're going to find anything like that. For one thing almost nobody uses the Windows command line any more, and the people who do use it -- why would they need to run a Java program which just does the same thing?

              But probably I've misunderstood your aims. Perhaps your proposed class would do something else as well as just emulating the command line?
              • 4. Re: dos cli in java
                sabre150
                Mike Goff wrote:
                The ProcessBuiilder.start()/Runtime.exec() is nice if I wanted to script out the commands but my goal is to provide the user the ability to type anything into the prompt and
                have it processed as if it were the windows program.
                Err ... you get ProcessBuilder to execute 'bash -i' or 'cmd.exe' or what ever shell you have available for you platform. You then present the user's commands to the Process stdin and write the Process stdout to whatever display you want.
                • 5. Re: dos cli in java
                  932142
                  By saying emulate, that to me means something that looks like the the real thing but has no real connection to the real thing. What I want is literary the same thing as a dos prompt that can run any command that the user would use. I am a network engineer in a test lab and I use lots of command prompts. Currently I have 12 command prompts open and 3 more ssh windows. I have 5 laptops and I am using dos on all of them. The people that I work with also have dos prompts open so they can run scripts that I have written. Dos is not dead yet. The reason I want dos in java is so that I can organize my windows better. I want this to be part of a larger application but this is the first big hurdle.
                  • 6. Re: dos cli in java
                    baftos
                    If I understand well, you want to provide a Java class, call it DosCli that would open a window and work exactly like cmd.exe. A program that uses this class may have a menu item "Dos Command Prompt" and when you click it, it's just like clicking on cmd.exe. You don't want to launch cmd.exe itself because it's a different process and maybe the program using DocCli wants to be aware of what happens in that window. I think you will not find an already cooked one, but it's totally feasable. It's like rewriting cmd.exe in java. It does not exist probably in Windows, because nobody thought it's useful. But I know it exists in Android, which does not have a native console and it is written in the Androidish dialect of Java. It's a Java implementation of a Unix shell, but for Windows it should be simillar. The main building blocks would be a rather simple Swing text component+ProcessBuilder stuff.
                    • 7. Re: dos cli in java
                      796440
                      Mike Goff wrote:
                      By saying emulate, that to me means something that looks like the the real thing but has no real connection to the real thing. What I want is literary the same thing as a dos prompt that can run any command that the user would use.
                      So, you want somebody to double-click a jar or invoke something like
                      java Mike_Goff_CLI
                      into a comman prompt, and that brings up a window where you can type in, for example,
                      format D:
                      And that will format the D drive. Et cetera.

                      Why? What will be the point in having a Java app that mimics a cmd window?

                      And why the restriction against using native code or invoking an outside process?

                      Some of the things that you can do in a Windows cmd terminal cannot be done in pure Java. And at least some of the commands available do invoke other executables, even when done from within a cmd window.
                      • 8. Re: dos cli in java
                        jschellSomeoneStoleMyAlias
                        Mike Goff wrote:
                        ... the same thing as a dos prompt that can run any command that the user would use....
                        Process Builder then.
                        ...3 more ssh windows.
                        Then you must always use ssh regardless of how you wrap it. Or your target must have something different.
                        I want dos in java is so that I can organize my windows better.
                        Pretty sure that there are apps that do that. But presumably you want your own app so..
                        1. Wrap Process Builder for local access.
                        2. Wrap a SSH client for ssh shells
                        3. Create a GUI that provides a window for each instance of 1 and 2.

                        As a random thought maybe you should research SNMP (Simple Network Management Protocol) and EMS (Element Management System) and NMS (Network Management System).
                        • 9. Re: dos cli in java
                          DrClap
                          Such a thing wouldn't be easy to write. For example when the user types the command "cd hamburger" into your shell emulator, that has to change the current working directory in the emulator. Likewise when the user types the command "set hamburger=free" into your shell emulator, that has to update the list of environment variables which your emulator is keeping track of. Which it will have initialized by copying the list of environment variables from the environment when it started up.

                          All of that is pretty trivial. But when the user types the name of an executable, and you track that executable down using your emulator's version of the PATH environment variable, you have to make sure that when you run it, it runs in an environment containing your shell's version of the environment variables. And you're going to have to have a batch-file interpreter which works the same as the existing Windows one. Note that you can't just run a batch file in a different process because a batch file can change the environment variables of the CLI it runs in (i.e. in your emulator).

                          I'm sure the list goes on. And as already said, why would anybody go to all this effort just to duplicate the Windows shell? Okay, I can see somebody doing it for fun, but would they get all of the gotchas covered?
                          • 10. Re: dos cli in java
                            932142
                            "Why? What will be the point in having a Java app that mimics a cmd window?"

                            I have many command prompts open currently. Organizing them on the screen can be a problem. At one time people had to have web pages in one window and now we have tabs.

                            "And why the restriction against using native code or invoking an outside process?"
                            "Some of the things that you can do in a Windows cmd terminal cannot be done in pure Java. And at least some of the commands available do invoke other executables, even when done from within a cmd window."

                            This class would allow me to have access to the command prompt but have multiple tabs. I need to be able to run anything that dos can run, and yes that means running other programs.

                            For additional information I use a 3d modeling software package called Blender. The blender user can subdivide the portions of the window as many times as desired. The portions of the window can be configured to be an object of various types. A list of types that I was planning to implement are; DOS prompt, Calculator, Text / RTF editor, SSH client, picutre viewer, File browser and maybe a few more things. All of those items are useful to someone in my profession and may be opened multiple times.
                            • 11. Re: dos cli in java
                              796440
                              Mike Goff wrote:
                              "Why? What will be the point in having a Java app that mimics a cmd window?"

                              I have many command prompts open currently. Organizing them on the screen can be a problem. At one time people had to have web pages in one window and now we have tabs.
                              Okay, fair enough.

                              >
                              "And why the restriction against using native code or invoking an outside process?"
                              "Some of the things that you can do in a Windows cmd terminal cannot be done in pure Java. And at least some of the commands available do invoke other executables, even when done from within a cmd window."

                              This class would allow me to have access to the command prompt but have multiple tabs. I need to be able to run anything that dos can run, and yes that means running other programs.
                              Okay, so you're removing the constraint in your first post that "nor do I want java to run some commands for me."
                              • 12. Re: dos cli in java
                                932142
                                It looks like ProcessBuiilder.start()/Runtime.exec() is the way to go. I would like to tank all of the people who have assisted in this project.
                                • 13. Re: dos cli in java
                                  932142
                                  I should say "thank"
                                  • 14. Re: dos cli in java
                                    796440
                                    Mike Goff wrote:
                                    I should say "thank"
                                    No, if you wanna get me tanked, that's fine too! :-)
                                    1 2 Previous Next