469,343 Members | 5,388 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,343 developers. It's quick & easy.

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
Jul 23 '08 #1
15 5945
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
Jul 23 '08 #2
BigDaddyLH
1,216 Expert 1GB
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?
Jul 23 '08 #3
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.....
Jul 24 '08 #4
r035198x
13,262 8TB
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.
Jul 24 '08 #5
BigDaddyLH
1,216 Expert 1GB
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

Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. java GUIApp | java ConsoleApp
Jul 24 '08 #6
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
Jul 24 '08 #7
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
Jul 24 '08 #8
BigDaddyLH
1,216 Expert 1GB
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?
Jul 24 '08 #9
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
Jul 24 '08 #10
BigDaddyLH
1,216 Expert 1GB
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?
Jul 24 '08 #11
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.
Jul 24 '08 #12
Runtime.getRuntim().exec("enter 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?
Jul 24 '08 #13
r035198x
13,262 8TB
..
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.
Jul 25 '08 #14
chaarmann
785 Expert 512MB
Runtime.getRuntim().exec("enter 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 contradictionary. 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("macrorecorder.exe generatedMacroFile");

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

Expand|Select|Wrap|Line Numbers
  1. Process p = Runtime.getRuntime().exec("myprogram.exe argument1 argument2");
  2. InputStream i = p.getInputStream();
  3. InputStream e = p.getErrorStream();
  4. 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:
Expand|Select|Wrap|Line Numbers
  1. ps = new PrintStream(o);        
  2. ps.println("myCommand");
  3. ps.close();
Jul 25 '08 #15
Hi, I know this thread is very old, but I am having a similar problem: I have a program written in Ruby which converts EDI files to XML files. This program can be run from the console, giving an .edi file as an argument, as follows:

edi2xml.rb in.edi

The resulting xml translation appears in the console. Well, I tried to call this same Ruby program from a Java class:
Expand|Select|Wrap|Line Numbers
  1. Runtime runtime = Runtime.getRuntime();
  2. Process process = runtime.exec("edi2xml.rb in.edi");
and I get the same error each time:

Expand|Select|Wrap|Line Numbers
  1. java.io.IOException: Cannot run program "edi2xml.rb": java.io.IOException: error=2, No such file or directory
This means that the process cannot find my ruby file. Well, how do I call this program? Please note that I added edi2xml.rb to my class path.

Thanks for your time!
Jun 25 '10 #16

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by Lem | last post: by
13 posts views Thread by BlackHawke | last post: by
114 posts views Thread by Maurice LING | last post: by
28 posts views Thread by liorm | last post: by
26 posts views Thread by Christoph Zwerschke | last post: by
16 posts views Thread by Amir Michail | last post: by
3 posts views Thread by gisto moss | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.