locked
Calling Java from C# using Process command RRS feed

  • Question

  • I am using the C# Process class to run a java.exe with the rest of the information being supplied as arguments to the Process.   Everything runs just fine except when I pass the incorrect number of arguments. The test still passes because it thinks java.exe succeeded when in fact the MyClass.class file failed.  Is there a way to detect the error within the MyClass.class file using the Process command or is there a better way to execute this.   I was thinking maybe a batch file.   Any ideas on how to detect when a class file has a failure?

    Thanks,

    Mike M.
    Friday, September 4, 2009 7:11 PM

Answers

  • Depending on how java.exe is returning the error information, you have a couple of options:

    You could look at the process.ExitCode for an error code.

    Alternatively, you may need to redirect the process standard output and standard error streams, and read the information from there.  This would work via ProcessStartInfo.RedirectStandardOutput / StandardError.  See the MSDN page linked for a code sample.
    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by Ji.Zhou Friday, September 11, 2009 3:43 AM
    Friday, September 4, 2009 7:35 PM
  • Hello Mike,

     

    If you want to use process.ExitCode, you must modify the Jave .exe application. In your MyClass.class file, if you detect the wrong numbers of parameters, you can call System.exit(exitCode) to terminate the process. Then you will get the exitCode in your C# Unit Test. Then, you can use Assert.AreEqual(exitCode, expectedValue) in the C# Unit Test to make sure it will fail when wrong numbers of parameters are passed into Jave application.

     

     

    Ji Zhou

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Ji.Zhou Friday, September 11, 2009 3:43 AM
    Monday, September 7, 2009 7:49 AM

All replies

  • Depending on how java.exe is returning the error information, you have a couple of options:

    You could look at the process.ExitCode for an error code.

    Alternatively, you may need to redirect the process standard output and standard error streams, and read the information from there.  This would work via ProcessStartInfo.RedirectStandardOutput / StandardError.  See the MSDN page linked for a code sample.
    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by Ji.Zhou Friday, September 11, 2009 3:43 AM
    Friday, September 4, 2009 7:35 PM
  • Hello Mike,

     

    If you want to use process.ExitCode, you must modify the Jave .exe application. In your MyClass.class file, if you detect the wrong numbers of parameters, you can call System.exit(exitCode) to terminate the process. Then you will get the exitCode in your C# Unit Test. Then, you can use Assert.AreEqual(exitCode, expectedValue) in the C# Unit Test to make sure it will fail when wrong numbers of parameters are passed into Jave application.

     

     

    Ji Zhou

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Ji.Zhou Friday, September 11, 2009 3:43 AM
    Monday, September 7, 2009 7:49 AM