How to retrieve an output of a Python script using a Java program
Question: I am having a problem with my code. I have a java class that calls a python
script that get gps data. The problem is that I can call the script but I want
to return the data to a string on the java side so I can use it in an array
later. So basically all I want is to take the data from the python script and
bring it to java as a string and place it in to an array.

**python code**

    
    
        #! /usr/bin/python
         import os
         from gps import *
         from time import *
         import time
         import threading
    
         gpsd = None #seting the global variable
    
         os.system('clear') #clear the terminal (optional)
    
         class GpsPoller(threading.Thread):
          def __init__(self):
          threading.Thread.__init__(self)
          global gpsd #bring it in scope
          gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info
          self.current_value = None
          self.running = True #setting the thread running to true
    
     def run(self):
       global gpsd
      while gpsp.running:
      gpsd.next() #this will continue to loop and grab EACH set of gpsd info to clear the     buffer
    
    if __name__ == '__main__':
     gpsp = GpsPoller() # create the thread
    try:
      gpsp.start() # start it up
      while True:
        #It may take a second or two to get good data
        #print gpsd.fix.latitude,', ',gpsd.fix.longitude,'  Time: ',gpsd.utc
    
        os.system('clear')
    
        print
        print ' GPS reading'
        print '----------------------------------------'
        print 'latitude    ' , gpsd.fix.latitude
        print 'longitude   ' , gpsd.fix.longitude     
    
        time.sleep(5) #set to whatever
    
    except (KeyboardInterrupt, SystemExit): #when you press ctrl+c
      print "\nKilling Thread..."
      gpsp.running = False
      gpsp.join() # wait for the thread to finish what it's doing
    print "Done.\nExiting."
    

**java code**

    
    
                       Process p4 = null;
                      //String commands4="sudo python test1.py";
    
    
    
                       // provision gpio pin #01 as an output pin and turn on
    
    
    
                        try {
    
    
    
    
                             p4 = Runtime.getRuntime().exec("sudo python test1.py");
                             InputStream is = p4.getInputStream();
                             int i = 0;
                             while( (i = is.read() ) != -1) {
                                 System.out.print((char)i);
                                 lat
                                 break;
                             }
    


Answer: You might want to check for the `p4.exitCode()` to see if the python script
was successfully invoked, and then if it wasn't successful you may check for
`p4.getErrorStream()` to see why.

Either way, generally the code should handle different exit codes because you
may never be 100% confident that the calling to python script will always be
successful.

