2

First let me say that this question is not a duplicate of Use Process.Start with parameters AND spaces in path. I am using the System.Diagnostics.Process to start a cmd.exe window and then running java in that window. Except I want the java command to be run based on their installed Java path, as the PATH environment variable is unreliable and doesn't seem to get set very often when Java is installed. So I replaced the "java" in my arguments for the Process with the actual Java path, but now I'm getting this error:

'C:\Program' is not recognized as an internal or external command, operable program or batch file.

Clearly this is because there are spaces in the name, but I am properly quoting the path and using escape characters to create those quotes. Here is the code used to run the cmd.exe:

        System.Diagnostics.Process process = new System.Diagnostics.Process();
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
        //startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
        startInfo.FileName = "cmd.exe";
        if (chbPath.Checked) startInfo.Arguments = "/C \"" + javaPath + "\\bin\\java.exe\" -Djava.library.path=\"lib\\natives-win\" -jar SecondDimension.jar " + chbWindowed.Checked.ToString();
        else startInfo.Arguments = "/C java -Djava.library.path=\"lib\\natives-win\" -jar SecondDimension.jar " + chbWindowed.Checked.ToString();
        process.StartInfo = startInfo;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardError = true;
        process.Start();
        this.Visible = false;
        process.WaitForExit();
        Console.WriteLine(process.StandardError.ReadToEnd());
        Application.Exit();

If chbPath.Checked = false, it runs the command with the java command set with PATH. Which works fine for me, but doesn't for people who haven't ever tried to run java from the command line. But when I check chbPath, then I get the error listed above. Can anyone help with this? This is really annoying and I should have been done with this hours ago, but of course a SPACE....a SINGLE SPACE is stopping me from progressing....ARGHHH!!!

Edit:

Also here is the code for my path finder, which I pulled off another post here:

        String javaKey = "SOFTWARE\\JavaSoft\\Java Runtime Environment";
        using (var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(javaKey))
        {
            String currentVersion = baseKey.GetValue("CurrentVersion").ToString();
            using (var homeKey = baseKey.OpenSubKey(currentVersion))
                return homeKey.GetValue("JavaHome").ToString();
        }
Community
  • 1
  • 1
Taien
  • 23
  • 6
  • DId you try to use the property WorkingDirectory of the ProcessStartInfo class? – Steve Sep 14 '15 at 20:38
  • I haven't. So you're saying set the `process.StartInfo.WorkingDirectory = javaPath` and then just run the java command? – Taien Sep 14 '15 at 20:42
  • Ok thanks, I will try that. – Taien Sep 14 '15 at 20:43
  • Read also the [MSDN page](https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.workingdirectory%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396) about that property and its interaction with UseShellExecute – Steve Sep 14 '15 at 20:44
  • Ok, I gave that a try and while it now properly finds the java executable, it can no longer find the .jar I am trying to run because it is in the directory the program is run from, which is not the same as the Java directory. -.- – Taien Sep 14 '15 at 20:49
  • You could try setting an Environment variable with the path where the java program resides, then use that variable to specify the path in the ProcessStartInfo arguments – Steve Sep 14 '15 at 21:01

2 Answers2

0

First you could use the property ProcessStartInfo.WorkingDirectory to set the working folder for the Java process, then, because your program is in a different directory you need to change the path to this program.

You could set an Environment variable and use that variable to complete the path to the program or directly include the name of the program in the environment variable

Environment.SetEnvironmentVariable("JAVA_PRG", @"d:\temp"); // Whatever
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WorkingDirectory = Path.Combine(javaPath, "bin");
startInfo.FileName = "cmd.exe";
if (chbPath.Checked) 
    startInfo.Arguments = "/C java.exe .... -jar %JAVA_PRG%\SecondDimension.jar ";
Steve
  • 213,761
  • 22
  • 232
  • 286
0

Ok, so I was able to fix it. Apparently I don't need to run a cmd window to run the java command, because using diagnostics.process to start a progarm this way always creates a console window. So I just changed the GetJavaInstallationPath() code to return the path to the actual java executable, and then just set StartInfo.FileName = GetJavaInstallationPath(); which pretty much solved all my problems. This way the working directory stays in the game's directory and I still get the console window I wanted. So I guess I was trying too hard. :) Here's the fixed code:

    private void btnLaunch_Click(object sender, EventArgs e)
    {
        System.Diagnostics.Process process = new System.Diagnostics.Process();
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        startInfo.FileName = GetJavaInstallationPath();
        startInfo.Arguments = "-Djava.library.path=\"lib\\natives-win\" -jar SecondDimension.jar " + chbWindowed.Checked.ToString();
        process.StartInfo = startInfo;
        process.Start();
        this.Visible = false;
        process.WaitForExit();
        Application.Exit();
    }
Taien
  • 23
  • 6