JAVA 和 C# 調用外部.exe文件,傳值並等等exe完成,獲取返回值


JAVA-

	String ykexe = getProperty("ykexe") + " " + tableout;   //getproperty("ykexe") 路徑   tableout 參數,多個參數用空格隔開
		StringBuilder infoMsg = new StringBuilder();
		StringBuilder errorMsg = new StringBuilder();
		String line = null;
		while (true) {
			Runtime rn = Runtime.getRuntime();
			Process p = null;
			try {
				p = rn.exec(ykexe);
				try {
					p.waitFor();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				BufferedReader info = new BufferedReader(new InputStreamReader(
						p.getInputStream()));
				BufferedReader error = new BufferedReader(
						new InputStreamReader(p.getErrorStream()));
				while ((line = info.readLine()) != null) {
					infoMsg.append(line).append("\n");
				}
				while ((line = error.readLine()) != null) {
					errorMsg.append(line).append("\n");
				}
				if (infoMsg.toString().contains("OK")) {
					return true;
				} else {
					System.out.println(errorMsg.toString());
					return false;
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				// p.destroy();
				e.printStackTrace();
				return false;
			}

  C# 

      string path = @"C:\GateWay\PrjCheck.exe";
            string fileName = path;
            Process p = new Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.FileName = fileName;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.Arguments = "IHSUSAA_1508211711";//參數以空格分隔,如果某個參數為空,可以傳入””
            p.Start();
            p.WaitForExit();
            //此處可以返回一個字符串,此例是返回壓縮成功之后的一個文件路徑
            string output = p.StandardOutput.ReadToEnd();
      

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM