11

I'm trying to run dot net console application via Java:

process = Runtime.getRuntime().exec(commandLine);

I get the following output:

Detecting
The handle is invalid.

when running it directly via the console (windows) there is no problem:

Detecting
100%
Done.
100%

I'm running more applications in this form but have no problem .

Got this stack trace:

Detecting at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
 at System.Console.GetBufferInfo(Boolean throwOnNoConsole, Boolean& succeeded)
 at System.Console.get_CursorTop()
 at AutomaticImageOrientation.HelperClasses.General.WriteProgressToConsole(Int32 lastIndex, Int32 totalImages)
 at AutomaticImageOrientation.MainManager.DetectImage(String[] files, String outputPath, String& globalErrorMessage, Dictionary`2& foundRotations)

The problem is when the .net app trying to write to the console What is the solution?

found the line that cause the problem:

Console.CursorLeft = 0;

Do you know why?

Mechanical snail
  • 29,755
  • 14
  • 88
  • 113

6 Answers6

12

The console application is trying to set the cursor position for a console. This isn't possible, since there is in fact no console. All operations which don't result in a simple read or write are likely to cause errors when there is no console (since most of them require a console output buffer to work).

It is a bad idea to do stuff like setting the cursor position or clearing the screen in console applications you wish to automate. A basic workaround is to just put the offending statement in a try-catch, and discard the exception. From the MSDN page on System.Console:

You should not use the Console class to display output in unattended applications, such as server applications. Similarly, calls to methods such as Write and WriteLine have no effect in Windows applications.

Console class members that work normally when the underlying stream is directed to a console might throw an exception if the stream is redirected, for example, to a file. Consequently, program your application to catch System.IO.IOException if you redirect a standard stream.

Community
  • 1
  • 1
waqas
  • 10,323
  • 2
  • 20
  • 11
  • 1
    Why is there no console when it's automated? Is there a way to programmatically check (other than with an exception) whether the console window exists? – xr280xr Mar 31 '11 at 18:24
  • 1
    @xr280xr Updated answer with MSDN quote and reference. Catching the exception is how you can detect it in a .NET application. Or you can call the C Windows API function GetConsoleWindow(), documented here: http://msdn.microsoft.com/en-us/library/ms683175(v=vs.85).aspx – waqas Apr 13 '11 at 07:11
1

I ran into the same problem, only it was from running a c# console application via the SQL task scheduler.

I believe the problem is that some console methods and properties (Console.WindowWidth, Console.CursorLeft) are attempting to manipulate the console output, which isn't possible when the console is redirected.

I wrapped the section of the code in a simple try catch block, and it works fine now.

//This causes the output to update on the same line, rather than "spamming" the output down the screen.
//This is not compatible with redirected output, so try/catch is needed.
try
{
    int lineLength = Console.WindowWidth - 1;
    if (message.Length > lineLength)
    {
        message = message.Substring(0, lineLength);
    }

    Console.CursorLeft = 0;
    Console.Write(message);
}
catch 
{
    Console.WriteLine(message);
}
Korey
  • 929
  • 1
  • 7
  • 14
0

Hard to diagnose without more detail - perhaps permissions... a little bit of exception handling (perhaps writing stack-trace to stderr) would help enormously. but not much help if you don't own the app.

If you don't get anywhere, you could try using reflector to see what the .NET app is doing during "Detecting" - it might help identify the cause.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    I found the line: Console.CursorLeft = 0; do you why it causes to handle problem ? (only when running it via Java) –  Nov 29 '08 at 13:20
  • @Shaul - maybe it relates to stream redirection? i.e. it can't find the native IO buffer? Freaky... not sure that helps you much... – Marc Gravell Nov 30 '08 at 23:56
0

I don't think there is a problem with your friend's program. You probably need to get the output stream of the process object you receive from Runtime.getRuntime().exec(commandLine), and call a read() method or something. It might work.

Hosam Aly
  • 41,555
  • 36
  • 141
  • 182
0

Try this to get the output stream of the call command

Runtime r = Runtime.getRuntime();
mStartProcess = r.exec(applicationName, null, fileToExecute);

StreamLogger outputGobbler = new StreamLogger(mStartProcess.getInputStream());
outputGobbler.start();

int returnCode = mStartProcess.waitFor();


class StreamLogger extends Thread{

   private InputStream mInputStream;

   public StreamLogger(InputStream is) {
        this.mInputStream = is;
    }

   public void run() {
        try {
            InputStreamReader isr = new InputStreamReader(mInputStream);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null) {
                    System.out.println(line);
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

}
Markus Lausberg
  • 12,177
  • 6
  • 40
  • 66
0

That can depend on how your Java application is being run. If your java application doesn't have console, when there can be problems when console presence is necessary for your inner process. Read this.

okutane
  • 13,754
  • 10
  • 59
  • 67