Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to IO console through MyApplication?

 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i m trying to implement a very small parser which will work like this
when ever user entered then my app will bring up a console where user can enter his input
i thought Console class can be helpful but javadoc says you can use it if VM has a console and when i used it it says VM don't have console and returns null
Runtime & ProcessBuilder both turn out be less flexible, and most of the time hangs up while i have read the java worlds trap but still i guess it is designed for just lunching some of the external programs and executing OS specific commands because there inputs are much small and will fit in the streams buffer size
however can any one has solution to my problem ?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't run your program from an IDE
 
Charles Hargrave
Greenhorn
Posts: 11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


Just an observation, but that seems like a lot of text to expect a user to type in correctly to make your program do something.

It sounds like you want your Java program to start up some other program, then have your Java program pass the user's input (commands) to the other program while it is running? If that is what you meant, you need to get that Process instance's output stream (see ProcessBuilder.getOutputStream(); it'll connect to the STDIN of the other program) and write the user's commands to it. If you want to feed in the contents of a file to that program's STDIN, in Java 1.7, look at ProcessBuilder's redirectInput(File file) method.

You might not need to bring up another console window; you could use the same one that the Java program is using but you'll need to put in code to know when to quit sending input to the other program. If the other program is simple enough that it can use command line parameters to tell it what to do, I would go with that approach. With the command line parameter approach, you can check all of the parameters before you try to execute the other program.

Without knowing any details about what the program is supposed to do, I can't make any other recommendations for fear of misdirecting you. Good luck to you.

CNH
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles Hargrave wrote:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


Just an observation, but that seems like a lot of text to expect a user to type in correctly to make your program do something.

It sounds like you want your Java program to start up some other program, then have your Java program pass the user's input (commands) to the other program while it is running? If that is what you meant, you need to get that Process instance's output stream (see ProcessBuilder.getOutputStream(); it'll connect to the STDIN of the other program) and write the user's commands to it. If you want to feed in the contents of a file to that program's STDIN, in Java 1.7, look at ProcessBuilder's redirectInput(File file) method.

You might not need to bring up another console window; you could use the same one that the Java program is using but you'll need to put in code to know when to quit sending input to the other program. If the other program is simple enough that it can use command line parameters to tell it what to do, I would go with that approach. With the command line parameter approach, you can check all of the parameters before you try to execute the other program.

Without knowing any details about what the program is supposed to do, I can't make any other recommendations for fear of misdirecting you. Good luck to you.

CNH

thanks for the help charles..
what i m doing is quite simple but my problem is process gets hanged
let me rephrase it to you , i have a file (HelloWorld.java) this file has two statements first and second accept user input and echo that out using
so when i m executing that file through Runtime.exe() , i m getting the input ie
enter msg
but when i m entering the msg the process gets hanged up or even deadlock
so i m searching solution for that , let me give you the code so that you can understand what is going on and can even isolate problem ?
 
Charles Hargrave
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naved momin wrote:
thanks for the help charles..
what i m doing is quite simple but my problem is process gets hanged
let me rephrase it to you , i have a file (HelloWorld.java) this file has two statements first and second accept user input and echo that out using
so when i m executing that file through Runtime.exe() , i m getting the input ie
enter msg
but when i m entering the msg the process gets hanged up or even deadlock
so i m searching solution for that , let me give you the code so that you can understand what is going on and can even isolate problem ?


Hello.

You're making Java run your program in an 'interactive' way as if someone is running FirstApp and entering commands directly. You have my apologies; I forgot to mention one other thing you needed to do when you make your program run this way.

Look at your Gobbler class's run() method at line 65 from your previous post. You need to add something to put in a newline (ENTER key / carriage return) after the command but before the flush() call at line 66. This is because your FirstApp is reading the showInputDialog String's value which does NOT have a newline (ENTER key / carriage return). It's like someone typed the answer but never hit the ENTER key, so the Process deadlocks.

Here's the only change I made to your code (at line 2 below):



I hope that helps,
CNH
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic