OpenJudgeSystem icon indicating copy to clipboard operation
OpenJudgeSystem copied to clipboard

Implement Java compiler

Open NikolayIT opened this issue 12 years ago • 1 comments

NikolayIT avatar Aug 22 '13 14:08 NikolayIT

Old code:

public class JavaCompiler : ICompiler
{
    public void Compile(Uri inputPath, Uri outputPath)
    {
        Uri compilerPath = new Uri(Settings.JavaCompilerPath);

        if (!File.Exists(inputPath.LocalPath))
        {
            throw new CompilerException(string.Format("Java source file not found! Searched in: \"{0}\"",
                inputPath.LocalPath));
        }

        if (!File.Exists(compilerPath.LocalPath))
        {
            throw new CompilerException(string.Format("Java compiler not found! Searched in: \"{0}\"",
                compilerPath.LocalPath));
        }

        ProcessStartInfo processStartInfo =
            new ProcessStartInfo(compilerPath.LocalPath);
        processStartInfo.RedirectStandardError = true;
        processStartInfo.UseShellExecute = false;

        processStartInfo.Arguments += string.Format("\"{0}\"", inputPath.LocalPath);

        string error;
        using (Process process = Process.Start(processStartInfo))
        {
            error = process.StandardError.ReadToEnd();
            process.WaitForExit();
        }

        string generatedOutput = inputPath.LocalPath.Substring(0, 
            inputPath.LocalPath.Length - ".java".Length) + ".class";

        if (string.IsNullOrEmpty(error))
        {
            if (File.Exists(generatedOutput))
            {
                File.Move(generatedOutput, outputPath.LocalPath);
            }
        }
        else
        {
            throw new CompilerException(error);
        }
    }
}

NikolayIT avatar Aug 22 '13 14:08 NikolayIT