Execute a python separate application in a java application
Question: I have a small java project which do several activities including a database
update and i have a separate python(.py) application which update the same
database after a data mining process. In my Java application i need to
open(execute) my python application which update the same database. i tried
several ways but the code is not working. below i paste the initial code
without calling the .py file.

    
    
     if (getDB("BP") != 0.0 && getDB("BT") != 0.0 &&
         getDB("SO2") != 0.0 && getDB("HR") != 0.0) {
                 if(analyzeData(inputLine)) {
                    setDB(inputLine);
            //<python(.py) file should execute independently in this section, after setDB(inputLine)>
                 } } 
                 else { 
                      setNormalBio(inputLine); 
                      }
    


Answer: I do not see anything related to spawning a new process from your java code.
Did you really investigate this topic? If not: Do not expect the following
code to be the top-notch solution. Maybe dig deeper in
<http://docs.oracle.com/javase/6/docs/api/java/lang/ProcessBuilder.html> and
the python docs, as I remember, when I did this from PHP, I ran into some
problems with the python code itself.

  

Python (deadparrot.py):

    
    
    from os import chdir, name as osname
    from os.path import dirname, abspath
    
    def voom():
        if osname == 'posix':
            chdir(dirname(abspath(__file__)))
        return "Testing! This is your nine o'clock alarm call!"
    
    if __name__ == '__main__':
        print voom()
        exit(0)
    

I am not sure with the chdir-call. Once got a project, in which I needed to
call python in a linux webserver environment with php (hence the 'posix'), and
doing stuff with relative pathes in the python-script. IIRC I didn't need it
while testing on my local windows-environment.  
  
Edit 1: I tested it in Windows 7. It looks like neither pb.directory(...) or
chdir(...) is needed, to call relativ pathes from the python script. IDK about
linux.  
  
About the '**__main__** ' condition. I am using this, because this way I still
can call the script from the java-app (exit(0) returns an ok in unix-
terminology) and in addition use the voom() function from another python-
script, like so:

    
    
    from deadparrot import voom
    
    voom()
    

  

Java:

    
    
    package test;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class TestJavaAppClean {
    
        public static void main(String[] args) {
            ProcessBuilder pb = null;
            Process p = null;
            BufferedReader br = null;
    
            try {
                String top = "C:\\Users\\MaxMueller\\Documents\\test\\";
                String cmd = "C:\\Python27\\python.exe";
                String arg = "deadparrot.py";
                pb = new ProcessBuilder(new String[]{cmd, top + arg});
                pb.directory(new File(top));
                p = pb.start();
                br = new BufferedReader(new InputStreamReader(p.getInputStream()));
                String line;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (Exception ex) {
    
            } finally {
                try {
                    if (br != null) {
                        br.close();
                    }
                } catch (IOException ex) {
                }
                if (p != null) {
                    p.destroy();
                }
            }
        }
    }
    

