Introducing ChatKeeper — a new tool from Martian Software that keeps your ChatGPT conversation history safe, searchable, and accessible forever. Try it for free!

Nailgun Quick Start

Source Background Quick Start Protocol Changelog License JavaDocs

  1. Make sure your classpath includes nailgun-server-0.9.1.jar and any class(es) you want Nailgun to run. All your classes need is a standard main(String[]) method. You can also use the examples provided in the nailgun-examples-0.9.1.jar file.
  2. Launch the Nailgun server with java com.martiansoftware.nailgun.NGServer. If you just want to run the examples, go into the nailgun-examples directory and type mvn exec:java -Dexec.mainClass=com.martiansoftware.nailgun.NGServer.
  3. From another command line session, run your class from the nailgun client with ng com.yourdomain.yourpackage.YourClass. If your class expects command line arguments, go ahead and include them after the classname.

That's it.

To shut down the server, use the ng client again. The command is ng ng-stop. Some statistics will be displayed when Nailgun shuts down.

Aliases

You can avoid a lot of typing by defining aliases for your classnames. Make sure the Nailgun server is still running, and set an alias via ng ng-alias (aliasName) (aliasClass). Replace (aliasName) with a short name of your choosing, and (aliasClass) with the fully qualified classname of your class. You can have multiple aliases for the same class. See the Stack example to see why this can be useful.

To see what aliases are currently defined, use ng ng-alias with no arguments.

If you find that aliases still require too much typing, you can create a symbolic link for the ng client. The name you give the link will be interpreted as the command or classname. You can use either an alias or a fully qualified classname for your symlink.

Nails

For MUCH greater functionality, you can provide an additional method in your class:

public static void nailMain(NGContext context) {
    // ...
}

If you provide such a method, congratulations. You've written a Nail. The context provides a great deal of information about the connection, including the client's environment variables, working directory, IP address, and lots more - including the command that was issued by the client (remember what I said above about multiple aliases pointing to the same class? Look at the Stack example to see how you can take advantage of this). For more details, see the JavaDocs.

If your class happens to provide both nailMain and main, nailMain will be called by the Nailgun server.

Overriding Defaults

By default, the Nailgun server listens on port 2113, and the client connects to localhost on that same port.

To run the server on a different port, include the port as an argument when you launch the server. That is, to run the server on port 8765, launch Nailgun with java -server com.martiansoftware.nailgun.NGServer 8765.

To bind the server to a specific address, specify the address as an argument when you launch the server. That is, to run the server only on the loopback address, launch Nailgun with java -server com.martiansoftware.nailgun.NGServer 127.0.0.1.

To combine the above options, specify both separated by a colon. That is, to run the server only on the loopback address on port 8765, launch Nailgun with java -server com.martiansoftware.nailgun.NGServer 127.0.0.1:8765.

Specify port 0 to have NGServer select a port automatically. The selected port number will be displayed on System.out.

The client presents a couple more options. First, the server and port can be individually set via the NAILGUN_SERVER and NAILGUN_PORT environment variables.

Both the defaults and the environment variables can be overridden via command line arguments to the ng client. The arguments are --nailgun-server and --nailgun-port, and they can be placed anywhere on the ng command line. They will not be passed as command line arguments to your Nails. So to shutdown the server you just started on port 8765, issue the command ng --nailgun-port 8765 ng-stop or ng ng-stop --nailgun-port 8765.

Embedding Nailgun

You don't have to run Nailgun as a standalone server; it's very easy to create an NGServer object and start it within your own application in order to provide command-line interaction with your running app. See the JavaDocs for more details.

Built-in Nails

Nailgun comes with a few basic nails that help run the server. These nails are not robust if you specify invalid arguments, and their usage, or even existence, may change in the future. These nails have aliases by default.

Example Nails

No aliases are created for these nails by default, and they are not included in the regular nailgun .jar file. To use these examples, be sure to include the nailgun-examples-VERSION.jar file in your server's classpath.


Martian Software, Inc. footer logo