通过java运行.net控制台时“句柄无效”

我正在尝试通过Java运行dot net console应用程序:

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

我得到以下输出:

 Detecting The handle is invalid. 

当通过控制台(windows)直接运行它时没有问题:

 Detecting 100% Done. 100% 

我正在以这种forms运行更多的应用程序,但没有问题。

得到了这个堆栈跟踪:

 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) 

问题是当.net应用程序尝试写入控制台时有什么解决方案?

找到导致问题的那一行:

 Console.CursorLeft = 0; 

你知道为什么吗?

控制台应用程序正在尝试为控制台设置光标位置。 这是不可能的,因为实际上没有控制台。 所有不会导致简单读取或写入的操作都可能在没有控制台时导致错误(因为大多数操作都需要控制台输出缓冲区才能工作)。

在您想要自动化的控制台应用程序中设置光标位置或清除屏幕等操作是个坏主意。 一个基本的解决方法是将违规语句放在try-catch中,并丢弃该exception。 从System.Console上的MSDN页面 :

您不应使用Console类在无人参与的应用程序(如服务器应用程序)中显示输出。 同样,对Write和WriteLine等方法的调用对Windows应用程序没有影响。

在将基础流定向到控制台时正常工作的控制台类成员可能会在将流重定向(例如,重定向到文件)时引发exception。 因此,如果重定向标准流,请对应用程序进行编程以捕获System.IO.IOException。

我遇到了同样的问题,只是通过SQL任务调度程序运行ac#console应用程序。

我认为问题是某些控制台方法和属性(Console.WindowWidth,Console.CursorLeft)正在尝试操作控制台输出,这在重定向控制台时是不可能的。

我将代码的一部分包装在一个简单的try catch块中,现在它工作正常。

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

很难在没有更多细节的情况下进行诊断 – 可能是权限……一点点exception处理(可能将堆栈跟踪写入stderr)会有很大帮助。 但如果您不拥有该应用程序,则没有多大帮助。

如果你没有到达任何地方,你可以尝试使用reflection器来查看.NET应用程序在“检测”期间正在做什么 – 它可能有助于确定原因。

我不认为你朋友的节目有问题。 您可能需要获取从Runtime.getRuntime()。exec(commandLine)接收的进程对象的输出流,并调用read()方法或其他东西。 它可能会奏效。

尝试此操作以获取call命令的输出流

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

这可能取决于您的Java应用程序的运行方式。 如果您的Java应用程序没有控制台,那么当您的内部进程需要控制台存在时可能会出现问题。 读这个 。