process Builder java cannot run some of the python code in java. How to solve?
<p>I am using java to create ProcessBuilder to run python. </p>

<p>Both of the two py can be run sucessfully in the python program. (the two py have no issue with the code)</p>

<p>input.py:</p>

<pre><code>print 'hello'  
number=[3,5,2,0,6]  
print number  
number.sort()  
print number  
number.append(0)  
print number  
print number.count(0)  
print number.index(5)
</code></pre>

<p>TESTopenBaseOnt.py:</p>

<pre><code>from rdflib import URIRef, Graph, Namespace
from rdflib.plugins.parsers.notation3 import N3Parser
from rdflib.namespace import RDF, OWL, RDFS
from rdflib import URIRef, BNode, Literal
from rdflib import Namespace
from rdflib.namespace import RDF, FOAF, RDFS
from rdflib import Graph

gUpdate = Graph()
print ".&gt; Step....1"
gUpdate.parse("BBCOntology.rdf" )
print ".&gt; Step....2"
print gUpdate.serialize(format='xml')
print ".&gt;  Finished......."
#
</code></pre>

<p>AS you can see the picture. </p>

<p>The code works for python:input.py
However, it does not work for python:TESTopenBaseOnt.py<br>
It might be because java cannot run the parse function in python. as the result shows that, the program stoped at step1.</p>

<pre><code>public static void main(String [] args) throws IOException
    {

        try

        {
        ProcessBuilder pb = new ProcessBuilder("C:/Python27/python","C:Desktop//searchTestJava//input.py");

//      ProcessBuilder pb = new ProcessBuilder("C:/Python27/python","C:Desktop//searchTestJava//TESTopenBaseOnt.py");



        Process p = pb.start();
        BufferedReader bfr = new BufferedReader(new InputStreamReader(p.getInputStream()));

        System.out.println(".........start   process.........");  
        String line = "";     
        while ((line = bfr.readLine()) != null){
            System.out.println("Python Output: " + line);
        }

        System.out.println("........end   process.......");


        }catch(Exception e){System.out.println(e);}


    }
</code></pre>

<p>So how to solve the problem that the python cannot run in the java
<a href="http://i.stack.imgur.com/L0r4Z.png" rel="nofollow"><img src="http://i.stack.imgur.com/L0r4Z.png" alt="enter image description here"></a></p>

<p>Your script runs, but it does not reach "Step 2", so </p>

<pre><code>gUpdate.parse("BBCOntology.rdf" )
</code></pre>

<p>will be the source of the problem. Possibly it is because the file <code>BBCOntology.rdf</code> is not in the current working directory of the Python process. Or it could be that the Python process does not have permission to open that file.</p>

<p>It might be worth reading the error stream from the Python process and printing that out in Java. Use <code>p.getErrorStream()</code> in the same manner that you use <code>p.getInputStream()</code>.</p>

<p>Or, easier, add an exception handler to your Python code that catches and prints exception messages to standard out:</p>

<pre><code>import traceback

try:
    gUpdate = Graph()
    print ".&gt; Step....1"
    gUpdate.parse("BBCOntology.rdf" )
    print ".&gt; Step....2"
    print gUpdate.serialize(format='xml')
    print ".&gt;  Finished......."
except Exception as exc:
    traceback.print_exc()
    raise exc
</code></pre>

<p>Your Java process should then print the message, which might be informative.</p>

java python parsing rdf processbuilder