JAVA带参数调用Python脚本方法

1.这里采用的是JAVA的Runtime方法,话不多说直接上代码,就是这么任性!

package com.ldp.boot_mapper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class PythonTest2 {
public static void main(String[] args) throws Exception {
	
	 System.out.println("start python");
     //需传入的参数
     String a = "aaa", b = "bbb", c = "ccc", d = "ddd",e="9999"; 
     System.out.println("start;;;" + a);
     //设置命令行传入参数
     String[] args1 = new String[] { "python", "C:\\Users\\Administrator\\Desktop\\UnionPay\\test.py", a, b, c, d ,e};
     Process pr = Runtime.getRuntime().exec(args1);
     //TODO:该方法只能传递字符串

     BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
     String line;

     while ((line = in.readLine()) != null) {
//         line = decodeUnicode(line);
         System.out.println(line);
     }
     in.close();
     pr.waitFor();
     System.out.println("end python");
}

	
}

2.Python 脚本文件时保存在本地的 首先你的电脑上要安装Python 环境哦! 这里不做介绍! 楼主用的是Python2.7

可能有一些python小白会问: sys.argv[1]是什么意思?  其实这个的意思是接收参数的意思 !

下面是Python脚本文件内容

import sys  
import urllib  
print "hello"  
print "world"  
print sys.path  

import sys
print sys.argv[0]
print sys.argv[1]
print sys.argv[2]
print sys.argv[3]
print sys.argv[4]
print sys.argv[5]

3.运行结果:

是不是很完美 ,很容易懂 ! 

猜你喜欢

转载自blog.csdn.net/DreamWeaver_zhou/article/details/81098214