7

I have an MVC .NET application, and I want to run some .exes on the server. The exes are jarsigner.exe and zipalign.exe, used to modify and re-sign an android APK. I want to run them from the Controller.

It works locally, using a Process to launch the application, but I have cheated and used hardcoded paths to the .exe and to the folder with the stuff to be used by the exe.

I've added the .exes to the top-level of my project in visual studio, and added a folder containing the files to be worked upon by the .exes.

I'm struggling to workout how I get the path to the exes, and to the folder of files. Once I have that I can then invoke the Process (I suspect I might hit permissions trouble, but one step at a time...).

var processInfo = new ProcessStartInfo(@"C:\jarsigner.exe", @"..arguments"){
 CreateNoWindow = true, UseShellExecute = false
};
David Makogon
  • 69,407
  • 21
  • 141
  • 189
gregm
  • 341
  • 3
  • 12

1 Answers1

3

I'm struggling to workout how I get the path to the exes, and to the folder of files.

If your files under the top-level of project. We can find the path by using Server.MapPath(@"~\Jar\TextFile1.txt"). For jarsigner.exe. It’s in the bin folder of your java JDK. So we can use the environment variable.

Here is the sample code to get the path of jarsigner.exe and running result.

//string path = Server.MapPath(@"~\Jar\TextFile1.txt");    //get file path on the top-level of project(eg. ~\folder\xxx)
        string JavaPath = Environment.GetEnvironmentVariable("JAVA_HOME", EnvironmentVariableTarget.Machine); 
        if(string.IsNullOrEmpty(JavaPath)){
         JavaPath = Environment.GetEnvironmentVariable("JAVA_HOME", EnvironmentVariableTarget.User);
        }
        string path = JavaPath + @"\bin\jarsigner.exe";

        var processInfo = new ProcessStartInfo()
        {
            FileName = path,
            CreateNoWindow = true,
            UseShellExecute = false,
            WindowStyle = ProcessWindowStyle.Hidden
        };

        Process proc = Process.Start(processInfo);
        proc.WaitForExit();
        if (proc.ExitCode == 0)
            ViewBag.Message = path + " exec success.";
        else
            ViewBag.Message = path + " exec fail.";

        return View();

enter image description here enter image description here

Terry Fei
  • 249
  • 1
  • 3
  • This works great on the local machine, and almost on the Azure server. The issue on the server is that the EnvironmentVariable "Java_HOME" is null, i.e. the JRE is not installed on the server. How do I do that? I have tried just copying over the java.dll and manually setting the java environment variable, but that didn't work. – gregm Apr 06 '17 at 12:42
  • Do you mean in your KUDU cmd "echo %JAVA_Home%" is null? – Terry Fei Apr 06 '17 at 14:11
  • when I check Environment.GetEnvironmentVariable("Java_HOME", EnvironmentVariableTarget.Machine) it is null – gregm Apr 06 '17 at 14:46
  • In KUDU I can see the Java folder, and the echo gives me the correct path – gregm Apr 06 '17 at 15:28
  • Try to use "EnvironmentVariableTarget.User". I have update the code. – Terry Fei Apr 06 '17 at 15:54
  • EnvironmentVariableTarget.Machine - doesn't work, EnvironmentVariableTarget.User - doesn't work, EnvironmentVariableTarget.Process - works! and Environment.GetEnvironmentVariable("Java_HOME") - works! where works means I get the correct path – gregm Apr 06 '17 at 22:46
  • When we get the correct path.we can invoke the "jarsigner.exe" in the bin folder like above demo. – Terry Fei Apr 07 '17 at 01:20
  • All working now - thanks for your help, I used Environment.GetEnvironmentVariable("Java_HOME") , and it then works as you said it would – gregm Apr 07 '17 at 11:07