File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Run commands in a Java class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Run commands in a Java class" Watch "Run commands in a Java class" New topic

Run commands in a Java class

Saleh Mohed

Joined: May 28, 2011
Posts: 1
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)
[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

Joined: May 03, 2008
Posts: 5115

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

luck, db
There are no new questions, but there may be new answers.
Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 885

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.

Understanding the scope of the problem is the first step on the path to true panic
Rob Spoor

Joined: Oct 27, 2005
Posts: 20271

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.

How To Ask Questions How To Answer Questions
John Vorwald
Ranch Hand

Joined: Sep 26, 2010
Posts: 139
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

Joined: Oct 27, 2005
Posts: 20271

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

Joined: Sep 26, 2010
Posts: 139
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

I agree. Here's the link:
subject: Run commands in a Java class
It's not a secret anymore!