简体   繁体   English

如何从 Java 程序运行 Python 脚本

[英]How to run Python Script from Java Program

In my Java program I'm creating a Least Squares Approximation and exporting the coefficients, ie c1x+c2x^2+c3x^3..., to a text file that my Python script will read in and plot.在我的 Java 程序中,我创建了一个最小二乘近似并将系数(即 c1x+c2x^2+c3x^3...)导出到我的 Python 脚本将读取并绘制的文本文件中。 However, I cannot even get a basic Hello World python file to run from my Java program.但是,我什至无法从我的 Java 程序中获得基本的 Hello World python 文件。 I've scoured the internet for answers and I've used all the tricks in the book.我在互联网上搜索答案,并且使用了书中的所有技巧。 This is what I did: methodsOneThroughTwo methodsThreeThroughFour , and here is the output: exceptions .这就是我所做的: methodsOneThroughTwo methodsThreeThroughFour ,这里是输出: exceptions

I have the same python file, test.py, placed in two different locations, the first one being the directory of the java program (C:\\Users\\BScot\\OneDrive\\Desktop\\Numerical Analysis\\Lab2\\test.py) and the second being in the directory of a python project, (C:\\Users\\BScot\\OneDrive\\Desktop\\Numerical Analysis\\Test\\test.py), just to check if that would make a difference.我有相同的 python 文件 test.py,放在两个不同的位置,第一个是 java 程序的目录 (C:\\Users\\Bscot\\OneDrive\\Desktop\\Numerical Analysis\\Lab2\\test.py) 和第二个是在 python 项目的目录中,(C:\\Users\\Bscot\\OneDrive\\Desktop\\Numerical Analysis\\Test\\test.py),只是为了检查这是否会有所不同。

Some of the exceptions indicate that the file is found but it cannot run because it is not an executable;一些异常表明找到了该文件,但由于它不是可执行文件而无法运行; but when I add the python start command to the front of the file path it reads that it cannot find the file "python".但是当我将 python start 命令添加到文件路径的前面时,它读取它找不到文件“python”。

Any help would be much appreciated.任何帮助将非常感激。

You have space character in python script path.你在 python 脚本路径中有空格字符。 Try to quote it with \\" \\"尝试用 \\" \\" 引用它

Also you should take a look at ProcessBuilder: https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html你也应该看看 ProcessBuilder: https : //docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html

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

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