“句柄无效”当通过java运行.net控制台 [英] "The handle is invalid" when running .net console via java

查看:591
本文介绍了“句柄无效”当通过java运行.net控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过Java运行点网络控制台应用程序:

  process = Runtime.getRuntime命令行); 

我得到以下输出:

 检测
句柄无效。

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

 检测
100%
完成。
100%

我以这种形式运行更多的应用程序,但没有问题。 / p>

有这个堆栈跟踪:

 检测System.IO。 .WinIOError(Int32 errorCode,String maybeFullPath)
在System.Console.GetBufferInfo(Boolean throwOnNoConsole,Boolean& succeeded)
在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)
pre>

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



找到导致问题的行:

  Console.CursorLeft = 0;你是否知道为什么?



$ b解决方案

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



这是一个坏主意,做一些事情,如设置光标位置或清除控制台应用程序,你想要自动化的屏幕。一个基本的解决方法是只是把违规的语句放在一个try-catch,并放弃该异常。从系统控制台上的 MSDN页面:


您不应使用控制台类
在无人值守的
应用程序(如服务器
应用程序)中显示输出。类似地,调用
方法(如Write和WriteLine
)在Windows
应用程序中没有效果。



控制台类成员工作
正常情况下,当底层流是
指向控制台可能会抛出一个
异常,如果流被重定向,例如
到一个文件。因此,
编程你的应用程序以捕获
System.IO.IOException如果你重定向
一个标准流。



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?

解决方案

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.

这篇关于“句柄无效”当通过java运行.net控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆