public static bool IsJavaRuntimeValid(JavaRuntimeEntity jre) { try { if (jre == null) { return false; } if (!File.Exists(jre.JavaPath) || !File.Exists(jre.JavaPath)) { return false; } var realDetail = GetJavaDetails(jre.JavaPath); if (realDetail.JavaType != jre.JavaDetails.JavaType || realDetail.JavaVersion != jre.JavaDetails.JavaVersion) { return false; } return true; } catch (Exception) { return false; } }
public JreManager(string configPath) { this.Config = new Config(new FileInfo(configPath)); this.SearchPaths = this.Config.GetConfigObject<List<string>>("javaSearchPaths"); var javaPaths = this.SearchPaths.Where(Directory.Exists) .SelectMany( path => Directory.GetDirectories(path, "*", SearchOption.TopDirectoryOnly), (path, folder) => Path.Combine(folder, "bin/java.exe") ).Where(File.Exists) .ToList(); foreach (var javaPath in javaPaths) { try { var runtimeEntity = new JavaRuntimeEntity() { JavaPath = javaPath, JavaWPath = Path.Combine(Path.GetDirectoryName(javaPath), "javaw.exe"), JavaDetails = JavaUtils.GetJavaDetails(javaPath) }; this.AvailableJavaRuntimes.Add( $"{runtimeEntity.JavaDetails.JavaVersion}:{runtimeEntity.JavaDetails.JavaType}", runtimeEntity); } catch (Exception ex) { Logging.TerminologyLogger.GetLogger() .WarnFormat($"Cannot indentify this java caused by {ex.Message}. Ignored!"); } } }
private bool CheckJavaPath() { //Check config if (this.Engine.JreManager.JavaRuntime != null) { return true; } var javaRuntimeEntitiesKP = new Dictionary<string, JavaRuntimeEntity>(); foreach (var availableJre in this.Engine.JreManager.AvailableJavaRuntimes) { javaRuntimeEntitiesKP.Add(availableJre.Key, availableJre.Value); } if (javaRuntimeEntitiesKP.Keys.Count != 0) { var field = new FieldReference<string>(javaRuntimeEntitiesKP.Keys.First()); var result = this.Engine.UiControl.PopupSingleSelectDialog(TranslationManager.GetManager.Localize("JavaSelectWindowTitle", "Select a Java"), TranslationManager.GetManager.Localize("JavaSelectField", "Available Java exe:"), javaRuntimeEntitiesKP.Keys, field); if (result == null || result.Value == false) { return false; } else { this.Engine.JreManager.JavaRuntime = javaRuntimeEntitiesKP[field.Value]; return true; } } while (this.Engine.JreManager.JavaRuntime == null) { TerminologyLogger.GetLogger().Warn("Java path is empty. Try to receive from user.."); var field = new FieldReference<string>(string.Empty); var result = this.Engine.UiControl.PopupSingleLineInputDialog(TranslationManager.GetManager.Localize("JavaInputWindowTitle", "Input a Java exe"), TranslationManager.GetManager.Localize("JavaInputField", "Java(not javaw) exe path:"), field); if (result == null || result.Value == false) { { try { var javaBinFolder = new DirectoryInfo(field.Value); var jre = new JavaRuntimeEntity { JavaDetails = JavaUtils.GetJavaDetails(Path.Combine(javaBinFolder.FullName, "java.exe")), JavaPath = Path.Combine(javaBinFolder.FullName, "java.exe"), JavaWPath = Path.Combine(javaBinFolder.FullName, "javaw.exe") }; if (JavaUtils.IsJavaRuntimeValid(jre)) { this.Engine.JreManager.JavaRuntime = jre; } else { continue; } } catch (Exception ex) { TerminologyLogger.GetLogger() .ErrorFormat($"cannot resolve java exe path through user input. Caused by:{ex.Message}"); continue; } break; } } else { return false; } } return true; }
private Boolean CheckJavaPath() { //Check config if (!String.IsNullOrEmpty(this.Engine.InstanceManager.Config.GetConfig("javaBinPath"))) { try { var javaBinFolder = new DirectoryInfo(this.Engine.InstanceManager.Config.GetConfig("javaBinPath")); if (javaBinFolder.Exists && File.Exists(Path.Combine(javaBinFolder.FullName, "java.exe")) || File.Exists(Path.Combine(javaBinFolder.FullName, "javaw.exe"))) { return true; } } catch (Exception) { //ignore } } //Search java from default path var searchPaths = this.Engine.CoreConfig.GetConfigs("javaSearchPaths"); var javaPaths = searchPaths.Where(Directory.Exists) .SelectMany( path => Directory.GetDirectories(path, "*", SearchOption.TopDirectoryOnly), (path, folder) => Path.Combine(folder, "bin/java.exe") ).Where(File.Exists) .ToList(); var javaRuntimeEntitiesKP = new Dictionary<String, JavaRuntimeEntity>(); foreach (var javaPath in javaPaths) { try { var runtimeEntity = new JavaRuntimeEntity() { JavaPath = javaPath, JavaWPath = Path.Combine(Path.GetDirectoryName(javaPath), "javaw.exe"), JavaDetails = JavaUtils.GetJavaDetails(javaPath) }; javaRuntimeEntitiesKP.Add(String.Format("{0}:{1}", runtimeEntity.JavaDetails.JavaVersion, runtimeEntity.JavaDetails.JavaType), runtimeEntity); } catch (Exception) { //Ignore } } if (javaRuntimeEntitiesKP.Keys.Count != 0) { var result = this.Engine.UiControl.StartSingleSelect("Select available java", "Java Runtime:", javaRuntimeEntitiesKP.Keys); if (result.Type == WindowResultType.Canceled) { return false; } else { this.Engine.InstanceManager.Config.SetConfig("javaBinPath", Directory.GetParent(javaRuntimeEntitiesKP[result.Result.ToString()].JavaWPath).FullName); return true; } } while (String.IsNullOrEmpty(this.Engine.InstanceManager.Config.GetConfig("javaBinPath"))) { Logger.GetLogger().Warn("Java path is empty. Try to receive from user.."); var result = this.Engine.UiControl.StartSingleLineInput("Request Java path", "Java Path"); switch (result.Type) { case WindowResultType.CommonFinished: { try { var javaExe = new FileInfo(result.Result.ToString()); if (javaExe.Exists && (javaExe.Name == "java.exe" || javaExe.Name == "javaw.exe")) { this.Engine.InstanceManager.Config.SetConfig("javaBinPath", javaExe.DirectoryName); Logger.GetLogger().Info("Received java path from user. Pass."); } } catch (Exception) { //ignore. } break; } case WindowResultType.Canceled: return false; } } return true; }