Java 在python子流程中将字符串作为参数发送

Java 在python子流程中将字符串作为参数发送,java,python,maven,batch-file,Java,Python,Maven,Batch File,根据答案,我可以用Python执行.bat文件。是否不仅可以执行.bat文件,还可以发送字符串作为参数,用于Java程序 我现在拥有的: Python脚本: Java程序: 我想要的: Python脚本: Java程序: 因此,主要思想是将python中的字符串作为参数发送到java程序并使用它。我试过的是: import os import subprocess filepath = "C:\\Path\\to\\file.bat" p = subprocess.Popen(filepath

根据答案,我可以用Python执行
.bat
文件。是否不仅可以执行
.bat
文件,还可以发送字符串作为参数,用于Java程序

我现在拥有的:

Python脚本:

Java程序:

我想要的:

Python脚本:

Java程序:

因此,主要思想是将python中的字符串作为参数发送到java程序并使用它。我试过的是:

import os
import subprocess

filepath = "C:\\Path\\to\\file.bat"
p = subprocess.Popen(filepath, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
grep_stdout = p.communicate(input=b"os.path.abspath('.\\file.txt')")[0]
print(grep_stdout.decode())
print(p.returncode)
print(os.path.abspath(".\\file.txt"))
输出:

1
表示出了问题。事实就是这样,因为Java程序如下所示:

public static void main(String[] args) throws IOException {
    String s = args[1];
    // write 's' to a file, to see the result
    FileOutputStream outputStream = new FileOutputStream("C:\\path\\to\\output.txt");
    byte[] strToBytes = s.getBytes();
    outputStream.write(strToBytes);
    outputStream.close();
}

在Python中执行
file.bat
后,
output.txt
为空。我做错了什么

代码中的问题是,您以错误的方式调用了
subprocess.Popen
。为了实现您想要的,作为状态,
Popen
应该使用一个字符串列表来调用,其中分别包含“可执行文件”和所有其他参数。更准确地说,在您的情况下,应该是:

p = subprocess.Popen([filepath, os.path.abspath('.\\file.txt')], stdout=subprocess.PIPE, stdin=subprocess.PIPE)

作为旁注,您应该/将只在“可执行文件”启动“请求”输入(stdin)时才使用
.communicate(…)

代码中的问题是,您以错误的方式调用了
subprocess.Popen
。为了实现您想要的,作为状态,
Popen
应该使用一个字符串列表来调用,其中分别包含“可执行文件”和所有其他参数。更准确地说,在您的情况下,应该是:

p = subprocess.Popen([filepath, os.path.abspath('.\\file.txt')], stdout=subprocess.PIPE, stdin=subprocess.PIPE)

作为旁注,您应该/将只在“可执行文件”启动“请求”输入(stdin)时才使用
.communicate(…)

这是一种回答。。。您必须使用包含“可执行文件”和每个参数的列表调用Popen,例如
p=subprocess.Popen([filepath,os.path.abspath('.\\file.txt')],stdout=subprocess.PIPE,stdin=subprocess.PIPE)
。如果调用的“程序”读取的是
stdin
,则可以使用
communicate
。不明白为什么需要
批处理文件
?@Squashman调用
mvn clean package
command@LohmarASHAR感谢您的反馈!我会试试你的建议,告诉你这是否是一种答案。。。您必须使用包含“可执行文件”和每个参数的列表调用Popen,例如
p=subprocess.Popen([filepath,os.path.abspath('.\\file.txt')],stdout=subprocess.PIPE,stdin=subprocess.PIPE)
。如果调用的“程序”读取的是
stdin
,则可以使用
communicate
。不明白为什么需要
批处理文件
?@Squashman调用
mvn clean package
command@LohmarASHAR感谢您的反馈!我会试试你的建议,并告诉你是否行得通
import os
import subprocess

filepath = "C:\\Path\\to\\file.bat"
p = subprocess.Popen(filepath, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
grep_stdout = p.communicate(input=b"os.path.abspath('.\\file.txt')")[0]
print(grep_stdout.decode())
print(p.returncode)
print(os.path.abspath(".\\file.txt"))
1
C:\\path\\to\\file.txt
public static void main(String[] args) throws IOException {
    String s = args[1];
    // write 's' to a file, to see the result
    FileOutputStream outputStream = new FileOutputStream("C:\\path\\to\\output.txt");
    byte[] strToBytes = s.getBytes();
    outputStream.write(strToBytes);
    outputStream.close();
}
p = subprocess.Popen([filepath, os.path.abspath('.\\file.txt')], stdout=subprocess.PIPE, stdin=subprocess.PIPE)