如何在通过 python 执行脚本时在 java 中询问用户输入
How to give the user input when asked in java while executing the script through python
我正在创建一个 python 脚本来执行以下任务:
1)列出目录中的所有文件
2) 如果找到的文件是 .java 类型那么
3) 它使用 subprocess.check_call 编译 java 文件
4) 如果没有错误,则使用与文件名
相同的class 名称执行文件
现在某些文件需要用户在 运行 时间内输入。
这正是我被困的地方。我的脚本编译并 运行s java 程序。
但是每当我的 java 程序要求输入时,"Enter The Number :" ,我的脚本不会接受输入,因为会抛出以下错误:
输入号码
线程中出现异常 "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:415)
at java.lang.Integer.parseInt(Integer.java:497)
at inp.main(inp.java:17)
我希望屏幕等待我的输入,当我输入数字时它会恢复执行
我的java程序是:
import java.io.*;
class inp
{
public static void main(String args[])throws IOException
{
InputStreamReader in=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(in);
System.out.println("Enter the Number");
int n=Integer.parseInt(br.readLine());
int b=10*n;
System.out.println("T 10 multiple of Number is : "+b);
}
}
我的 python 脚本是:
import subprocess
import sys
import os
s=os.getcwd()
s="codewar/media/"
print os.chdir(s)
t=os.getcwd()
print os.listdir(t)
for file in os.listdir(t):
if file.endswith(".java"):
proc=subprocess.check_call(['javac',file])
print proc
if proc==0:
l=file.split(".")
proc=subprocess.Popen(['java',l[0]],stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
input=subprocess.Popen(['java',l[0]],shell=True,stdin=subprocess.PIPE)
print proc.stdout.read()
请指出错误或告诉我新的方法。
首先,代替:
proc=subprocess.Popen(['java',l[0]],stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
input=subprocess.Popen(['java',l[0]],shell=True,stdin=subprocess.PIPE)
应该是:
proc=subprocess.Popen(['java',l[0]],stdout=subprocess.PIPE,stderr=subprocess.STDOUT, shell=True,stdin=subprocess.PIPE)
其次,使用 Popen.communicate 与子流程进行通信,即向其提供输入。在你的例子中:
(stdoutdata, stderrdata) = proc.communicate('2')
会将'2'传递给子进程,return子进程的stdout和stderr。
to give the user input when asked in java while executing the script through python
#!/usr/bin/env python
import os
from glob import glob
from subprocess import Popen, PIPE, call
wdir = "codewar/media"
# 1) list all .java files in directory
for path in glob(os.path.join(wdir, "*.java")):
# 2) compile the java file
if call(['javac', path]) != 0: # error
continue
# 3) if there is no error it then executes the file using its
# class name which is same as file name
classname = os.path.splitext(os.path.basename(path))[0]
p = Popen(['java', '-cp', wdir, classname],
stdin=PIPE, stdout=PIPE, stderr=PIPE,
universal_newlines=True) # convert to text (on Python 3)
out, err = p.communicate(input='12345')
if p.returncode == 0:
print('Got {result}'.format(result=out.strip().rpartition(' ')[2]))
else: # error
print('Error: exit code: {}, stderr: {}'.format(p.returncode, err))
关键这里要用.communicate()
方法
But I want in such a way that while its running and displays enter a
number : the screen should wait for my input and when i enter the
number it resumes its execution
如果您不需要捕获输出并且想从键盘手动提供输入那么您不需要使用 Popen(.., PIPE)
和 .communicate()
,只需使用 call()
而不是:
#!/usr/bin/env python
import os
from glob import glob
from subprocess import call
wdir = "codewar/media"
# 1) list all .java files in directory
for path in glob(os.path.join(wdir, "*.java")):
# 2) compile the java file
if call(['javac', path]) != 0: # error
continue
# 3) if there is no error it then executes the file using its
# class name which is same as file name
classname = os.path.splitext(os.path.basename(path))[0]
rc = call(['java', '-cp', wdir, classname])
if rc != 0:
print('Error: classname: {} exit code: {}'.format(classname, rc))
我正在创建一个 python 脚本来执行以下任务: 1)列出目录中的所有文件 2) 如果找到的文件是 .java 类型那么 3) 它使用 subprocess.check_call 编译 java 文件 4) 如果没有错误,则使用与文件名
相同的class 名称执行文件现在某些文件需要用户在 运行 时间内输入。 这正是我被困的地方。我的脚本编译并 运行s java 程序。 但是每当我的 java 程序要求输入时,"Enter The Number :" ,我的脚本不会接受输入,因为会抛出以下错误:
输入号码
线程中出现异常 "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:415)
at java.lang.Integer.parseInt(Integer.java:497)
at inp.main(inp.java:17)
我希望屏幕等待我的输入,当我输入数字时它会恢复执行
我的java程序是:
import java.io.*;
class inp
{
public static void main(String args[])throws IOException
{
InputStreamReader in=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(in);
System.out.println("Enter the Number");
int n=Integer.parseInt(br.readLine());
int b=10*n;
System.out.println("T 10 multiple of Number is : "+b);
}
}
我的 python 脚本是:
import subprocess
import sys
import os
s=os.getcwd()
s="codewar/media/"
print os.chdir(s)
t=os.getcwd()
print os.listdir(t)
for file in os.listdir(t):
if file.endswith(".java"):
proc=subprocess.check_call(['javac',file])
print proc
if proc==0:
l=file.split(".")
proc=subprocess.Popen(['java',l[0]],stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
input=subprocess.Popen(['java',l[0]],shell=True,stdin=subprocess.PIPE)
print proc.stdout.read()
请指出错误或告诉我新的方法。
首先,代替:
proc=subprocess.Popen(['java',l[0]],stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
input=subprocess.Popen(['java',l[0]],shell=True,stdin=subprocess.PIPE)
应该是:
proc=subprocess.Popen(['java',l[0]],stdout=subprocess.PIPE,stderr=subprocess.STDOUT, shell=True,stdin=subprocess.PIPE)
其次,使用 Popen.communicate 与子流程进行通信,即向其提供输入。在你的例子中:
(stdoutdata, stderrdata) = proc.communicate('2')
会将'2'传递给子进程,return子进程的stdout和stderr。
to give the user input when asked in java while executing the script through python
#!/usr/bin/env python
import os
from glob import glob
from subprocess import Popen, PIPE, call
wdir = "codewar/media"
# 1) list all .java files in directory
for path in glob(os.path.join(wdir, "*.java")):
# 2) compile the java file
if call(['javac', path]) != 0: # error
continue
# 3) if there is no error it then executes the file using its
# class name which is same as file name
classname = os.path.splitext(os.path.basename(path))[0]
p = Popen(['java', '-cp', wdir, classname],
stdin=PIPE, stdout=PIPE, stderr=PIPE,
universal_newlines=True) # convert to text (on Python 3)
out, err = p.communicate(input='12345')
if p.returncode == 0:
print('Got {result}'.format(result=out.strip().rpartition(' ')[2]))
else: # error
print('Error: exit code: {}, stderr: {}'.format(p.returncode, err))
关键.communicate()
方法
But I want in such a way that while its running and displays enter a number : the screen should wait for my input and when i enter the number it resumes its execution
如果您不需要捕获输出并且想从键盘手动提供输入那么您不需要使用 Popen(.., PIPE)
和 .communicate()
,只需使用 call()
而不是:
#!/usr/bin/env python
import os
from glob import glob
from subprocess import call
wdir = "codewar/media"
# 1) list all .java files in directory
for path in glob(os.path.join(wdir, "*.java")):
# 2) compile the java file
if call(['javac', path]) != 0: # error
continue
# 3) if there is no error it then executes the file using its
# class name which is same as file name
classname = os.path.splitext(os.path.basename(path))[0]
rc = call(['java', '-cp', wdir, classname])
if rc != 0:
print('Error: classname: {} exit code: {}'.format(classname, rc))