从 python 编译 java

发布于 2024-11-29 05:03:20 字数 394 浏览 1 评论 0原文

我正在编写一个脚本来从 python 中编译 .java 文件 但错误

import subprocess
def compile_java(java_file):
    cmd = 'javac ' + java_file 
    proc = subprocess.Popen(cmd, shell=True)

compile_java("Test.java")

错误:

javac is not recognized as an internal or external command windows 7

我知道如何解决 Windows 上的 CMD 问题。但是对于python我该如何解决呢? 我的意思是:我如何设置路径?

I'm writing a script to compile a .java file from within python
But the error

import subprocess
def compile_java(java_file):
    cmd = 'javac ' + java_file 
    proc = subprocess.Popen(cmd, shell=True)

compile_java("Test.java")

Error:

javac is not recognized as an internal or external command windows 7

I know how to fix the problem for CMD on windows. But how do I solve it for python?
What I mean is: how do i set the path?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦回梦里 2024-12-06 05:03:20
proc = subprocess.Popen(cmd, shell=True, env = {'PATH': '/path/to/javac'})

或者

cmd = '/path/to/javac/javac ' + java_file 
proc = subprocess.Popen(cmd, shell=True)
proc = subprocess.Popen(cmd, shell=True, env = {'PATH': '/path/to/javac'})

or

cmd = '/path/to/javac/javac ' + java_file 
proc = subprocess.Popen(cmd, shell=True)
眼眸印温柔 2024-12-06 05:03:20

您还可以发送参数:

            variableNamePython = subprocess.Popen(["java", "-jar", "lib/JavaTest.jar", variable1, variable2, variable3], stdout=subprocess.PIPE)
            JavaVariableReturned = variableNamePython.stdout.read()
            print "The Variable Returned by Java is: " + JavaVariableReturned

接收这些变量的 Java 代码如下所示:

public class JavaTest {
    public static void main(String[] args) throws Exception {
        String variable1 = args[0];
        String variable2 = args[1];
        String variable3 = args[2];

You can also send arguments as well:

            variableNamePython = subprocess.Popen(["java", "-jar", "lib/JavaTest.jar", variable1, variable2, variable3], stdout=subprocess.PIPE)
            JavaVariableReturned = variableNamePython.stdout.read()
            print "The Variable Returned by Java is: " + JavaVariableReturned

The Java code to receive these variables will look like:

public class JavaTest {
    public static void main(String[] args) throws Exception {
        String variable1 = args[0];
        String variable2 = args[1];
        String variable3 = args[2];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文