# Starting the Network Server

To start the Network Server, you can invoke a script, a jar file, or a class.

Important: Always shut down the Network Server properly after use, because failure to do so might result in unpredictable side effects, such as blocked ports on the server.

You are strongly urged to enable user authentication and user authorization when you run a Network Server. For details on how to configure user authentication and authorization, see "Configuring user authentication" and "Configuring user authorization" in the Derby Security Guide.

You are also urged to install a Java security manager with a customized security policy. For details on how to do this, see "Configuring Java security," also in the Derby Security Guide. By default a security manager will be installed with a default security policy when a Network Server is started. This default policy file is called `server.policy` and is part of `derbynet.jar`. There is also a `serverTemplate.policy` file in the `demo/templates` directory of the distribution. It can be used as a starting point for customizing a server policy. For more information about policy files, granting permissions, and property expansion, see "Default Policy Implementation and Policy File Syntax" at http://docs.oracle.com/javase/7/docs/technotes/guides/security/PolicyFiles.html and "Policy File Creation and Management" at http://docs.oracle.com/javase/7/docs/technotes/guides/security/PolicyGuide.html.

If you start the Derby Network Server from the command line as described here, access to databases and to other Derby files is by default restricted to the operating system account that started the Network Server. It is possible to override this default behavior. For more information, see "Restricting file permissions" in the Derby Security Guide.

You can start the Network Server in any of the following ways:

- If you are relatively new to the Java programming language, follow the instructions in "Setting up your environment" in Getting Started with Derby to set the `DERBY_HOME` and `JAVA_HOME` environment variables and to add `DERBY_HOME/bin` to your path. Then use the `startNetworkServer.bat` script to start the Network Server on Windows machines and the `startNetworkServer` script to start the Network Server on UNIX systems. These scripts are located in `$DERBY_HOME/bin`, where `$DERBY_HOME` is the directory where you installed Derby.

  You can run NetworkServerControl commands only from the host that started the Network Server. The following table shows the sequence of commands.

  Table 1. Commands to run the `startNetworkServer` command
  Operating System | Command
  --- | ---
  Windows |
      set DERBY_HOME=C:\derby
      set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_24
      set PATH=%DERBY_HOME%\bin;%PATH%
      startNetworkServer
  UNIX (Korn Shell) |
      export DERBY_HOME=/opt/derby
      export JAVA_HOME=/usr/j2se
      export PATH="$DERBY_HOME/bin:$PATH"
      startNetworkServer

- If you are a regular Java user but are new to Derby, set the `DERBY_HOME` environment variable, then use a `java` command to invoke the `derbyrun.jar` or `derbynet.jar` file, as shown in the following table.

  Table 2. Commands to invoke the Derby jar files
  Operating System | Command
  --- | ---
  Windows |
      set DERBY_HOME=C:\derby
      java -jar %DERBY_HOME%\lib\derbyrun.jar server start
      or
      java -jar %DERBY_HOME%\lib\derbynet.jar start
  UNIX (Korn Shell) |
      export DERBY_HOME=/opt/derby
      java -jar $DERBY_HOME/lib/derbyrun.jar server start
      or
      java -jar $DERBY_HOME/lib/derbynet.jar start

  To see the command syntax, invoke `derbyrun.jar server` or `derbynet.jar` with no arguments.

- If you are familiar with both the Java programming language and Derby, you have already set `DERBY_HOME`. Set your classpath/modulepath to include the Derby jar files. Then use a `java` command to invoke the `NetworkServerControl` class directly, as shown in the following tables.

  Table 3. Commands to invoke the `NetworkServerControl` class via the classpath
  Operating System | Command
  --- | ---
  Windows |
      %DERBY_HOME%\bin\setNetworkServerCP
      java org.apache.derby.drda.NetworkServerControl start
  UNIX (Korn Shell) |
      $DERBY_HOME/bin/setNetworkServerCP
      java org.apache.derby.drda.NetworkServerControl start

  Table 4. Commands to invoke the `NetworkServerControl` class via the modulepath
  Operating System | Command
  --- | ---
  Windows |
      %DERBY_HOME%\bin\setNetworkServerCP
      java -p %MODULEPATH% ^
        -m org.apache.derby.server/org.apache.derby.drda.NetworkServerControl start
  UNIX (Korn Shell) |
      $DERBY_HOME/bin/setNetworkServerCP
      java -p $MODULEPATH \
        -m org.apache.derby.server/org.apache.derby.drda.NetworkServerControl start

The default system directory is the directory in which Derby was started. (See the Derby Developer's Guide for more information about the default system directory.)

You can specify a different host or port number when you start the Network Server by specifying an option to the command.

- Specify a port number other than the default (1527) by using the `-p portnumber` option, as shown in the following example:

      java org.apache.derby.drda.NetworkServerControl start -p 1368

- Specify a specific interface (host name or IP address) to listen on other than the default (`localhost`) by using the `-h` option, as shown in the following example:

      $DERBY_HOME/bin/startNetworkServer -h myhost -p 1368

  where myhost is the host name or IP address.

  Remember: Before using the `-h` option, you should run under the Java security manager with a customized security policy and you should enable user authentication.

By default, the Network Server will listen to requests only on the loopback address, which means that it will only accept connections from the local host.

- Starting the Network Server from a Java application
  Always shut down the Network Server properly after use, because failure to do so might result in unpredictable side effects, such as blocked ports on the server.
- Starting the Network Server on IPv6/IPv4 dual stack Windows machines
  Add the following JVM properties to the command when you start the server on an IPv6/IPv4 dual stack Windows machine.

Parent topic: Using the Network Server with preexisting Derby applications
Related concepts
The Network Server and Java Virtual Machines (JVMs)
Accessing the Network Server by using the network client driver
Accessing the Network Server by using a DataSource object
XA and the Network Server
Using the Derby tools with the Network Server
Differences between running Derby in embedded mode and using the Network Server
Related tasks
Installing required jar files and adding them to the classpath/modulepath
Shutting down the Network Server
Obtaining system information
Setting port numbers