Пример #1
0
        private void FindJdk()
        {
            string programFiles;
            string programFilesX86;

            if (Environment.Is64BitProcess)
            {
                programFiles    = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
                programFilesX86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
            }
            else
            {
                programFiles    = Environment.ExpandEnvironmentVariables("%ProgramW6432%");
                programFilesX86 = Environment.ExpandEnvironmentVariables("%ProgramFiles(x86)%");
            }

            string environmentVariable = Environment.GetEnvironmentVariable("path");

            if (!string.IsNullOrWhiteSpace(environmentVariable))
            {
                string[] path = environmentVariable.Split(';');
                string   jdk  = path.FirstOrDefault(p => p.Contains("jdk"));
                if (!string.IsNullOrWhiteSpace(jdk))
                {
                    JdkPath = jdk;
                    return;
                }
            }
            string javaPath86 = Path.Combine(programFilesX86, "Java");
            string java       = JdkHelper.SearchJavaPath(javaPath86);

            if (java != null)
            {
                JdkPath = java;
                return;
            }
            string javaPath = Path.Combine(programFiles, "Java");

            java = JdkHelper.SearchJavaPath(javaPath);
            if (java != null)
            {
                JdkPath = java;
                return;
            }

            throw new CompileException("Java Development Kit (JDK) could not be found on this System!");
        }
Пример #2
0
        public async Task <ICompileResult> Compile()
        {
            ToValidCode();

            string tmp = Path.Combine(Path.GetTempPath(), $"{ClassName}.java");

            File.WriteAllText(tmp, SourceCode);

            var runResult = await ExecuteThreaded <string> .Execute(
                () => JdkHelper.CompileJava(JavacPath, tmp, CompilerProperties), (int)CompilerProperties.Timeout
                );

            string output = runResult.ReturnValue;
            var    error  = runResult.Exception;
            int    time   = runResult.ElapsedMilliseconds;

            IEnumerable <IDiagnostic> diagnostics = null;
            IEnumerable <Exception>   errors      = null;

            if (!string.IsNullOrWhiteSpace(output))
            {
                diagnostics = new List <IDiagnostic> {
                    new JavaDiagnostic(output, -1, -1, -1, -1, Severity.Info)
                }
            }
            ;
            if (error != null)
            {
                errors = new List <Exception> {
                    error
                }
            }
            ;

            var result =
                new JavaCompileResult(time, SourceCode, diagnostics, null, errors);

            CompileResult = result;
            return(result);
        }