从maven运行外部python脚本

时间:2015-07-12 23:49:16

标签: java python eclipse maven

我必须从maven项目运行一个python脚本。我使用main方法创建了一个临时类来检查它是否按预期工作,使用了流程构建器,如果我指定python脚本的绝对路径,然后使用RUN作为Java应用程序从eclipse运行java类,它就可以工作。

如果我更改了getClass()。getResourceAsStream(" /scripts/script.py"),它会抛出一个异常,因为它无法找到python脚本。

放置python脚本的最佳位置是什么?如何在不指定完整路径的情况下在Java类中访问它。由于我是maven的新手,可能是因为用于执行Java程序的方法。

afterUpdate

这是pb命令的输出 pbCommand [0]:/ Users / user1 / .virtualenvs / python3 / bin / python3

pbCommand [1]:显示完整的python脚本

package discourse.apps.features;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.simple.JSONObject;  
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;



public class Test {

protected String scriptPath = "/Users/user1/project1/scripts/script.py";
protected String python3Path = "/Users/user1/.virtualenvs/python3/bin/python3";


public static void main(String[] args) throws IOException {
    new Test().score();
}

public JSONObject score() {
        String text1="a";
        String text2="b";
        JSONObject rmap =null;  

        try
        {

        String line= null;  
        String writedir=System.getProperty("user.dir")+ "/Tmp";
        String pbCommand[] = { python3Path, scriptPath,"--stringa", text1, "--stringb",text2,"--writedir", writedir };
        ProcessBuilder pb = new ProcessBuilder(pbCommand);
        Process p = pb.start();

            InputStream is = p.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            while ((line = br.readLine()) != null) {
                JSONParser parser = new JSONParser(); 
                rmap= (JSONObject) parser.parse(line);
            }

        } catch (IOException | ParseException ioe) {
            System.err.println("Error running  script");
            ioe.printStackTrace();
            System.exit(0);
        }

        return rmap;
    }
}

pbCommand [2]: - stringa

pbCommand [3]:a

pbCommand [4]: - stringb

pbCommand [5]:b

pbCommand [6]: - writedir

pbCommand [7]:/用户/用户1 / PROJECT1 / TMP

1 个答案:

答案 0 :(得分:0)

将脚本放在main / resources文件夹中,然后将其复制到目标文件夹。 然后确保使用类似com.google.common.io.Resources类的内容,您可以使用

添加该类
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava-io</artifactId>
    <version>r03</version>
</dependency>

然后我有一个这样的类,它有助于将资源文件转换为字符串:

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

import com.google.common.base.Charsets;
import com.google.common.io.Resources;

public class FileUtil
{

    public static String convertResourceToString(URL url)
    {
        try
        {
            return Resources.toString(url, Charsets.UTF_8);
        }
        catch (Exception e)
        {
            return null;
        }
    }

    public static String convertResourceToString(String path)
    {
        return convertResourceToString(Resources.getResource(path));
    }

    public static String convertResourceToString(URI url)
    {
        try
        {
            return convertResourceToString(url.toURL());
        }
        catch (MalformedURLException e)
        {
            return null;
        }
    }

}

如果您正在学习maven,请尝试使用它而不是IDE来运行和打包您的应用程序,这就是它的假设。然后,一旦您确信应用程序将作为打包的jar运行,那么只需使用IDE即可运行它。

相关问题