Search code examples
c#pathenvironment-variablesjavacprocess.start

Process.Start() unable to find javac, even though it is in PATH


I am trying to compile Java from a C# console application using Process.Start(). Here is my code:

static string ProjectRootDirectory
{
    get
    {
        return Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), @"..\.."));
    }
}

static void Main(string[] args)
{
    var info = new ProcessStartInfo()
    {
        RedirectStandardOutput = true,
        FileName = "javac", 
        Arguments = Path.Combine(ProjectRootDirectory, @"Java\Main.java"), 
        UseShellExecute = false
    };
    var proc = Process.Start(info);
    using (var reader = proc.StandardOutput)
    {
        string s = reader.ReadToEnd();
        System.Diagnostics.Debug.WriteLine(s);
        Console.WriteLine(s);
        Console.ReadKey();
    }
}

When I run this, I get a Win32Exception on Process.Start() because it says "The system cannot find the file specified." However, I have already set by PATH to C:\Program Files\Java\jdk1.8.0_31\bin\ . Here is my full PATH (Java path found at end):

C:\ProgramData\Oracle\Java\javapath;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\wbem;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Users\James\utils\bash\;C:\Users\James\utils\batch\;C:\Program Files (x86)\Git\bin\;%JAVA_HOME%\bin\

And here is the JAVA_HOME environment variable my PATH references:

C:\Program Files\Java\jdk1.8.0_31\

When I type javac [file] into the command prompt manually, it works fine. But in Process.Start(), I have to specify the full path of javac. Any idea why?

EDIT: Following Alexei Levenkov's suggestion, I have found out that PATH is indeed seen differently by the current process. Printing Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process) results in

C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\NativeBinaries/x86;C:\ProgramData\Oracle\Java\javapath;C:\windows\system32;C:\windows;C:\windows\System32\wbem;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Users\James\utils\bash\;C:\Users\James\utils\batch\;C:\Program Files (x86)\Git\bin\

which seems to be excluding the path for javac.


Solution

  • Most likely reason of seeing different path:

    You launch VS (and correspondingly application) from desktop shortcut (or start menu) which uses default path. You launch command prompt with additional CMD configuration file which adds more paths.

    Fixes:

    • update system-wide PATH variable to include javac path
    • launch VS (devenv.exe) from the same command prompt you get correct PATH variable from
    • launch your program from the same command prompt you get correct PATH variable from (instead of VS).