Search code examples
javaprocesscentosoverpass-api

java give a string to running process exe


I was tried to get OSM data from my local overpass API. There are four steps to get OSM data.

  1. run the binary file /srv/osm3s/bin/osm3s_query
  2. once the osm3s_query is running, you'll see this messageencoding remark: Please enter your query and terminate it with CTRL+D.
  3. input your query <query type="node"><bbox-query n="51.0" s="50.9" w="6.9" e="7.0"/><has-kv k="amenity" v="pub"/></query><print/>
  4. press ctrl+D and get the OSM results

my code as below:

try
    {            
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec("/srv/osm3s/bin/osm3s_query");
        InputStream stderr = proc.getErrorStream();
        InputStreamReader isr = new InputStreamReader(stderr);
        BufferedReader br = new BufferedReader(isr);
        String line = null;
        while ( (line = br.readLine()) != null)
            System.out.println(line);
        int exitVal = proc.waitFor();
        System.out.println("Process exitValue: " + exitVal);
    } catch (Throwable t)
      {
        t.printStackTrace();
      }

The process will hang on step 2 after shown the message encoding remark: Please enter your query and terminate it with CTRL+D.. I have no idea how to give the process the query string.

Anybody has some idea?


Solution

  • OutputStreamWriter output = proc.getOutputStream();
    output.write(yourQuery);
    output.write(4); // that's ctrl-d
    output.flush();