Java GUI and typing console commands

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • beavis601
    New Member
    • Jul 2008
    • 13

    Java GUI and typing console commands

    I have sucessfully launched a jar file from a C program, but now i need to figure out how to actually type commands into that console. I tried using the robot class, but when the GUI window is up it will not type into the console window.......

    Any help would be greatly appreciated, thank you!

    -Doug
  • beavis601
    New Member
    • Jul 2008
    • 13

    #2
    I guess to hopefully clarify......If button1 is pressed i want to type in "year 2008" and hit return so that the program takes that as a command. Hope this clears it up.

    doug

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      Let me get this right, because it sounds completely crazy. You have a program with a GUI interface that want to simulate typing onto a console window?! Why?

      Comment

      • beavis601
        New Member
        • Jul 2008
        • 13

        #4
        Its for a program that was already written, and instead of having to type in the command "play" or "pause", they want me to have a GUI that if you press the play button it will have command passed.....

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by beavis601
          Its for a program that was already written, and instead of having to type in the command "play" or "pause", they want me to have a GUI that if you press the play button it will have command passed.....
          You thus need to write an event listener for your play and pause buttons.
          You can read about how to do that here.

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            Originally posted by beavis601
            Its for a program that was already written, and instead of having to type in the command "play" or "pause", they want me to have a GUI that if you press the play button it will have command passed.....
            So the earlier program is console based?

            You can solve this by piping the programs -- no need to use Robot, which would be a fragile and tedious solution. To pipe the output of program one as the input of program two, execute on the shell level

            Code:
            program1 | program2
            That's it! Notice this is a general solution, independent of Java. So if you want Java program GuiApp to send its System.out as the System.in of ConsoleApp, write:

            Code:
            java GUIApp | java ConsoleApp

            Comment

            • beavis601
              New Member
              • Jul 2008
              • 13

              #7
              I understand how to write GUI's and use actionlisteners ......The one thing I know how to code the java program to write in a command in C. Is there a way to actually have the command passed as

              C:\ year 2008

              or something very similar.

              Its my last week at work and I just really want to get this working, thank you so much for all the help!

              Doug

              Comment

              • beavis601
                New Member
                • Jul 2008
                • 13

                #8
                BIGDaddy

                Thank you! Piping the programs is a great idea, and seems to be a quite simple solution to my problem. Do you know how to actually pass a command in java though? I dont want to just write to the console, I want to actually have a command passed.

                Thanks for all the help!

                Doug

                Comment

                • BigDaddyLH
                  Recognized Expert Top Contributor
                  • Dec 2007
                  • 1216

                  #9
                  Originally posted by beavis601
                  BIGDaddy
                  Do you know how to actually pass a command in java though? I dont want to just write to the console, I want to actually have a command passed.
                  I don't understand what you mean by "pass a command". Can you give an example?

                  Comment

                  • beavis601
                    New Member
                    • Jul 2008
                    • 13

                    #10
                    for example if you open command prompt and you type "cd .." it performs change directory, or if you type "dir" it shows what in the current directory. I am working with a custom console, but I essentially want to be able to pass the same type of commands once a certain button is pushed.

                    I'm sorry this isn't very clear, I'm just a student and am at a summer job working on this project.


                    Doug

                    Comment

                    • BigDaddyLH
                      Recognized Expert Top Contributor
                      • Dec 2007
                      • 1216

                      #11
                      I have misgivings about what you are trying to do. Why not keep it simple and write a single program to do what ever it is you want to do?

                      Comment

                      • beavis601
                        New Member
                        • Jul 2008
                        • 13

                        #12
                        My program is used in a custom console. I have a UI that has certain buttons, and based off of those buttons I need to execute certain commands in the shell. If I can get a simple command to execute outside of the custom console with a basic java app, then executing any command inside the custom console should be easy.

                        Comment

                        • beavis601
                          New Member
                          • Jul 2008
                          • 13

                          #13
                          Runtime.getRunt im().exec("ente r command here);

                          Is there a way to have this command executed in the same process that I have running where i execute the jar file that this java application would be in?

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by beavis601
                            ..
                            Is there a way to have this command executed in the same process that I have running where i execute the jar file that this java application would be in?
                            Could you elaborate more on this question?
                            Remember Runtime.exec is used to invoke mostly native processes.

                            Comment

                            • chaarmann
                              Recognized Expert Contributor
                              • Nov 2007
                              • 785

                              #15
                              Originally posted by beavis601
                              Runtime.getRunt im().exec("ente r command here);

                              Is there a way to have this command executed in the same process that I have running where i execute the jar file that this java application would be in?
                              This is a java command! But you said in the beginning, you are writing a C-program that launches the Java program (jar-file) and not the other way around!
                              That is contradictionar y. Please clarify.

                              So what do you want to do now?
                              1.) writing a c program to launch and steer an already existing java program?
                              You could easily decompile the java-program (google for java decomiler) and change its source code (public static void main(String[] args)) to accept commands either from the command line or from a file or current input stream.
                              2.) writing a java program to launch and steer an already existing C-program?
                              You can use a windows macro recorder (google for macro recorder), start it, steer your C-program, stop it and then execute the generated macro with runtime.exec("m acrorecorder.ex e generatedMacroF ile");

                              But first, in any case, choose the easy way: figure out if the program supports
                              command line parameter (case 1) or reads/writes to the standard input/output stream (case 2). Most programs do. If you have a program that does not, you only have the option mentioned above.
                              Most people don't know that for example winRAR or internetExplore r accepts command lines arguments. I have successfully called these programs in my own java program to automatically compress directories or pop up a webpage.
                              You can make a quick test by typing into the windows-command-shell: "myprogram. exe /help" or "myprogram. exe -help" and see the output.
                              For case 2 you can make a quick test: write your commands into a text file and type into the windows-command-shell: "myprogram. exe < mycommands.txt" . If that works, then you can use following code snippet below to steer your program.

                              This is a java forum, so I will give you the java code here only. But the C-code would be very similar and use the same principle.

                              Code:
                              Process p = Runtime.getRuntime().exec("myprogram.exe argument1 argument2");
                              InputStream i = p.getInputStream();
                              InputStream e = p.getErrorStream();
                              OutputStream o = p.getOutputStream();
                              now you can just write your commands into the output stream and listening to the output or errors of your program from the input streams.

                              For example to issue a command:
                              Code:
                              ps = new PrintStream(o);		
                              ps.println("myCommand");
                              ps.close();

                              Comment

                              Working...