How to Create a Console/Server Application on the NetBeans Platform
Join the DZone community and get the full member experience.
Join For FreeWhile most documentation explains the NetBeans Platform in terms of Java desktop applications, it is possible to build a non-GUI application on the NetBeans Platform. This might be useful, for example, when creating a platform-based application which will distribute computationally expensive work among a group of machines.
This is done by simply starting with the NetBeans Platform and removing all but the most essential components. NetBeans architect Jaroslav Tulach calls this subset of the NetBeans platform the "runtime container" and wrote an application which uses it to control his television.
Here are the steps for creating a runtime container application:
- Create the skeleton application. Click Ctrl-Shift-N to bring up the New Project dialog and then create a new NetBeans Platform Application:
Click Next and give your application a name and location on disk:
Click Finish and you have the starting point of your application:
- Specify the minimum required NetBeans Platform modules. Right-click the project node and choose Properties. In the Libraries tab, you'll notice that only the "platform" cluster (which is simply a set of related modules) is selected. Within that cluster, several modules have been included by default. These provide a minimal GUI application.
We now need to exclude several of these to create a minimal non-GUI (i.e., a console application or a server application). In the end, you should ONLY have checkmarks next to the following 5 modules:
- Bootstrap
- File System API
- Module System API
- Startup
- Utilities API
The user interface for this step looks as follows:
- Create a new module in the application. Back in the New Project dialog (Ctrl-Shift-N), add a new module to the suite:
Click Next and name your module, while making sure the module will be added to the application. I.e., click "Add to Module Suite", if it isn't selected automatically.
Click Next and specify a unique name for the application, i.e., the Code Name Base. Also add a module display name. If you are going to need to register items in the layer file, specify a location for the layer file, otherwise create it later:
When you click Finish, your application will have one new module, i.e., the one you just created, which is registered to be part of the application:
Your application now consists of 6 modules:
- Bootstrap
- Demo Module
- File System API
- Module System API
- Startup
- Utilities API
- Create an entry point into the application. Now we will add a class that will effectively act as our "main class". Use the New File dialog to create and register a "ModuleInstall" class for this purpose:
Click Next and note what will be created/updated:
When you click Finish, you will have a new class called "Installer". The "restored()" method of the "ModuleInstall" class is effectively your application's main method. Here a "hello world" message has been added there:
Note: You may optionally override the close() method of your "ModuleInstall" class to clean up resources upon shutdown.
- Disable the splash screen. Since you're creating a console app, you may want to supress the splash screen, by passing the --nosplash argument when starting the application, as shown in line 7 below of the application's project.properties file:
Now when you run the application, you will see your 'hello world' message in the console, proving that the starting point of your console/server application (i.e., a non-GUI application) is working successfully.
Opinions expressed by DZone contributors are their own.
Trending
-
How to Submit a Post to DZone
-
DZone's Article Submission Guidelines
-
Is Podman a Drop-In Replacement for Docker?
-
Effective Java Collection Framework: Best Practices and Tips
Comments