start jar with Jline using Process and redirected stdin,stdout,stderr

58 Views Asked by At

I have the following problem. I'm trying to make a wrapper for interacting with the Microchip mdb debugger. Which is actually a java application. When trying to redirect stdin and stdout, it throws an error:

>?????? 03, 2023 4:19:47 PM org.jline.utils.Log logr
WARNING: Unable to create a system terminal, creating a dumb terminal (enable debug logging for more information)

I understand that the problem is that jline is waiting for something from the terminal for its logging and does not receive it, since we redirected stdin and stdout. If I remove the stdin/stdout redirection, then have started child console window with the debugger launched and prompt. So what could i do to solve that?

There is my code. (I simplify it just a little using string constants instead of getting them programmatically)

static void Main(string[] args)
{
    var mplabx_dir = @"C:\PROGRA~1\MICROC~1\MPLABX\v6.15\MPLAB_~1";
    var mdb_jar = @$"{mplabx_dir}\lib\mdb.jar";
    var jdk_home = @$"C:\Program Files\Microchip\MPLABX\v6.15\sys\java\zulu8.64.0.19-ca-fx-jre8.0.345-win_x64\bin\java.exe";
    var startInfo = new ProcessStartInfo();
    
    // I've tried to execute it through cmd.exe with the same result
    // startInfo.FileName = "cmd.exe";  
    // startInfo.ArgumentList.Clear();
    // startInfo.ArgumentList.Add("/C");
    // startInfo.ArgumentList.Add(jdk_home);
    
    startInfo.FileName = jdk_home;
    startInfo.ArgumentList.Clear();
    
    startInfo.ArgumentList.Add("-Dfile.encoding=UTF-8");
    startInfo.ArgumentList.Add("-jar");
    startInfo.ArgumentList.Add($"{mdb_jar}");
    startInfo.UseShellExecute = false;
    startInfo.CreateNoWindow = true;
    startInfo.RedirectStandardOutput = true;
    startInfo.RedirectStandardError = true;
    startInfo.RedirectStandardInput = true;
    startInfo.StandardErrorEncoding = System.Text.Encoding.ASCII;
    startInfo.StandardInputEncoding = System.Text.Encoding.ASCII;
    startInfo.StandardOutputEncoding = System.Text.Encoding.ASCII;
    
    commandsToSend = new List<string>();
    commandsToSend.Add("help");
    commandsToSend.Add("quit");
    commandsToSend.Add("");
    
    using (Process mdb = Process.Start(startInfo))
    {
        int cmd_idx = 0;
        string from_mdb;
        var ticks = DateTime.Now.Ticks;
        while (mdb.Responding)
        {
            from_mdb = ReadLineNb(mdb.StandardOutput);
            from_mdb += ReadLineNb(mdb.StandardError);
            if (from_mdb != "")
                Console.Write(from_mdb);
    
            var currTicks = DateTime.Now.Ticks;
            if((currTicks - ticks) >= 500)
            {
                ticks = currTicks;
                var to_mdb = commandsToSend[cmd_idx++];
                Console.Write(to_mdb);
                mdb.StandardInput.WriteLine(commandsToSend[cmd_idx++]);
            }
        }
    }
}

static string ReadLineNb(TextReader stream)
{
    var result = new StringBuilder();
    while (stream.Peek() > 0)
    {
        result.Append((char)stream.Read());
    }
    return result.ToString();
}

0

There are 0 best solutions below