2017-04-11 28 views
0

當我嘗試從cmd運行Java代碼時,它可以工作。但是當我從ASP.NET MVC應用程序運行它時,我總是得到: 無法找到或加載main。 這是我正在使用的代碼:從ASP.NET MVC運行Java不起作用

Process p = new Process(); 
      p.StartInfo.FileName = "C:/Program Files/Java/jdk1.8.0_20/bin/java.exe"; 
      p.StartInfo.UseShellExecute = false; 
      p.StartInfo.Arguments = "E:/Atypon/TE1/src/Assignment3.DateTest"; 
      p.StartInfo.RedirectStandardInput = true; 
      p.StartInfo.CreateNoWindow = true; 
      p.StartInfo.RedirectStandardOutput = true; 
      p.StartInfo.CreateNoWindow = true; 
      p.StartInfo.RedirectStandardError = true; 
      p.Start(); 
      string result = p.StandardError.ReadToEnd(); 
      p.WaitForExit(); 

我在做什麼錯?

+1

也許你沒有提供完整的文件名。通常java runnables的文件以'.class'或'.jar'結尾。 –

+0

添加擴展是從cmd運行java時出現此錯誤的原因之一。但我試過了,它不起作用。 – Comp23555

+0

在C#中運行之前,如果您只是在命令行執行它,會發生什麼? ''C:/ Program Files/Java/jdk1.8.0_20/bin/java.exe「E:/Atypon/TE1/src/Assignment3.DateTest'在從C#開始工作之前先完成這個工作。 – mason

回答

0

看來我必須指定工作目錄的路徑,主類,沒有參數:

public ActionResult Run() 
    { 
     Process p = new Process(); 
     p.StartInfo.FileName = "C:/Program Files/Java/jdk1.8.0_20/bin/java.exe"; 
     p.StartInfo.UseShellExecute = false; 
     //path to dir 
     p.StartInfo.WorkingDirectory = "E:/Atypon/TE1/src"; 
     //packageName.MainClass 
     p.StartInfo.Arguments = "Assignment3.DateTest"; 
     p.StartInfo.RedirectStandardInput = true; 
     p.StartInfo.CreateNoWindow = true; 
     p.StartInfo.RedirectStandardOutput = true; 
     p.StartInfo.CreateNoWindow = true; 
     p.StartInfo.RedirectStandardError = true; 
     p.Start(); 
     string result = p.StandardOutput.ReadToEnd(); 
     p.WaitForExit(); 
     return View("Index",(Object)result); 
    }