当前位置:   article > 正文

C#项目中直接使用cmd调用jar包和Python脚本_c# winform 启动cmd运行jar程序

c# winform 启动cmd运行jar程序

调用jar包,首先你得先开发一个jar包,可以自己通过命令:

java -jar xxxxx.jar  param1         正常调用。
  • 1

调用代码:

private void button3_Click(object sender, EventArgs e)
		{
            Process p = new Process();
            //设置要启动的应用程序
            p.StartInfo.FileName = "cmd.exe";
            //是否使用操作系统shell启动
            p.StartInfo.UseShellExecute = false;
            // 接受来自调用程序的输入信息
            p.StartInfo.RedirectStandardInput = true;
            //输出信息
            p.StartInfo.RedirectStandardOutput = true;
            // 输出错误
            p.StartInfo.RedirectStandardError = true;
            //不显示程序窗口
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
            //启动程序
            p.Start();
            //向cmd窗口发送输入信息
            p.StandardInput.WriteLine("java -jar D:\\360MoveData\\Users\\Administrator\\Desktop\\speak.jar 中国国家" + "&exit");
            p.StandardInput.AutoFlush = true;
            //获取输出信息
            string strOuput = p.StandardOutput.ReadToEnd();
            Console.WriteLine(strOuput);
            //等待程序执行完退出进程
            p.WaitForExit();
            p.Close();
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

调用Python脚本也一样,开发好Python脚本,首先得能独立运行,自己测好后,在C#里调用,
代码:

			string pythonFilePath = Directory.GetCurrentDirectory() + "\\脚本\\自动出报告\\WriteHtml_地图.py";
            Process proc = null;
            try
            {
                proc = new Process();
                //proc.StartInfo.WorkingDirectory = pythonFilePath;
                proc.StartInfo.FileName = @"C:\Windows\System32\cmd.exe";
                proc.StartInfo.RedirectStandardInput = true;   //接受来自调用程序的输入信息
                proc.StartInfo.RedirectStandardOutput = true;  //由调用程序获取输出信息
                proc.StartInfo.UseShellExecute = false;       //要重定向 IO 流,Process 对象必须将 UseShellExecute 属性设置为 False
                proc.StartInfo.CreateNoWindow = true;          //不显示程序窗口
                proc.Start();
                string cmd = "python " + pythonFilePath + " " + tableName + " " + rangeType + " " + rangeName + " " + yiDong + " " + lianTong + " " + dianXin + "&exit";
                Logger.RecordInfo(cmd);
                proc.StandardInput.WriteLine(cmd);
                proc.StandardInput.AutoFlush = true;
                //获取cmd窗口的输出信息
                string output = proc.StandardOutput.ReadToEnd();
                Logger.RecordInfo(output);
                proc.WaitForExit();
                proc.Close();
                if (output.Contains("执行结束"))
                {
                    MessageBox.Show(type + "分析完成!!!");
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                Logger.RecordInfo(ex.StackTrace.ToString());
                Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
            }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/478571
推荐阅读
相关标签
  

闽ICP备14008679号