java启动exe程序,传递参数和获取参数

1、java中启动exe程序 ,并添加传参

    String[] cmd = {"hh.exe","12315"};
        
        Process process = null;
        try {
        	ProcessBuilder pb = new ProcessBuilder();
        	pb.command(cmd);
        	process=pb.start(); 
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (process != null){
                process.destroy();
            }
        }

在cmd中,第一个参数所要启动的EXE程序,第二个参数为 传参;

2、在exe程序中获取 传递的参数
在步骤一中打开的“hh.exe”程序,也是基于java编写的。
我们打印一下main方法的args[],即可拿到由上一个程序传递的参数。

	public static void main(String args[]) {
//		for (int i=0;i<args.length;i++){
////			System.out.println(args[i]);
//			JOptionPane.showMessageDialog (null, args[i], "日志", JOptionPane.CANCEL_OPTION);
//		}
//		System.exit(0);//结束 当前进程

猜你喜欢

转载自blog.csdn.net/baidu_30891377/article/details/83619740