0

I am running a batch file that

  1. Opens CMD
  2. CD to HSQLDB home directory
  3. Start Database

The database server starts in a CMD window. I have added a Shutdownhook in my program. Inside it's run method I need to close the server/CMD window. I have tried destroy method on the Processobject that starts the Server. I think the process instance won't know about the CMD window that was opened by running the bat file. I can't figure out how to do it. I have little or no knowledge about batch files. And I don't know if it is possible using Java all alone.

My command line is quite basic. The content is

START cmd.exe /k "cd C:\Users\aman.jangra\Downloads\hsqldb-2.5.0\hsqldb & java -cp lib/hsqldb.jar org.hsqldb.server.Server --database.0 file:data/mydb --dbname.0 Test"

The cmd window starts the database server in the same window. It can be manually closed i.e. server can be manually closed by pressing Ctrl+C or closing the window but I want to do it when my program exits.

Compo
  • 36,585
  • 5
  • 27
  • 39
Aman jangra
  • 268
  • 1
  • 16
  • 2
    The option `/k` means __keep__ command process running after finishing execution of the command line. I suggest to change this `cmd.exe` option to `/C` to __close__ command process after finishing execution of the command line. – Mofi Dec 24 '19 at 08:37
  • That didn't work. The cmd window starts the database server in the same window. It can be manually closed i.e. server can be manually closed by pressing Ctrl+C or closing the window but I want to do it when my program exits. – Aman jangra Dec 24 '19 at 08:40
  • I suggest further to open a [command prompt](https://www.howtogeek.com/235101/), run `cmd /?` and read the output help. Next run `start /?` and read again the output help. You can read that the command `start` has an option `/D` to specify the directory in which the executable should be started, i.e. which should be the current directory for the executable on starting it. Taking this into account try: `start "Database server" /D"%UserProfile%\Downloads\hsqldb-2.5.0\hsqldb" java.exe -cp lib/hsqldb.jar org.hsqldb.server.Server --database.0 file:data/mydb --dbname.0 Test` – Mofi Dec 24 '19 at 08:41
  • If it is a console program, your program, then closing the console will terminate the program. This is computer user stuff so ask on https://superuser.com. –  Dec 24 '19 at 08:44
  • If you don't want to see a console window with `Database server` as title, I suggest to use `javaw.exe` instead of `java.exe`, see [Difference between java/javaw/javaws](https://stackoverflow.com/questions/8194713/). The command line could be in this case `start "" /D"%UserProfile%\Downloads\hsqldb-2.5.0\hsqldb" javaw.exe -cp lib/hsqldb.jar org.hsqldb.server.Server --database.0 file:data/mydb --dbname.0 Test`. It would be advisable to reference `javaw.exe` with full path, i.e. full qualified file name (drive + path + name + extension) enclosed in `"` if the location of `javaw.exe` is fixed. – Mofi Dec 24 '19 at 08:48
  • @Mofi After adding the title to the window, Is it possible to get all the CMD instances running from Java code somehow and than when the title Database Server is encountered, I kill it. Will try this – Aman jangra Dec 24 '19 at 08:51
  • You should avoid to use a batch file and `cmd.exe` at all. That is not necessary. Your Java application could use [class Process](https://docs.oracle.com/javase/8/docs/api/java/lang/Process.html) to run directly `javaw.exe` with the arguments and the working directory set for this process with [ProcessBuilder.directory](https://docs.oracle.com/javase/8/docs/api/java/lang/ProcessBuilder.html). Then `Process.destroy` (graceful termination) or `Process.destroyForcibly` (brutal kill, not advisable if it can be avoided) could be used to terminate (or kill) the database server process. – Mofi Dec 24 '19 at 09:07
  • If you do not want to use the `/D` option of `start`, you should replace `cd` by `cd /D`... – aschipfl Dec 24 '19 at 09:37

0 Answers0