简体   繁体   English

程序终止,没有任何错误

[英]Program terminates without any error

I am trying to run this Python program in Java. 我正在尝试用Java运行此Python程序。

Issue: When i run this program from Commandline: Python msp.py dennys-san-jose-2 - it WORKS 问题:当我运行命令行从这个程序:Python的msp.py丹尼斯-圣-何塞- 2 -它的工作原理

When I call the same script through this java program. 当我通过此java程序调用相同的脚本时。 It just terminates. 它只是终止。 I tested other python scripts and they work! 我测试了其他python脚本,它们起作用了!

public void pythonrun(String args) throws IOException, InterruptedException
{
    String pythonScriptPath = "/yelp/msp.py";
    String[] cmd = new String[2 + args.length()];
    cmd[0] = "C:\\Python27\\python.exe";
    cmd[1] = pythonScriptPath;
    for(int i = 0; i < args.length(); i++) {
    cmd[i+2] = args;
    }

    // create runtime to execute external command
    Runtime rt = Runtime.getRuntime();
    Process pr = rt.exec(cmd);
    //pr.waitFor();

    // retrieve output from python script
    BufferedReader bfr = new BufferedReader(new InputStreamReader(pr.getInputStream()));
    String line = "";
    while((line = bfr.readLine()) != null) {
    // display each output line form python script
    System.out.println(line);
    }
}

public static void main(String args[]) throws IOException, InterruptedException {
YelpPython demo = new YelpPython();
demo.pythonrun("dennys-san-jose-2");
}

Script (msp.py): 脚本(msp.py):

What it does? 它能做什么? (in short, the script goes to a page and scrapes the reviews) (简而言之,脚本转到页面并刮取评论)

from bs4 import BeautifulSoup
from urllib import urlopen
import sys
queries = 0
while queries <201:
    stringQ = sys.argv[1]
    page = urlopen('http://www.yelp.com/biz/' + stringQ)

    soup = BeautifulSoup(page)
    reviews = soup.findAll('p', attrs={'itemprop':'description'})
    authors = soup.findAll('span', attrs={'itemprop':'author'})

    flag = True
    indexOf = 1

    for review in reviews:
        dirtyEntry = str(review)
        while dirtyEntry.index('<') != -1:
            indexOf = dirtyEntry.index('<')
            endOf = dirtyEntry.index('>')
            if flag:
                dirtyEntry = dirtyEntry[endOf+1:]
                flag = False
            else:
                if(endOf+1 == len(dirtyEntry)):
                    cleanEntry = dirtyEntry[0:indexOf]
                    break
                else:
                    dirtyEntry = dirtyEntry[0:indexOf]+dirtyEntry[endOf+1:]
        f=open("reviews.txt", "a")
        f.write(cleanEntry)
        f.write("\n")
        f.close
    queries = queries + 40

Problem (In short): 问题(总而言之):

When I run this script through command line it works and it finally stores a reviews.txt file. 当我通过命令行运行此脚本时,它可以工作,并且最终存储了reviews.txt文件。 But when I run it through this program nothing happens. 但是当我通过该程序运行它时,什么也没有发生。

I have played with pr.wait() and pr.waitfor() and yet nothing happens. 我玩过pr.wait()和pr.waitfor(),但是什么也没发生。

Please advice. 请指教。

Thank you. 谢谢。

public void pythonrun(String args) throws IOException, InterruptedException {
    String pythonScriptPath = "/yelp/msp.py";
    String[] cmd = new String[2 + args.length()];
    cmd[0] = "C:\\Python27\\python.exe";
    cmd[1] = pythonScriptPath;
    for(int i = 0; i < args.length(); i++) {
        cmd[i+2] = args;
    }
    :
}

That doesn't look quite right. 看起来不太正确。 You're creating a string array with a size based on the size of the string being passed in. 您正在创建一个字符串数组,其大小取决于要传入的字符串的大小。

That means that pythonrun("1234") will end up executing: 这意味着pythonrun("1234")将最终执行:

C:\Python27\python.exe /yel/msp.py 1234 1234 1234 1234

If you just want to pass in a single argument for the script, you would do something like: 如果只想为脚本传递一个参数,则可以执行以下操作:

public void pythonrun(String args) throws IOException, InterruptedException {
    String pythonScriptPath = "/yelp/msp.py";
    String[] cmd = new String[3];
    cmd[0] = "C:\\Python27\\python.exe";
    cmd[1] = pythonScriptPath;
    cmd[2] = args;
    :
}

If you wanted to pass in an array of arguments, something like this would be better: 如果您想传递参数数组 ,则如下所示会更好:

public void pythonrun(String [] args) throws IOException, InterruptedException {
    String pythonScriptPath = "/yelp/msp.py";
    String[] cmd = new String[2 + args.length()];
    cmd[0] = "C:\\Python27\\python.exe";
    cmd[1] = pythonScriptPath;
    for(int i = 0; i < args.length(); i++) {
        cmd[i+2] = args[i];
    }
    :
}

You can tell exactly what parameters are being used for the process by placing the following code after the code where you set them: 通过将以下代码放置在设置它们的位置之后,您可以准确地确定该过程正在使用哪些参数:

for (int i = 0; i < cmd.length(); i++)
    System.out.println ("DEBUG " + i + ": [" + cmd[i] + "]");

Beyond that, there may be discrepencies between your command line version and the one called from your Java program. 除此之外,您的命令行版本与从Java程序调用的版本之间可能会有差异。

For one thing, your Java program is calling /yelp/msp.py whereas your command line version call msp.py directly. 一方面,您的Java程序正在调用/yelp/msp.py而您的命令行版本msp.py直接调用msp.py Are you sure that your msp.py script is actually in /yelp ? 您确定您的msp.py脚本实际上 /yelp msp.py 吗?

Also make sure that C:\\Python27\\python.exe is the correct Python interpreter. 还要确保C:\\Python27\\python.exe是正确的Python解释器。

And, one last thing, check which directory you're in when the Java program is running. 最后,检查Java程序运行时所处的目录。 If it's not what you expect, you may well be creating reviews.txt in a totally unexpected place. 如果这不是您所期望的,那么您很可能在一个完全意外的地方创建reviews.txt

String python_script_path ="Path to Python script/";
ProcessBuilder pb = new ProcessBuilder("python","msp.py","parameter1","parameter2");
 pb.directory(new File(python_script_path));
Process process = pb.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br1 = new BufferedReader(isr);
String line1;
 while ((line1 = br1.readLine()) != null) {
 System.out.println(line1);
 }

Use Processbuilder and set path variables 使用Processbuilder并设置路径变量

I have managed to find the solution for this one although I still don't get it. 尽管我还是不明白,但我设法找到了解决方案。

I used the same code and it works on Ubuntu. 我使用了相同的代码,并且可以在Ubuntu上运行。

cmd[0] = "python";

This is all I modified and I ran the same script and b00m and it works! 这就是我所做的全部修改,并且我运行了相同的脚本和b00m,并且可以正常工作!

The question is WHY? 问题是为什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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