bool TryGetJavaJdkInfo(string javacFilename, out JdkInfo javaJdkInfo) { var args = new ProcessArgumentBuilder(); args.Append("-version"); var p = new ProcessRunner(new FileInfo(javacFilename), args); var result = p.WaitForExit(); var m = rxJavaCVersion.Match(result.GetOutput() ?? string.Empty); var v = m?.Value; if (!string.IsNullOrEmpty(v)) { javaJdkInfo = new JdkInfo(javacFilename, v); return(true); } javaJdkInfo = default; return(false); }
IEnumerable <string> run(bool withAccept, ProcessArgumentBuilder args) { if (jdk == null) { jdk = Jdks.FirstOrDefault(); } var sdkManager = FindToolPath(AndroidSdkHome); var java = jdk.Java; var libPath = Path.GetFullPath(Path.Combine(sdkManager.DirectoryName, "..", "lib")); var toolPath = Path.GetFullPath(Path.Combine(sdkManager.DirectoryName, "..")); var cpSeparator = IsWindows ? ";" : ":"; // Get all the .jars in the tools\lib folder to use as classpath //var classPath = "avdmanager-classpath.jar"; var classPath = string.Join(cpSeparator, Directory.GetFiles(libPath, "*.jar").Select(f => new FileInfo(f).Name)); var proc = new Process(); // This is the package and class that contains the main() for avdmanager proc.StartInfo.Arguments = "com.android.sdklib.tool.sdkmanager.SdkManagerCli " + args.ToString(); // This needs to be set to the working dir / classpath dir as the library looks for this system property at runtime //proc.StartInfo.Environment["JAVA_TOOL_OPTIONS"] = $"-Dcom.android.sdkmanager.toolsdir=\"{toolPath}\""; proc.StartInfo.Environment["JAVA_TOOL_OPTIONS"] = $"-Dcom.android.sdklib.toolsdir=\"{toolPath}\""; // Set the classpath to all the .jar files we found in the lib folder proc.StartInfo.Environment["CLASSPATH"] = classPath; // Java.exe proc.StartInfo.FileName = java.FullName; // lib folder is our working dir proc.StartInfo.WorkingDirectory = libPath; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.RedirectStandardError = true; proc.StartInfo.RedirectStandardInput = true; var output = new List <string>(); var stderr = new List <string>(); var stdout = new List <string>(); proc.OutputDataReceived += (s, e) => { if (!string.IsNullOrEmpty(e.Data)) { output.Add(e.Data); stdout.Add(e.Data); } }; proc.ErrorDataReceived += (s, e) => { if (!string.IsNullOrEmpty(e.Data)) { output.Add(e.Data); stderr.Add(e.Data); } }; var cmd = $"{proc.StartInfo.FileName} {proc.StartInfo.Arguments}"; proc.Start(); proc.BeginOutputReadLine(); proc.BeginErrorReadLine(); while (!proc.HasExited) { Thread.Sleep(250); try { proc.StandardInput.WriteLine("y"); proc.StandardInput.Flush(); } catch { } } proc.WaitForExit(); if (proc.ExitCode != 0) { throw new SdkToolFailedExitException("avdmanager", proc.ExitCode, stderr, stdout); } return(output); }