使用Java运行Python脚本的问题(Eclipse)

时间:2011-06-23 15:58:53

标签: java python eclipse jython interpreter

所以昨天我问了一个问题,我觉得我得到了答案,因为一切似乎都运转正常。我今天回来尝试运行我的程序并发现没有任何反应,这次我根本没有收到任何错误。

为了让您对事物有所了解,我一直在寻找将朋友制作的Python脚本合并到我正在尝试开发的Java应用程序中。经过一些试验和错误,我终于发现了'Jython',并使用PythonInterpreter尝试运行脚本。

但是,在尝试运行它时,我在Python脚本中收到错误。这是通过一个SO的成员提出的改变“thisDir = getcwd()”的建议来解决的,或者我认为。现在我不知道什么是错的,当我直接从命令行运行时,脚本工作正常并完全符合我的需要(从存储在同一目录中的.docx文件中提取所有图像),所以我不知道。有任何想法吗?

有人可以帮助我吗?

爪哇:

import org.python.core.PyException;
import org.python.util.PythonInterpreter;

public class SPImageExtractor
{
    public static void main(String[] args) throws PyException
    {   
        try
        {
            PythonInterpreter.initialize(System.getProperties(), System.getProperties(), new String[0]);
            PythonInterpreter interp = new PythonInterpreter();
            interp.execfile("C:/Documents and Settings/user/workspace/Intern Project/Proposals/Converted Proposals/Image-Extractor2.py");
        }
        catch(Exception e)
        {
            System.out.println(e.toString());
            e.printStackTrace();
        }
    }
}

的Python:

from os import path, chdir, listdir, mkdir, gcwd
from sys import argv
from zipfile import ZipFile
from time import sleep

#A few notes -
#(1) when I do something like " _,variable = something ", that is because
#the function returns two variables, and I only need one.  I don't know if it is a
#common convention to use the '_' symbol as the name for the unused variable, but
#I saw it in some guy's code in the past, and I started using it.
#(2) I use "path.join" because on unix operating systems and windows operating systems
#they use different conventions for paths like '\' vs '/'.  path.join works on all operating
#systems for making paths.

#Defines what extensions to look for within the file (you can add more to this)
IMAGE_FILE_EXTENSIONS = ('.bmp', '.gif', '.jpg', '.jpeg', '.png', '.tif', '.tiff')

#Changes to the directory in which this script is contained
thisDir = gcwd()
chdir(thisDir)

#Lists all the files/folders in the directory
fileList = listdir('.')
for file in fileList:

    #Checks if the item is a file (opposed to being a folder)
    if path.isfile(file):

        #Fetches the files extension and checks if it is .docx
        _,fileExt = path.splitext(file)
        if fileExt == '.docx':

            #Creates directory for the images
            newDirectory = path.join(thisDir, file + "-Images")
            if not path.exists(newDirectory):
                mkdir(newDirectory)

            currentFile = open(file,"r")
            for line in currentFile:
                print line

            sleep(5)



            #Opens the file as if it is a zipfile
            #Then lists the contents
            try:
                zipFileHandle = ZipFile(file)
                nameList = zipFileHandle.namelist()

                for archivedFile in nameList:
                    #Checks if the file extension is in the list defined above
                    #And if it is, it extracts the file
                    _,archiveExt = path.splitext(archivedFile)
                    if archiveExt in IMAGE_FILE_EXTENSIONS:
                        zipFileHandle.extract(archivedFile, newDirectory)
            except:
                pass

修改

刚刚取得了一些进展,所以在Eclipse中,当我自己运行脚本(不是通过java调用)作为“Python Run”时,一切正常。但是,当我执行“Jython Run”脚本时,它会为图像创建新目录,但它不会自己提取图像。

最终编辑

男人,只是拧蹩脚的Jython。我刚刚将python安装到我的路径中......

Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("C:\\Python27\\python.exe  \"C:\\Documents and Settings\\user\\workspace\\Intern Project\\Proposals\\Converted Proposals\\ImageExtractor2.py\"");
巴姆,工作得很好。

希望帮助那里的人。

0 个答案:

没有答案