Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Run commands in a Java class

 
Saleh Mohed
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to run a program by command in a Java class. This program has a specific commands which I would like to execute in a Java program. Running this program in the command line would be as follow:

[user@mycomputer ~] xx
xx> draw(1,2)
....
...
...
xx>end()
[user@mycomputer ~]

I tried to do the following:



but it gave me the following error:



It seems that what this code did is equivalent to the following in the command line:
[user@mycomputer ~] xx
[user@mycomputer ~] draw(1,2)

It is supposed that draw(1,2) is executed under the program xx.

Any help would be appreciated.
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Saleh and welcome to the Ranch!

Read all about it here:
When Runtime.exec() won't

Make sure to read all the sections and implement all the recommendations!

edit And please BeForthrightWhenCrossPostingToOtherSites
http://www.java-forums.org/advanced-java/44683-run-commands-java-class.html
 
Carey Brown
Ranch Hand
Pie
Posts: 1317
15
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm assuming you have a program named "xx" that you can manually run from a command line. I also assume from your description that draw(1,2) is supposed to be handled within the xx program. If this is the case you'll have to pass "draw(1,2)" as an argument to the "xx" program and make sure that xx knows how to handle an argument of draw(1,2). You should be able to test xx from the command line buy running something like:
xx "draw(1,2)"
P.S. the quotes around the arguments may be necessary in order to avoid having the command-line interpreter from doing something funny with any special characters.
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried writing the commands (including line breaks) to the Process' output stream as returned by getOutputStream()? Don't forget to flush after sending a command, or it may not work immediately.
 
John Vorwald
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like xx is a program, and draw(1,2) is a command inside of program xx.
The Java program is to telling the operating system to run the command "xx" and then run the command "draw(1,2)".

What you should try is to see if program xx can be ran using a redirect of the input. You need to look up the redirect for your operating system, but "<<" may work. Put the command "draw(1,2)", and other commands, into a file, "commands.txt", with one command on each line. Then try the command "xx << commands.txt". If this works, then your program needs to write the "commands.txt" file, and then execute program xx with the redirect.



 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<< won't work inside Runtime.exec or ProcessBuilder, as that's a shell mechanism for redirecting data to a process. It's similar to writing to the Process' getOutputStream() return value.
 
John Vorwald
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for indicating that the proposed solution will not work.

It may still be possible to use redirect, by using Runtime.exe(String []), see refs

http://www.ensta-paristech.fr/~diam/java/online/io/javazine.html
http://stackoverflow.com/questions/2563337/how-to-redirect-stdin-to-java-runtime-exec

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic