Anto
Anto

Reputation: 4305

closing a command line window in Java

I have created a small Java application in which there is some code that executes a batch file. The execution of the batch file leads to the command line window to be opened and to display some output messages. I would like to know if there is some way in Java to call this command line window to be closed from within the program...Thanks!

Upvotes: 2

Views: 5384

Answers (4)

user85421
user85421

Reputation: 29680

Start java by using javaw or javaw.exe.

java (java.exe) runs with an associated console window,
javaw (javaw.exe) is the same but without the console window.

see the documentation for the java command.

On Windows also use start to invoke another shell

start javaw ClassName

I'm not sure for Linux&Co. Try using an & after the command to run it in the background

javaw ClassName &

The other way, closing the window from a batch started by Java:
I don't believe that is possible directly from within Java. You can only close the batch file by itself.
Is hard to help without knowing what that batch file is doing. You may try using the start command on windows or the & in Linux to run the process in the background (start has an option to open the window minimized [/MIN] or in the background [/B]). Or try some hack like using AutoHotKey or some system functionality (some WinAPI-DLL or equivalent in other systems).

Upvotes: 2

ch4nd4n
ch4nd4n

Reputation: 4197

start /b [bat file name]

Upvotes: 0

NimChimpsky
NimChimpsky

Reputation: 47280

the command window should close automatically when the batch file completes.

and to run a batch file in background/invisibly, check other questions

Upvotes: 2

sleske
sleske

Reputation: 83577

As an addition to NimChimpsky's answer:

If you run a batch file in Windows, Windows will automatically open a command window for the batch file, in case the batch file wants to print output or prompt for input. This also applies if you launch the bat file from a Java process.

Unfortunately, Windows itself apparently provides no way to launch a batch file without such a window. To avoid the window, you will have to run the batch file via some helper program. There are several available; google for "run bat no window" to find some.

If you just want the window to go away after the batch file terminates: That should happen automatically. If it does not, some program launched by the batch file is still running.

Upvotes: 1

Related Questions