简体   繁体   English

从Java运行带有参数的exe python脚本

[英]Running python script, as exe, with parameters, from Java

I have found some tips on how to do that, but combining them together does not work, so i am asking for help. 我已经找到了一些有关如何做到这一点的技巧,但是将它们组合在一起是行不通的,所以我寻求帮助。

I have a script in python, which gets it's variables from parameters(arguments?)(sys.argv). 我在python中有一个脚本,该脚本从parameters(arguments?)(sys.argv)获取它的变量。 I have used PyInstaller to create an exe. 我已经使用PyInstaller创建一个exe。

Now i need to open this file, with parameters, from java. 现在,我需要从Java打开带有参数的文件。

The problem is, i got the error: Failed to execute script [ScriptName]. 问题是,我得到了错误:无法执行脚本[ScriptName]。

This is my code from java: 这是我来自Java的代码:

try {
    ProcessBuilder process = new ProcessBuilder(Location, arg1, arg2, arg3, arg4,
                                                arg5, arg6, arg7, arg8, arg9,
                                                arg10, arg11, arg12, arg13, arg14);
    Process proc = process.start();
    proc.waitFor();
} catch (IOException | InterruptedException e) {
    //logging the result
    e.printStackTrace();
}

And this is my code in python: 这是我在python中的代码:

arg1 = sys.argv[2]
arg2 = sys.argv[3]
arg3 = sys.argv[4]
arg4 = sys.argv[5]
arg5 = sys.argv[6]
(...)

I have tried different 'argv positions' (ie arg1 = sys.argv[1] etc.), but nothing works. 我尝试了不同的“ argv位置”(即arg1 = sys.argv [1]等),但没有任何效果。

Thanks in advance for any help. 在此先感谢您的帮助。

You can try to compose a call String with all your arguments like this (you should use a StringBuilder or something similar for tidy code): 您可以尝试使用所有这样的参数来组成一个调用字符串(对于整洁的代码,您应该使用StringBuilder或类似的东西):

String call = "python yourscript.py argument1 argument2 argument3";
Process p = Runtime.getRuntime().exec(call);

So far, this has always worked for me, without even needing an .exe. 到目前为止,这一直对我有用,甚至不需要.exe。 Also, you should be able to run an .exe the exact same way. 另外,您应该能够以完全相同的方式运行.exe。

None of the things mentioned have worked, i have decided to do it by txt file (writing txt in java and reading it in python). 提到的任何事情都没有起作用,我决定通过txt文件(在Java中写入txt并在python中读取它)进行操作。

I think that the best option would be to do pass the data by database, but in that case it was not an option :( 我认为最好的选择是通过数据库传递数据,但是在这种情况下,这不是一个选择:(

BTW. BTW。 i also have tried getting values by argparse instead of sys.arg, but i think the problem was in java (The script was not even executed). 我也尝试通过argparse而不是sys.arg获取值,但是我认为问题出在Java中(脚本甚至没有执行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM