How to retrieve an output of a Python script using a Java program -


i having problem code. have java class calls python script gps data. problem can call script want return data string on java side can use in array later. want take data python script , bring java string , place in array.

python code

    #! /usr/bin/python      import os      gps import *      time import *      import time      import threading       gpsd = none #seting global variable       os.system('clear') #clear terminal (optional)       class gpspoller(threading.thread):       def __init__(self):       threading.thread.__init__(self)       global gpsd #bring in scope       gpsd = gps(mode=watch_enable) #starting stream of info       self.current_value = none       self.running = true #setting thread running true   def run(self):    global gpsd   while gpsp.running:   gpsd.next() #this continue loop , grab each set of gpsd info clear     buffer  if __name__ == '__main__':  gpsp = gpspoller() # create thread try:   gpsp.start() # start   while true:     #it may take second or 2 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 whatever  except (keyboardinterrupt, systemexit): #when press ctrl+c   print "\nkilling thread..."   gpsp.running = false   gpsp.join() # wait thread finish it's doing print "done.\nexiting." 

java code

                   process p4 = null;                   //string commands4="sudo python test1.py";                       // provision gpio pin #01 output pin , turn on                        try {                              p4 = runtime.getruntime().exec("sudo python test1.py");                          inputstream = p4.getinputstream();                          int = 0;                          while( (i = is.read() ) != -1) {                              system.out.print((char)i);                              lat                              break;                          } 

you might want check p4.exitcode() see if python script invoked, , if wasn't successful may check p4.geterrorstream() see why.

either way, code should handle different exit codes because may never 100% confident calling python script successful.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -