Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
Search Coderanch
Advance search
Google search
Register / Login
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
Jeanne Boyarsky
Ron McLeod
Liutauras Vilda
Paul Clapham
Sheriffs:
paul wheaton
Tim Cooke
Henry Wong
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Piet Souris
Bartenders:
Mike London
Forum:
Java in General
read stdout of a commandline prog
Ido Daisuke
Greenhorn
Posts: 1
I like...
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
For example, if I do this:
$ echo "foo\nbar\nbaz"
How to get the
java
prog to read these lines?
Luigi Plinge
Ranch Hand
Posts: 441
I like...
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I'm by no means an expert on this, and it's probably not the best way, but how about getting your command line prog to call "java MyClass foo\nbar\nbaz" then deal with them as command line arguments?
Paul Clapham
Marshal
Posts: 27534
88
I like...
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You pipe them to the stdin of your Java class:
$ echo "foo\nbar\nbaz" | java yourClass
and write your class to read the data from System.in.
Stephan van Hulst
Saloon Keeper
Posts: 14697
331
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Yes, this is more OS specific, and not really Java related. Assuming you are using Unix, in Windows you can pipe commands using | as well.
Srinivas Kalvala
Ranch Hand
Posts: 257
I like...
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Below class reads the input from standard input stream. Please let me know if it is not what you are looking for ...
package com.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class CmdTest { public static void main(String[] args) { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(System.in)); System.out.println("Reading data .... "); String data; try { while((data = bufferedReader.readLine()) != null){ System.out.println(data); } bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } }
Test
:
echo 'Hello' | java -cp . com.test.CmdTest
Result:
Reading data ....
'Hello'
Campbell Ritchie
Marshal
Posts: 77245
371
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Welcome to the Ranch
This question is too difficult for "beginning" so I shall move it.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Screen Cap on Mouse Click Anywhere
Urgent Help.........
Java certication
Java certication
i nedd source code of networking prog of below quest
More...