perl - Java调用perl

代码亲自跑一下,很好玩

Java 调用 perl 

Java代码   收藏代码
  1. Runtime.getRuntime().exec("perl C:\\Perl\\execise.pl");  


Java 调用 perl 通过IO的方式来得到返回值 
Java代码   收藏代码
  1. package fiveGame;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.InputStreamReader;  
  7.   
  8. public class JavaCallPerl {  
  9.     public static void main(String[] args) throws IOException {  
  10.   
  11.         StringBuffer resultStringBuffer = new StringBuffer();  
  12.   
  13.         String lineToRead = "";  
  14.         int exitValue = 0;  
  15.   
  16.         try {  
  17.   
  18.             Process proc = Runtime.getRuntime().exec("perl C:\\Perl\\execise.pl");  
  19.             InputStream inputStream = proc.getInputStream();  
  20.             BufferedReader bufferedRreader = new BufferedReader(new InputStreamReader(inputStream));  
  21.   
  22.             // save first line  
  23.             if ((lineToRead = bufferedRreader.readLine()) != null) {  
  24.                 resultStringBuffer.append(lineToRead);  
  25.             }  
  26.   
  27.             // save next lines  
  28.             while ((lineToRead = bufferedRreader.readLine()) != null) {  
  29.                 resultStringBuffer.append("\r\n");  
  30.                 resultStringBuffer.append(lineToRead);  
  31.             }  
  32.   
  33.             // Always reading STDOUT first, then STDERR, exitValue last  
  34.             proc.waitFor(); // wait for reading STDOUT and STDERR over  
  35.             exitValue = proc.exitValue();  
  36.         } catch (Exception ex) {  
  37.             resultStringBuffer = new StringBuffer("");  
  38.             exitValue = 2;  
  39.         }  
  40.   
  41.         System.out.println("exit:" + exitValue);  
  42.   
  43.         System.out.println(resultStringBuffer.toString());  
  44.   
  45.     }  
  46.   
  47. }  



perl 
Java代码   收藏代码
  1. #!/usr/bin/perl -w  
  2. $pi = 3.141592654;  
  3. $circ = 2 * $pi * 12.5;  
  4. print "The circumference of a circle of radius 12.5 is $circ.\n"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值