`

Java调用EXE

阅读更多

本文将详细为大家介绍一个java调用.net带参数exe的方法,以实现特殊的客户的特殊要求;

 

实现例子:

一、C# 制作TextExe.exe

打开VS2010,新建winform工程TextExe

1、新建FormEXE

Program.cs的代码:此处代码很重要,用于引入参数

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace TextExe
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            if (args.Length <= 0)
            {
                MessageBox.Show(args.Length.ToString());
                MessageBox.Show("请输入启动参数");
                Application.Exit();
            }
            if (args.Length == 1)
            {
                MessageBox.Show(args[0].ToString());
                if (args[0] == "Test")
                {
                    MessageBox.Show(args.Length.ToString());
                    Application.Run(new FormEXE(args));
                }
                else
                {
                    MessageBox.Show("启动参数错误,请输入Test");
                    Application.Exit();
                }
            }
        }


    }
}

FormEXE代码:弹出参数的对话框

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TextExe
{
    public partial class FormEXE : Form
    {
        private string[] s = new string[1];
        public FormEXE(string[] p)
        {
            InitializeComponent();
            s = p;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            MessageBox.Show(s[0].ToString());
        }
    }
}

 

 使用Java调用TextExe.exe

 

 首先---

  • TextExe.exe放入c:\windows\system32或jdk\bin目录下

public class testjava {
 
 public static void main(String[] args) {

  openMyExe();
 }
    public static void openMyExe() {  
        Runtime rn = Runtime.getRuntime();  
        String str= "lvqingboy
";
        String cmd[]={"TextExe",str};
        try {  
         Process p = rn.exec(cmd);
        } catch (Exception e) {  
            System.out.println("Error my exec ");  
        }
     }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics