How to display printout of JAVA log to C # console
I am trying to display information printed out on the command line from a JAVA console in C #. It works fine when System.out.println is issued in JAVA, however it does not work with logger.info printing.
Note. When I run it via Windows Command Prompt (CMD> java -jar testbed.jar), the logger information is printed on the command prompt.
Appreciate the inputs and help from anyone on this. The test code I'm working on is the following:
JAVA to make a prime number increment and decrement and print through the logger when the button is pressed to increment or decrement. (THIS CODE CANNOT CHANGE)
private static int ValueInteger = 0 ;
static Logger l;
public TestBed() {
initComponents();
try{
l = Logger.getLogger("");
FileHandler fh = new FileHandler("main.log", false);
l.addHandler(fh);
l.setLevel(Level.ALL);
l.fine("Logger Created - JTSRunner.main()");
}catch(Exception ex){}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
ValueInteger--;
// System.out.println(Integer.toString(ValueInteger));
l.info(Integer.toString(ValueInteger));
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
ValueInteger++;
// System.out.println(Integer.toString(ValueInteger));
l.info(Integer.toString(ValueInteger));
}
C # code to handle and print in a textbox (CODE I WORK):
private void Button_Click(object sender, RoutedEventArgs e)
{
ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/k java -jar Testbed.jar ")
{
WorkingDirectory = "C:\\Testbed",
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
};
Process process = Process.Start(startInfo);
process.OutputDataReceived += writeCommandInfo;
process.BeginOutputReadLine();
//process.WaitForExit();
}
private void writeCommandInfo(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
this.Text.Dispatcher.Invoke(new Action(() => this.Text.Text = e.Data.ToString()), DispatcherPriority.Normal, null);
}
}
source to share
The java.util.logging
default properties file configures the ConsoleHandler, which registers messages to System.err
( source ) and therefore messages appear on the command line. But since your C # code is redirecting standard version and not standard error ( RedirectStandardError
), the process cannot capture messages.
Try setting the property RedirectStandardError
to true.
source to share