• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Really confused about runtime.exec()

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi' i'm really confuse about this.
is there anyway to call windows commandprompt from java code?

i read the article http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

The example on that article only explain about displaying the output. it doesn't explain what if the output need some input from user. cos i try it and what i got is my computer freeze.

how to execute another java class file from java code?

thanks a lot...

i hope my question easy to understand...
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at the examples e89, e90 and e91 here. They show how to send input to a process and how to read its output.

You can run Java processes just like you would do using a command line, something like "java -classpath C:\\myApp\\myApp.jar myapp.Main".
 
Grim DeFoe
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi' i already look at that.But when i try it my program still hang.can you help me...
this my code (GoodWindowsExec.java)


this file that i want to execute (maintest.java) from GoodWindowsExec,just for sample.


why every time i run GoodWindowsExec it still hang.

thanks a lot...
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two major issues here. One is that you're reading data from the program's stdout and stderr, but you're not sending anything to its standard input. When the program asks for input, your program needs to supply it -- the launched program can't read from the keyboard directly. Look at those examples again, especially e91.

The second major issue is that you've written your program to first read everything from the program's stdout, and then everything from stderr; you can't do this in a real program, as in general output can appear at both of these at the same time, interspersed from input requests. You generally need to use multiple threads, so that all of these things can go on at once.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your maintest is waiting for input from stdin. If you write to the process.getOutputStream() the data you send will go to the maintest stdin. You may need to flush() the output stream to make things work right, and then close it so maintest will see end-of-file.

You may also have to put the code in your display method into new threads so you can read maintest's output at the same time you are writing to maintest's stdin.
 
Grim DeFoe
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>>> but you're not sending anything to its standard input. When the program asks for input, your program needs to supply it -- the launched program can't read from the keyboard directly.

if the launched program can't read from the keyboard directly so this is not independent process, right? is there anyway to make it independent process? cos what i really need the launched program can run in independent,just like we execute java from windows commandpromt.

How to make we execute in independent process??

Thanks a lot...
 
Grim DeFoe
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>>>> You may also have to put the code in your display method into new threads so you can read maintest's output at the same time you are writing to maintest's stdin.

Hi' i try to create like this :


this is works, but input not come from user/execute class?
How to make the input come from user/execute class? (not from GoodWindowsExec)

thanks a lot...
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you're asking how to get user input into the program you're starting through exec? You can make yet another thread that reads from the keyboard and writes to the process output stream. So GoodWindows looks more like this:

Can you picture StreamGobbler reading from the first argument and writing to the second until eof? I used join on the three threads because once when I tried this the readers ran for a while after process.waitFor() came back.

PS: Strictly for convenience this implies StreamGobbler extends Thread. It's cleaner to implement Runnable instead; that adds a few variables and lines to the example.

PPS: Google for StreamGobbler. I don't know where this name came from but there are numerous implementations around. Don't blindly copy any one of them until you see how they are different and figure out why.
 
Grim DeFoe
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi' i already create thread like that, i change my code into like this :

This is outputer code :

i'am using this class to read input. My problem now why the output become like this :

how to synchronized it? and i don't know how to handle the input if the executed program have more than 1 input request?

thanks....

PS : i try to join() all the thread but the output still same. Try to input word not in the first line.

This is code how i read the stdout and stderr.(like StreamGobbler)

[ November 22, 2006: Message edited by: Grim DeFoe ]
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Checking to see if I'm keeping up with you ... you have it working to send one line that the user enters from the console to the process stdin and get a response back. The next challenge is to hold a conversation: send one line, get a response, send another line, get a response. Is that close?

If we want the program to hold this conversation instead of the user at the console, we can lose the thread that reads console and writes to the process stdin.

Instead the thread that reads the process stdout can just run the conversation.

We have to know when we've read the whole response. If it's one line we have no problems. If it can be 1..n lines we might have the process send some end-of-reply marker like an empty line or some special word.

I worked with another language called EASEL (or ESL) for which this was the main way to integrate with modules written in other languages. Their database support was: write SQL to process.stdin, read result sets from process.stdout.

Is that going the right direction for you?
 
Grim DeFoe
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi' thanks for the answer and keeping up with me.

i think you're right,what i need now is how to make my program start with the correct conversation.

But in previous post, you post something like this : "You may also have to put the code in your display method into new threads so you can read maintest's output at the same time you are writing to maintest's stdin."

How about that post?

>> "If it can be 1..n lines we might have the process send some end-of-reply marker like an empty line or some special word."

what about Eclipse, Eclipse can capture all the output and interact correctly with user and seems that eclipse doesn't need some special word too?

i Try to build my own java editor, in my editor user can compile and execute code. i need to solve this, is there any possible way to implement something like this?

Thanks a lot..
 
And then the flying monkeys attacked. My only defense was this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic