1

I've written a utility in Java that filters JSON input, from a file or from stdin, to XML. Though I've made it a stand-alone, runnable JAR, I'm lost as to how to redirect input to it via stdin. For example:

cat sample.json | java -jar ./target/json-to-xml-1.0.0.jar

This command line doesn't work; I wonder if this can even be done.

 private static String readContentFromStdIn() throws IOException
 {
 char ch;
 StringBuilder sb = new StringBuilder();
 PipedInputStream input = new PipedInputStream();
 while( ( ch = ( char ) input.read() ) != -1 )
 sb.append( ch );
 input.close();
 return sb.toString();
 }
asked Dec 19, 2014 at 17:20
2
  • 1
    Do you have a case when your program does read from stdin successfully? Commented Dec 19, 2014 at 17:31
  • 1
    That unix command is correct, I guess your problem is in the code. How are you reading from stdin? System.in? (please paste the relevant code) Commented Dec 19, 2014 at 17:35

1 Answer 1

1

Your code is creating a PipedInputStream which is not connected to anything and will never get any data. For reading from stdin, you don't need to create a new stream; you should just read from System.in.

answered Dec 19, 2014 at 17:59
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! I adopted Scanner sc=new Scanner(System.in) to read from stdin and it now works.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.