3

I have a method that I call on PageLoad of an aspx (the method will later be moved to a file in the AppCode dir) that looks like this:

public partial class PagesSystem_TestGCC : System.Web.UI.Page
{
     protected void Page_Load(object sender, EventArgs e)
     {
        string strCmdText;

        strCmdText = "/K java -jar c:\\TheSite\\compiler.jar --js      
                      c:\\TheSite\\JSTest\\hello.js --js_output_file
                      c:\\TheSite\\JSTest\\hello-compiled.js";

            System.Diagnostics.Process.Start("CMD.exe", strCmdText);
     }
}

The problem is that this approach relies on using the computer's file system. What do I need to change in my code to make it work with the application's root?

Thanks.

frenchie
  • 51,731
  • 109
  • 304
  • 510
  • 1
    Side note: there are many "how to run executable from ASP.Net" questions around - search/check - you need to get permissions right to be able to do so (in addition to have correct JAVA environment for account you run server code under). Also many questions on "how to capture CMD output" which may help there (or create .CMD file that will dump output of java to file instead of running it directly) – Alexei Levenkov Dec 31 '13 at 04:31
  • @AlexeiLevenkov: indeed, I'm using parts of the first answer provided here: http://stackoverflow.com/questions/1469764/run-command-prompt-commands – frenchie Dec 31 '13 at 04:46
  • you can try [redirect output stream](http://technet.microsoft.com/en-us/library/bb490982.aspx) to file and then see it – Grundy Jan 02 '14 at 09:00
  • Why the downvote?? I think running Closure Compiler as a java process is really useful. – frenchie Jan 03 '14 at 17:47
  • @frenchie I can't imagine running closure-compiler on demand on the server. Invoking the java app is slow and only parts of the application are thread safe. (FYI I'm not the one who downvoted the question). – Chad Killingsworth Jan 03 '14 at 22:10
  • @ChadKillingsworth: I'm not looking to do compilations on request (ie. user loads page = compile the js) but instead I'm looking to compile the script I have (27K lines). Time is not an issue. – frenchie Jan 03 '14 at 22:20
  • http://stackoverflow.com/q/206323/863564 – Lorenzo Dematté Jan 09 '14 at 10:23
  • @LorenzoDematté: this does not solve the problem: the syntax I have is c:\\TheSite\\JSTest\\hello-compiled.js and that won't work in a hosted environment like Azure for instance. – frenchie Jan 09 '14 at 17:43

2 Answers2

3

the command line window flickers open and then closes, without a chance to see what's going on

Use "cmd.exe /K ... instead of "cmd.exe /C ....

I think that your actual problem is either access permissions or not having java.exe in the PATH of the ASP.Net user.

Use this code:

System.Diagnostics.Process.Start(
    new ProcessStartInfo {
        FileName = "java.exe",
        Arguments = "-jar compiler.jar --js JSTest/hello.js --js_output_file JSTest/hello-compiled.js",
        WorkingDirectory = HttpRuntime.AppDomainAppPath,
        UseShellExecute = false
    }
);
Ark-kun
  • 6,358
  • 2
  • 34
  • 70
  • Ok, that helped! It says "Error: Unable to access jarfile /GCG/compiler.jar" – frenchie Jan 07 '14 at 21:26
  • You probably need to set the `StartInfo.WorkingDirectory` correctly. – Ark-kun Jan 07 '14 at 21:33
  • You can get the application path from `HttpRuntime.AppDomainAppPath`. You can also use `HttpContext.Current.Server.MapPath`. – Ark-kun Jan 07 '14 at 21:46
  • `AppDomainAppPath` and `MapPath` will work. Not so sure about starting processes. I think this would require FullTrust. – Ark-kun Jan 07 '14 at 22:20
  • The problem is that I need to pass the arguments of the file system back to the java process – frenchie Jan 07 '14 at 22:22
  • And why are you unable to do that? – Ark-kun Jan 07 '14 at 22:23
  • Because I'm passing in the filenames based on my computer's file system, not based on the app's filesystem. Changing the arguments to this: -jar /GCC/compiler.jar --js /GCC/JSTest/hello.js --js_output_file /GCC/JSTest/hello-compiled.js"; doesn't work – frenchie Jan 07 '14 at 22:39
  • Apps don't have a separate filesystem. And if you're talking about the virtual paths, that's what `MapPath` and `AppDomainAppPath` are all about. – Ark-kun Jan 08 '14 at 08:48
  • @Arl-kun: so do are the file parameters I need to pass into the java command line? – frenchie Jan 09 '14 at 08:12
2

open the windows command prompt and run the failed command there, so you will get a chance to note the exact error is throwing. Most probably the JAVA environment variables are not in place.

Rajin Kumar
  • 36
  • 1
  • 3