41907

How to send EOF to a process in Java?

Question:

I want to run groff in a Java program. The input comes from a string. In real command line, we will terminate the input by ^D in Linux/Mac. So how to send this terminator in Java program?

String usage += ".Dd \\[year]\n"+ ".Dt test 1\n"+ ".Os\n"+ ".Sh test\n"+ "^D\n"; // <--- EOF here? Process groff = Runtime.getRuntime().exec("groff -mandoc -T ascii -"); groff.getOutputStream().write(usage.getBytes()); byte[] buffer = new byte[1024]; groff.getInputStream().read(buffer); String s = new String(buffer); System.out.println(s);

Or any other idea?

Answer1:

^D isn't a character; it's a command interpreted by your shell telling it to close the stream to the process (thus the process receives EOF on stdin).

You need to do the same in your code; flush and close the OutputStream:

String usage = ".Dd \\[year]\n" + ".Dt test 1\n" + ".Os\n" + ".Sh test\n"; ... OutputStream out = groff.getOutputStream(); out.write(usage.getBytes()); out.close(); ...

Answer2:

I wrote this utility method:

public static String pipe(String str, String command2) throws IOException, InterruptedException { Process p2 = Runtime.getRuntime().exec(command2); OutputStream out = p2.getOutputStream(); out.write(str.getBytes()); out.close(); p2.waitFor(); BufferedReader reader = new BufferedReader(new InputStreamReader(p2.getInputStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } return sb.toString(); }

Recommend

  • How does given keyword work in Scala 3 or dotty?
  • How does one pickle arbitrary pytorch models that use lambda functions?
  • Input field showing different size in iOS
  • Service Fabric - How to reserve or protect my hardcoded Port
  • Java - localhost root folder?
  • Outlook.Store.GetDefaultFolder Outlook is creating strange folders in a new .pst file
  • Is there a way to continuously pipe data from Azure Blob into BigQuery?
  • Do not save model on duplicate file - Django 2
  • How to handle extraction of double type numbers from a complex input (string) [duplicate]
  • Difference between Canvas and Grid to Move, Resize, Rotate UIElements
  • How to store model in `.pb` file with Estimator in TensorFlow?
  • Synchronize two vobs on two different hosts
  • jquery: Disable/Enable button not working after reset
  • How deleteLater() actually works in qt?
  • How to read config files with section in bash shell
  • Python: Why am I getting a UnicodeDecodeError?
  • Identify xml text elements with Schematron
  • Http Requests not getting routed to Https NodeJs
  • NextJS auth with an external server
  • Heroku Git Push Master Error
  • android 3G signal strength
  • How can I access the Google account user_id?
  • Vue.js 2: Vue cannot find files from /assets folder (v-for)
  • integrity constraint violation: NOT NULL check constraint
  • Jenkins sending notifications to the wrong commit id
  • Auto send email based on the time and email address in database
  • Defer unused CSS
  • Symfony - Setting Cookie onKernelRequest
  • Add checkbox dynamically using angular 2
  • Ajax call on Multiple selection in Select box
  • When to use the tag in the head and body section of a html page? [duplicate]
  • Floating parent div grows to hypothetical width of floating child div
  • Using redis as an LRU cache for postgres
  • VSTS work items list through REST API
  • How to decleare char *const argv[] in swift [duplicate]
  • How to use FirstOrDefault inside Include
  • PHP Permalinks.. how to change?
  • Running R's aov() mixed effects model from Python using rpy2
  • ReferenceError: TextEncoder is not defined
  • XSLT Transformation to validate rules in XML document