Reputation: 195
I am using java to create ProcessBuilder to run python.
Both of the two py can be run sucessfully in the python program. (the two py have no issue with the code)
input.py:
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)
TESTopenBaseOnt.py:
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 ".> Step....1"
gUpdate.parse("BBCOntology.rdf" )
print ".> Step....2"
print gUpdate.serialize(format='xml')
print ".> Finished......."
#
AS you can see the picture.
The code works for python:input.py
However, it does not work for python:TESTopenBaseOnt.py
It might be because java cannot run the parse function in python. as the result shows that, the program stoped at step1.
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);}
}
So how to solve the problem that the python cannot run in the java
Upvotes: 0
Views: 536
Reputation: 195
gUpdate = Graph()
print ".> Step....1"
gUpdate.parse("D:\\Desktop\\searchTestJava\\BBCOntology.rdf" )
print ".> Step....2"
The BBCOntology.rdf is in the current working directory of the Python process. So the program can work in python even if I wrote as (gUpdate.parse("BBCOntology.rdf" )).
However, java does not know the directory BBCOntology.rdf is same as the TESTopenBaseOnt.py. Once I add the gUpdate.parse("D:\Desktop\searchTestJava\BBCOntology.rdf" ) , Java can work.
Upvotes: 0
Reputation: 87074
Your script runs, but it does not reach "Step 2", so
gUpdate.parse("BBCOntology.rdf" )
will be the source of the problem. Possibly it is because the file BBCOntology.rdf
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.
It might be worth reading the error stream from the Python process and printing that out in Java. Use p.getErrorStream()
in the same manner that you use p.getInputStream()
.
Or, easier, add an exception handler to your Python code that catches and prints exception messages to standard out:
import traceback
try:
gUpdate = Graph()
print ".> Step....1"
gUpdate.parse("BBCOntology.rdf" )
print ".> Step....2"
print gUpdate.serialize(format='xml')
print ".> Finished......."
except Exception as exc:
traceback.print_exc()
raise exc
Your Java process should then print the message, which might be informative.
Upvotes: 2