以交互方式从 java 代码调用 perl 脚本
有人可以在下面的场景中帮助我吗,
我需要从我的 java 代码调用 perl 脚本。 perl脚本是一个交互式代码,它在执行过程中获取用户的输入并继续执行直至结束。因此,我使用的示例是,当用户输入某个值(例如“26”)时,perl 脚本在执行时会通过在控制台中打印“你多大了?”来询问年龄。然后它会打印“哇!你已经 26 岁了!”。
当我尝试从 java 代码调用此脚本时,该过程会等待,直到我在输出流中给出值 26,而在输入流中没有值。最后,当我再次读取输入流时,我得到了脚本的整个输出。那么,这里我不能让它互动吗?
我浏览了很多论坛和博客,但找不到任何一个完全符合我的要求。
这是java代码
import java.io.*;
public class InvokePerlScript {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Process process;
try
{
process = Runtime.getRuntime().exec("cmd /c perl D:\\sudarsan\\eclips~1\\FirstProject\\Command.pl");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
out.write("23");
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
process.waitFor();
if(process.exitValue() == 0)
{
System.out.println("Command Successful");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
else
{
System.out.println("Command Failure");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
catch(Exception e)
{
System.out.println("Exception: "+ e.toString());
}
}
}
Perl代码如下
$| = 1;
print "How old are you? \n";
$age = <>;
print "WOW! You are $age years old!";
提前致谢, 苏达桑
Can someone help me in the below scenario,
I need to call a perl script from my java code. The perl script is an interactive code, which gets the input from the user during its execution and continues further to end. So, the example I have used is, the perl script when executed asks for the age by printing in the console "How old are you?", when the user enter some value say '26'. Then it prints "WOW! You are 26 years old!".
When I tried calling this script from my java code, the process waits till I give the value as 26 in the outputstream, while in the inputstream there is no value. Then finally when again I read the inputstream, i get the entire output of the script together. So, here can't I make it interactive?
I have went through many forums and blogs, but couldn't locate any, which exactly target my requirement.
Here is the java code
import java.io.*;
public class InvokePerlScript {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Process process;
try
{
process = Runtime.getRuntime().exec("cmd /c perl D:\\sudarsan\\eclips~1\\FirstProject\\Command.pl");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
out.write("23");
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
process.waitFor();
if(process.exitValue() == 0)
{
System.out.println("Command Successful");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
else
{
System.out.println("Command Failure");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
catch(Exception e)
{
System.out.println("Exception: "+ e.toString());
}
}
}
Perl code is as below
$| = 1;
print "How old are you? \n";
$age = <>;
print "WOW! You are $age years old!";
Thanks in advance,
Sudarsan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在写入值后,您是否在 Java 中的 OutputStream 上调用
flush()
?如果不这样做,它们很可能只是保存在 Java 进程内的流缓冲区中,因此永远不会到达 Perl(结果是两个进程最终都会等待对方的 IO。)(取决于在流的实现上,这可能是必要的,也可能不是必要的,但它肯定不会有什么坏处——而且我过去也曾被这个问题困扰过,通常不需要那么小心,因为刷新是隐式发生的。
close()
被调用,但在这里你不能在写完后关闭流。)Are you calling
flush()
on the OutputStream in Java after writing the values? If you don't, there's a good chance they'll just be held in the stream's buffer within the Java process, and so never make it to Perl (with the result that both processes end up waiting for the other's IO.)(Depending on the implementation of the stream this may or may not be necessary, but it certainly wouldn't hurt - and I've been bitten by this in the past. Usually one doesn't need to be as careful, since flushing happens implicitly when
close()
is called, but here you can't close the stream after you've finished writing.)看起来您正在尝试读取此代码中的整行:
但是,在您的 perl 代码中,您没有打印结束行字符,因此
readLine
永远不会返回(根据 文档)。It looks like you're trying to read a full line in this code:
However, in your perl code, you are not printing an endline character, so
readLine
never returns (as per the documentation).