Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Process Builder从Java运行python_Java_Python_Beautifulsoup_Processbuilder - Fatal编程技术网

使用Process Builder从Java运行python

使用Process Builder从Java运行python,java,python,beautifulsoup,processbuilder,Java,Python,Beautifulsoup,Processbuilder,对于要扩展的现有Java代码,我需要在Java中运行python代码。我正在为此使用Process builder: ProcessBuilder pb = new ProcessBuilder("python", "/directorypath/mypython.py"); Process p=pb.start(); BufferedReader stdInput = new BufferedReader(new InputStreamRea

对于要扩展的现有Java代码,我需要在Java中运行python代码。我正在为此使用Process builder:

   ProcessBuilder pb = new ProcessBuilder("python", "/directorypath/mypython.py");
   Process p=pb.start();
   BufferedReader stdInput = new BufferedReader(new
                 InputStreamReader(p.getInputStream()));

   BufferedReader stdError = new BufferedReader(new
                 InputStreamReader(p.getErrorStream()));

            // read the output from the command
            System.out.println("Here is the standard output of the command:\n");
            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);
            }

            // read any errors from the attempted command
            System.out.println("Here is the standard error of the command (if any):\n");
            while ((s = stdError.readLine()) != null) {
                System.out.println(s);
            }

            System.exit(0);
        }
虽然代码在命令行中运行得非常好,但在Java中我得到一个“We need BeautifulSoup,sorry”错误。据我所知,这意味着Java环境缺少某种类型的库,但代码在命令行中可以完美地工作

我需要发送一些环境变量吗?如果是,什么和如何


我已经安装了“BeautifulSoup4”,它是最新的

这不是一个确定的答案,但在我看来,这是该过程运行环境中的一个问题;它可能缺少一些环境变量,
python
希望这些变量找到“BeautifulSoup4”扩展名

ProcessBuilder
有一个
.environment()
方法,返回一个
Map
,其键是环境变量,其值是这些变量的值请注意,修改此地图实际上会改变环境(您将启动的流程之一,即)


尝试打印您的环境,并将其与从命令行运行时的环境进行比较。

这不是一个确定的答案,但在我看来,这在运行此进程的环境中是一个问题;它可能缺少一些环境变量,
python
希望这些变量找到“BeautifulSoup4”扩展名

ProcessBuilder
有一个
.environment()
方法,返回一个
Map
,其键是环境变量,其值是这些变量的值请注意,修改此地图实际上会改变环境(您将启动的流程之一,即)


尝试打印您的环境,并将其与从命令行运行时的环境进行比较。

您确定命令的最后一个参数吗?看起来很奇怪,它们只是一些要传递的参数。这段代码在命令行上运行良好。是的,但我相信您传递的代码是错误的;除非您的命令行看起来像
python/directorypath/mypython.py”-一个“二进制工具”-c1“
ProcessBuilder
不是shell解释器!好的,但即使我在没有这些参数的情况下传递[ProcessBuilder pb=newProcessBuilder(“python”,“/directorypath/mypython.py”);我再次得到相同的“我们需要美丽的乌苏,对不起”错误。我编辑了这个问题。谢谢您确定命令的最后一个参数吗?看起来很奇怪,它们只是一些要传递的参数。这段代码在命令行上运行良好。是的,但我相信您传递的代码是错误的;除非您的命令行看起来像
python/directorypath/mypython.py”-一个“二进制工具”-c1“
ProcessBuilder
不是shell解释器!好的,但即使我在没有这些参数的情况下传递[ProcessBuilder pb=newProcessBuilder(“python”,“/directorypath/mypython.py”);我再次得到相同的“我们需要美丽的乌苏,对不起”错误。我编辑了这个问题。谢谢根据您的建议,我运行了[ProcessBuilder pb=new ProcessBuilder(“env”);]并提取了linux的所有环境变量,将它们设置为一个映射,并使用此映射使用ProcessBuilder.environment.putAll(映射)方法设置ProcessBuilder环境变量。我仍然收到相同的错误消息,并且Process.waitfor()返回一个值1:(我说过要与您命令行中的env进行比较,而不是从JVM发布的进程根据您的建议,我运行[ProcessBuilder pb=new ProcessBuilder(“env”);]并提取linux的所有环境变量,将它们设置为映射,并使用此映射使用ProcessBuilder.environment.putAll(映射)设置ProcessBuilder环境变量)我仍然得到相同的错误消息,Process.waitfor()返回一个值1:(我说的是与命令行中的env进行比较,而不是从JVM发出的进程进行比较