0

When I try to run Java code from cmd, it works. But when I run it from ASP.NET MVC application, I always get: Cannot find or load main. This is the code I'm using:

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();

What am I doing wrong?

Comp23555
  • 33
  • 2
  • 7
  • 1
    Probably you did not provide it with complete file name. Usually java runnables' files end with either `.class` or `.jar`. – M. Prokhorov Apr 11 '17 at 15:50
  • Adding the extension is one of the causes of this error when running java from cmd. But I tried it and it didn't work. – Comp23555 Apr 11 '17 at 15:57
  • Before running it in C#, what happens if you just execute it at the command line? `"C:/Program Files/Java/jdk1.8.0_20/bin/java.exe" E:/Atypon/TE1/src/Assignment3.DateTest` Get that working first before trying to do it from C#. – mason Apr 11 '17 at 16:25
  • I'm getting the same error. Even though if I use `java E:/Atypon/TE1/src/Assignment3.DateTest `, it works.( if I use it from the Java program directory). – Comp23555 Apr 11 '17 at 16:36
  • Can you just use the latter version that works? – M. Prokhorov Apr 11 '17 at 17:56
  • This version works from cmd, but not from ASP.NET process. – Comp23555 Apr 11 '17 at 19:17

2 Answers2

1

Could the paramters be wrong for the class name, perhaps it should be

p.StartInfo.Arguments = "E:/Atypon/TE1/src/Assignment3.DateTest.class"; 

What does "Could not find or load main class" mean?

Community
  • 1
  • 1
toby
  • 40
  • 5
0

Looks like I have to specify the path to main class in the working directory , not Arguments:

 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);
    }
Comp23555
  • 33
  • 2
  • 7