Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.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
Java 使用Runtime.getRuntime().exec(字符串[]cmdarray,字符串[]envive,文件目录)_Java_Python - Fatal编程技术网

Java 使用Runtime.getRuntime().exec(字符串[]cmdarray,字符串[]envive,文件目录)

Java 使用Runtime.getRuntime().exec(字符串[]cmdarray,字符串[]envive,文件目录),java,python,Java,Python,我想使用Runtime.getRuntime().exec(String[]cmdarray,String[]envi,File dir),因为我在python程序中使用相对方向。当我写这段代码时;它不工作,并且没有显示错误报告 Process proc1=Runtime.getRuntime().exec("python /Users/g/Documents/Project/fyp/Q/__init__.py 453454336",,/Users/g/Documents/Project/f

我想使用
Runtime.getRuntime().exec(String[]cmdarray,String[]envi,File dir)
,因为我在python程序中使用相对方向。当我写这段代码时;它不工作,并且没有显示错误报告

  Process proc1=Runtime.getRuntime().exec("python /Users/g/Documents/Project/fyp/Q/__init__.py 453454336",,/Users/g/Documents/Project/fyp/Q/)
proc1.waitFor();
我在mac中使用java。python版本是2.7。

实际上,我想在终端中做的是:

cd /Users/g/Documents/Project/fyp/Q/
python __init__.py

似乎您正在向.py文件传递一些参数 简单使用

String command = "python /Users/g/Documents/Project/fyp/Q/__init__.py 453454336"
Runtime.getRuntime().exec(command)
确保将
null
传递给环境变量数组,并且第三个参数实际上是一个文件对象

使用以下代码

File directory = new File("/Users/g/Documents/Project/fyp/Q/"); 
Process proc1=Runtime.getRuntime().exec("python /Users/g/Documents/Project/fyp/Q/__init__.py 453454336",null,directory)

您能展示一些实际编译的Java代码吗?它仍然不起作用,而且因为我的.py文件需要读取其他文件,所以我需要使用相对方向。作为命令行参数?您的.py文件将如何读取该文件?我尝试执行我的.py程序,然后.py程序将执行包括读取文件在内的大量工作。但是路径是:file_dir=“./myFile.txt”,所以我需要更改dir。我重写我的.py文件dir使用绝对路径,然后使用您的答案,它可以工作。但是我仍然不知道如何使用Runtime.getRuntime().exec(String[]cmdarray,String[]envi,File dir)