其实也很简单,就是在C#中构建一个Process,启动jar包,并且给jar包传递参数
因为我并没有怎么学过JAVA,所以只写了个很小的Demo,就是根据传入的参数获取对应的数据
以下是JAVA Demo
package com.movie; import static java.lang.System.*; /** * Created by rZH on 2018/11/21 */ public class MovieInfo { public static void main(String[] args){ //如果当前无请求参数 if ( args.length<=0){ //则程序终止 return; } //构建数据对象 DataClass dc=new DataClass(); //获取传进来的第一个参数 String arg=args[0]; //使用数据对象根据参数来得到对应的数据 String str= dc.GetMovieListJson(arg); //打印数据 out.println(str); } }
package com.movie; /** * Created by rZH on 2018/11/21 */ public class Movie { public Movie(String name,String beginTime,String room,String date,String version,String language){ this.Name=name; this.BeginTime=beginTime; this.Data=date; this.Room=room; this.Version=version; this.Language=language; } public String Name; public String BeginTime; public String Room; public String Data; public String Version; public String Language; }
package com.movie; import com.alibaba.fastjson.JSON; import java.util.ArrayList; import java.util.List; /** * Created by rZH on 2018/11/21 */ public class DataClass { public String GetMovieListJson(String date){ List<Movie> list=new ArrayList<Movie>(); list.add(new Movie("M1","13:00","1号厅","20181120","2D","国语")); list.add(new Movie("M1","13:20","2号厅","20181120","2D","国语")); list.add(new Movie("M2","13:40","3号厅","20181120","3D","国语")); list.add(new Movie("M1","14:00","4号厅","20181120","2D","英语")); list.add(new Movie("M1","15:00","5号厅","20181120","2D","国语")); list.add(new Movie("M3","14:30","2号厅","20181120","2D","国语")); list.add(new Movie("M1","15:20","3号厅","20181130","3D","国语")); list.add(new Movie("M1","14:40","4号厅","20181120","2D","国语")); list.add(new Movie("M1","16:00","5号厅","20181122","2D","原话")); list.add(new Movie("M2","16:20","1号厅","20181120","3D","国语")); list.add(new Movie("M1","16:35","2号厅","20181122","2D","国语")); list.add(new Movie("M1","17:00","4号厅","20181120","2D","国语")); list.add(new Movie("M2","17:20","3号厅","20181120","3D","国语")); list.add(new Movie("M1","17:45","5号厅","20181120","2D","粤语")); list.add(new Movie("M4","18:00","2号厅","20181120","2D","国语")); List<Movie> ml=new ArrayList<Movie>(); for(int i=0;i<list.size();i++){ if (list.get(i).Data.equals(date)){ ml.add(list.get(i)); } } String str = JSON.toJSONString(ml); return str; } }
额,原谅我真不会写JAVA,就这些代码我还是在网上东查西查才查出来的,也原谅我用C#的风格去写吧
接着就是C#里面的,这个方法是在网上找的,某个博主写的,网页关了我找不着本人了
我的一些配置是写在app.config文件里的
以下是该文件的内容
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> </startup> <appSettings> <!--cmd需要启动的程序路径--> <add key="startPath" value="cmd.exe"/> <!--cmd程序启动的程序命令--> <add key="args" value="java -jar F:JavaMovieDemooutartifactsJavaMovieDemo_jarJavaMovieDemo.jar"/> </appSettings> </configuration>
static void Main (string[] args) { //运行程序 string result= RunCmdAndGet (ConfigurationManager.AppSettings["args"]+ " 20181120"); Console.WriteLine ("从java包得到的数据是: "); Console.WriteLine (result); Console.ReadKey (); } /// <summary> /// 启动CMD并且获取到数据 /// </summary> /// <param name="query"></param> public static string RunCmdAndGet (string query) { Process CmdProcess = new Process (); CmdProcess.StartInfo.FileName = ConfigurationManager.AppSettings["startPath"]; CmdProcess.StartInfo.CreateNoWindow = true; // 不创建新窗口 CmdProcess.StartInfo.UseShellExecute = false; //不启用shell启动进程 CmdProcess.StartInfo.RedirectStandardInput = true; // 重定向输入 CmdProcess.StartInfo.RedirectStandardOutput = true; // 重定向标准输出 CmdProcess.StartInfo.RedirectStandardError = true; // 重定向错误输出 CmdProcess.StartInfo.Arguments = "/c " + query;//“/C”表示执行完命令后马上退出 CmdProcess.Start ();//执行 string result= CmdProcess.StandardOutput.ReadToEnd ();//获取返回值 CmdProcess.WaitForExit ();//等待程序执行完退出进程 CmdProcess.Close ();//结束 return result; }
以下图片就是结果