开发者

Java: how to execute a process which is not attached to a Windows console?

Both, Runtime.exec() as well as ProcessBuilder seem to attach a console to the started process. On Windows 7, one can see a conhost.exe popping up in the Task Manager. My problem is now that the C process I开发者_StackOverflow社区'm trying to start performs following test to determine whether it has a console window to which it can issue prompts:

HANDLE cons = CreateFile("CONOUT$", GENERIC_WRITE,
            FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
            FILE_ATTRIBUTE_NORMAL, NULL);

if (cons != INVALID_HANDLE_VALUE) {
   // Prompt user; this makes my application hang
}

Is it possible with Java to start the C process in a way that upper test fails in order to avoid the prompt?


At least on OpenJDK 6, CreateProcess is being called with CREATE_NO_WINDOW. I would imagine that the Sun JDK's code is pretty similar. This makes me wonder if something else is causing that console to be present. Have you tried running your program with javaw.exe instead of java.exe?

Thinking outside of the box, maybe JGit is a better way to solve your particular problem.


Try this:

ProcessBuilder pb = new ProcessBuilder( "cmd", "/C start /B myprogram.exe param1 param2" );

The /B flag tells start to not create a new console, though I don't know whether or not start itself will end up allocating a console when called from Java.


Using Runtime is tricky cause you need to consume the output and input with streams.. See this article:

http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

Instead try using the exec library of apache commons.. Here's something that will get you started:

http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜