Top Description Fields Constructors Methods
org.h2.tools

public Class Server

extends Tool
implements Runnable, ShutdownHandler
Class Inheritance
All Implemented Interfaces
org.h2.server.ShutdownHandler, java.lang.Runnable
Imports
java.net.URI, java.sql.Connection, .SQLException, org.h2.api.ErrorCode, org.h2.engine.SysProperties, org.h2.message.DbException, org.h2.server.Service, .ShutdownHandler, .TcpServer, org.h2.server.pg.PgServer, org.h2.server.web.WebServer, org.h2.util.StringUtils, .Tool, .Utils

Starts the H2 Console (web-) server, TCP, and PG server.

Field Summary

Modifier and TypeField and Description
private boolean
private Server
private final Service
private ShutdownHandler
private boolean
private Server
private Server
Inherited from org.h2.util.Tool:
out

Constructor Summary

AccessConstructor and Description
public
Server()

Generic constructor

public
Server(Service
the service
service
,
String...
the command line arguments
args
)

Create a new server for the given service.

Method Summary

Modifier and TypeMethod and Description
public static Server

Returns:

the server
createPgServer
(String...
the argument list
args
)

Create a new PG server, but does not start it yet.

public static Server

Returns:

the server
createTcpServer
(String...
the argument list
args
)

Create a new TCP server, but does not start it yet.

public static Server

Returns:

the server
createWebServer
(String...
the argument list
args
)

Create a new web server, but does not start it yet.

pack-priv static Server

Returns:

the server
createWebServer
(String[]
the argument list
args
,
String
key, or null
key
,
boolean
whether creation of databases using the key should be allowed
allowSecureCreation
)

Create a new web server, but does not start it yet.

public int

Returns:

the port
getPort
()

Gets the port this server is listening on.

public Service

Returns:

the service
getService
()

Get the service attached to this server.

public String

Returns:

the status
getStatus
()

Get the status of this server.

public String

Returns:

the url
getURL
()

Gets the URL of this server.

public boolean

Returns:

if the server is running
isRunning
(boolean
if errors should be written
traceError
)

Checks if the server is running.

public static void
main(String...
the command line arguments
args
)

When running without options, -tcp, -web, -browser and -pg are started.

public static void
openBrowser(String
the URL to open
url
)

Open a new browser tab or window with the given URL.

public void
run()

Implements java.lang.Runnable.run.

INTERNAL

public void
runTool(String...
the argument list
args
)

Implements abstract org.h2.util.Tool.runTool.

Run the tool with the given output stream and arguments.

public void
setShutdownHandler(ShutdownHandler
to set
shutdownHandler
)

INTERNAL

public void
public static void
shutdownTcpServer(String
example: tcp://localhost:9094
url
,
String
the password to use ("" for no password)
password
,
boolean
the shutdown (don't wait)
force
,
boolean
whether all TCP servers that are running in the JVM should be stopped
all
)

Shutdown one or all TCP server.

public Server

Returns:

the server if successful
start
()

Tries to start the server.

public static void
startWebServer(Connection
the database connection (the database must be open)
conn
)

Start a web server and a browser that uses the given connection.

public static void
startWebServer(Connection
the database connection (the database must be open)
conn
,
boolean
if true properties from .h2.server.properties will be ignored
ignoreProperties
)

Start a web server and a browser that uses the given connection.

public void
stop()

Stops the server.

private void
private void
verifyArgs(String... args)

private static void
wait(int i)

Inherited from org.h2.util.Tool:
getMainClassNameisOptionprintNoDatabaseFilesFoundsetOutshowUsageshowUsageAndThrowUnsupportedOptionthrowUnsupportedOption

Field Detail

fromCommandLineback to summary
private boolean fromCommandLine
pgback to summary
private Server pg
serviceback to summary
private final Service service
shutdownHandlerback to summary
private ShutdownHandler shutdownHandler
startedback to summary
private boolean started
tcpback to summary
private Server tcp
webback to summary
private Server web

Constructor Detail

Serverback to summary
public Server()

Generic constructor

Serverback to summary
public Server(Service service, String... args) throws SQLException

Create a new server for the given service.

Parameters
service:Service

the service

args:String[]

the command line arguments

Exceptions
SQLException:
on failure

Method Detail

createPgServerback to summary
public static Server createPgServer(String... args) throws SQLException

Create a new PG server, but does not start it yet. Example:

Server server =
    Server.createPgServer("-pgAllowOthers").start();
Supported options are: -pgPort, -pgAllowOthers, -pgDaemon, -trace, -ifExists, -ifNotExists, -baseDir, -key. See the main method for details.

If no port is specified, the default port is used if possible, and if this port is already used, a random port is used. Use getPort() or getURL() after starting to retrieve the port.

Parameters
args:String[]

the argument list

Returns:Server

the server

Exceptions
SQLException:
on failure
createTcpServerback to summary
public static Server createTcpServer(String... args) throws SQLException

Create a new TCP server, but does not start it yet. Example:

Server server = Server.createTcpServer(
    "-tcpPort", "9123", "-tcpAllowOthers").start();
Supported options are: -tcpPort, -tcpSSL, -tcpPassword, -tcpAllowOthers, -tcpDaemon, -trace, -ifExists, -ifNotExists, -baseDir, -key. See the main method for details.

If no port is specified, the default port is used if possible, and if this port is already used, a random port is used. Use getPort() or getURL() after starting to retrieve the port.

Parameters
args:String[]

the argument list

Returns:Server

the server

Exceptions
SQLException:
on failure
createWebServerback to summary
public static Server createWebServer(String... args) throws SQLException

Create a new web server, but does not start it yet. Example:

Server server = Server.createWebServer("-trace").start();
Supported options are: -webPort, -webSSL, -webAllowOthers, -webDaemon, -trace, -ifExists, -ifNotExists, -baseDir, -properties. See the main method for details.
Parameters
args:String[]

the argument list

Returns:Server

the server

Exceptions
SQLException:
on failure
createWebServerback to summary
pack-priv static Server createWebServer(String[] args, String key, boolean allowSecureCreation) throws SQLException

Create a new web server, but does not start it yet.

Parameters
args:String[]

the argument list

key:String

key, or null

allowSecureCreation:boolean

whether creation of databases using the key should be allowed

Returns:Server

the server

getPortback to summary
public int getPort()

Gets the port this server is listening on.

Returns:int

the port

getServiceback to summary
public Service getService()

Get the service attached to this server.

Returns:Service

the service

getStatusback to summary
public String getStatus()

Get the status of this server.

Returns:String

the status

getURLback to summary
public String getURL()

Gets the URL of this server.

Returns:String

the url

isRunningback to summary
public boolean isRunning(boolean traceError)

Checks if the server is running.

Parameters
traceError:boolean

if errors should be written

Returns:boolean

if the server is running

mainback to summary
public static void main(String... args) throws SQLException

When running without options, -tcp, -web, -browser and -pg are started. Options are case sensitive.

Supported options
[-help] or [-?] Print the list of options
[-web] Start the web server with the H2 Console
[-webAllowOthers] Allow other computers to connect - see below
[-webExternalNames <names>] The comma-separated list of external names and IP addresses of this server, used together with -webAllowOthers
[-webDaemon] Use a daemon thread
[-webVirtualThreads <true|false>] Use virtual threads (on Java 21+ only)
[-webPort <port>] The port (default: 8082)
[-webSSL] Use encrypted (HTTPS) connections
[-webAdminPassword] Hash of password of DB Console administrator, can be generated with WebServer#encodeAdminPassword(String). Can be passed only to the runTool(String...) method, this method rejects it. It is also possible to store this setting in configuration file of H2 Console.
[-browser] Start a browser connecting to the web server
[-tcp] Start the TCP server
[-tcpAllowOthers] Allow other computers to connect - see below
[-tcpDaemon] Use a daemon thread
[-tcpVirtualThreads <true|false>] Use virtual threads (on Java 21+ only)
[-tcpPort <port>] The port (default: 9092)
[-tcpSSL] Use encrypted (SSL) connections
[-tcpPassword <pwd>] The password for shutting down a TCP server
[-tcpShutdown "<url>"] Stop the TCP server; example: tcp://localhost
[-tcpShutdownForce] Do not wait until all connections are closed
[-pg] Start the PG server
[-pgAllowOthers] Allow other computers to connect - see below
[-pgDaemon] Use a daemon thread
[-pgVirtualThreads <true|false>] Use virtual threads (on Java 21+ only)
[-pgPort <port>] The port (default: 5435)
[-properties "<dir>"] Server properties (default: ~, disable: null)
[-baseDir <dir>] The base directory for H2 databases (all servers)
[-ifExists] Only existing databases may be opened (all servers)
[-ifNotExists] Databases are created when accessed
[-trace] Print additional trace information (all servers)
[-key <from> <to>] Allows to map a database name to another (all servers)
The options -xAllowOthers are potentially risky. For details, see Advanced Topics / Protection against Remote Access.
Parameters
args:String[]

the command line arguments

Exceptions
SQLException:
on failure
openBrowserback to summary
public static void openBrowser(String url) throws Exception

Open a new browser tab or window with the given URL.

Parameters
url:String

the URL to open

Exceptions
Exception:
on failure
runback to summary
public void run()

Implements java.lang.Runnable.run.

INTERNAL

Annotations
@Override
runToolback to summary
public void runTool(String... args) throws SQLException

Implements abstract org.h2.util.Tool.runTool.

Doc from org.h2.util.Tool.runTool.

Run the tool with the given output stream and arguments.

Parameters
args:String[]

the argument list

Annotations
@Override
Exceptions
SQLException:
on failure
setShutdownHandlerback to summary
public void setShutdownHandler(ShutdownHandler shutdownHandler)

INTERNAL

Parameters
shutdownHandler:ShutdownHandler

to set

shutdownback to summary
public void shutdown()

Implements org.h2.server.ShutdownHandler.shutdown.

INTERNAL

Annotations
@Override
shutdownTcpServerback to summary
public static void shutdownTcpServer(String url, String password, boolean force, boolean all) throws SQLException

Shutdown one or all TCP server. If force is set to false, the server will not allow new connections, but not kill existing connections, instead it will stop if the last connection is closed. If force is set to true, existing connections are killed. After calling the method with force=false, it is not possible to call it again with force=true because new connections are not allowed. Example:

Server.shutdownTcpServer("tcp://localhost:9094",
        password, true, false);
Parameters
url:String

example: tcp://localhost:9094

password:String

the password to use ("" for no password)

force:boolean

the shutdown (don't wait)

all:boolean

whether all TCP servers that are running in the JVM should be stopped

Exceptions
SQLException:
on failure
startback to summary
public Server start() throws SQLException

Tries to start the server.

Returns:Server

the server if successful

Exceptions
SQLException:
if the server could not be started
startWebServerback to summary
public static void startWebServer(Connection conn) throws SQLException

Start a web server and a browser that uses the given connection. The current transaction is preserved. This is specially useful to manually inspect the database when debugging. This method return as soon as the user has disconnected.

Parameters
conn:Connection

the database connection (the database must be open)

Exceptions
SQLException:
on failure
startWebServerback to summary
public static void startWebServer(Connection conn, boolean ignoreProperties) throws SQLException

Start a web server and a browser that uses the given connection. The current transaction is preserved. This is specially useful to manually inspect the database when debugging. This method return as soon as the user has disconnected.

Parameters
conn:Connection

the database connection (the database must be open)

ignoreProperties:boolean

if true properties from .h2.server.properties will be ignored

Exceptions
SQLException:
on failure
stopback to summary
public void stop()

Stops the server.

stopAllback to summary
private void stopAll()
verifyArgsback to summary
private void verifyArgs(String... args) throws SQLException
waitback to summary
private static void wait(int i)