简体   繁体   中英

process Builder java cannot run some of the python code in java. How to solve?

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 在此处输入图片说明

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM