Question

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?

No correct solution

OTHER TIPS

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.

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);
}

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.

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.

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();
        }
    }

}

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top