views:

907

answers:

5

Hi,

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)\n at
System.Console.GetBufferInfo(Boolean throwOnNoConsole, Boolean& succeeded)\n at 
System.Console.get_CursorTop()\n at AutomaticImageOrientation.HelperClasses.General.
WriteProgressToConsole(Int32 lastIndex, Int32 totalImages)\n 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 ?

Thanks in advance, Shaul.

A: 

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
I found the line:Console.CursorLeft = 0;do you why it causes to handle problem ? (only when running it via Java)
@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
A: 

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
A: 

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.

waqas
A: 

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
A: 

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.

Dmitriy Matveev