-1

如何在 python 中打开 java 文件?,我在网上搜索并找到了这个:

import os.path, subprocess
from subprocess import STDOUT, PIPE

def compile_java (java_file):
    subprocess.check_call(['javac', java_file])

def execute_java (java_file):
    cmd=['java', java_file]
    proc=subprocess.Popen(cmd, stdout = PIPE, stderr = STDOUT)
    input = subprocess.Popen(cmd, stdin = PIPE)
    print(proc.stdout.read())

compile_java("CsMain.java")
execute_java("CsMain")

但后来我得到了这个错误:

Traceback (most recent call last):
  File "C:\Python33\lib\subprocess.py", line 1106, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\casestudy\opener.py", line 13, in <module>
    compile_java("CsMain.java")
  File "C:\casestudy\opener.py", line 5, in compile_java
    subprocess.check_call(['javac', java_file])
  File "C:\Python33\lib\subprocess.py", line 539, in check_call
    retcode = call(*popenargs, **kwargs)
  File "C:\Python33\lib\subprocess.py", line 520, in call
    with Popen(*popenargs, **kwargs) as p:
  File "C:\Python33\lib\subprocess.py", line 820, in __init__
    restore_signals, start_new_session)
  File "C:\Python33\lib\subprocess.py", line 1112, in _execute_child
    raise WindowsError(*e.args)
FileNotFoundError: [WinError 2] The system cannot find the file specified
>>> 

python 文件和 java 文件在同一个文件夹中,我使用的是 Python 3.3.2,我该如何解决这个问题?还是你们有其他方法可以做到这一点?,任何答案都非常感谢,谢谢!

4

2 回答 2

1

我认为它没有识别javac命令。尝试手动运行命令,如果javac不是可识别的命令,请将其注册到您的PATH变量中,然后重试。

或者您可以尝试为javac和键入 Java 目录的完整路径名java

于 2013-10-20T16:03:58.483 回答
0

你需要添加path到你的java文件名。像这样:

compile_java("C:\\path\to\this\CsMain.java")
于 2013-10-20T15:54:09.480 回答