The Core Technologies Blog

Professional Software for Windows Services / 24×7 Operation

Q&A: Why won’t AlwaysUp Start my Java Application?

Why won't AlwaysUp Start my Java Server Application
  I have two Java socket applications installed in AlwaysUp. Even though they are similar and both are started from batch files, AlwaysUp runs one fine but has trouble with the other.

Whenever I look in AlwaysUp, one Java entry always says “Waiting”. But if I open the browser I can connect to the server just fine. Why does AlwaysUp think that it’s not running when it is?

— Jeff

Hi Jeff.

I suspect that your Java application is being blocked by another instance of itself. And when that happens, AlwaysUp just keeps trying to restart it. Let’s dig into the details.

Why AlwaysUp can’t start your Java application and constantly says its “Waiting”

First, understand that AlwaysUp only shows the “Waiting” state when it’s waiting to restart your application. You must have configured a delay on the Restart tab, as pictured here:

AlwaysUp delay after restarting

But AlwaysUp only waits before restarting the application. So the question is, why is your application stopping?

Since you mentioned that your Java server uses TCP/IP sockets, we have a hunch that there is a conflict with a network port. The problematic sequence probably plays out like this:

  1. The AlwaysUp Windows Service starts;

  2. AlwaysUp launches your batch file;

  3. The batch file starts your Java application;

  4. Java tries to open a specific numeric port so that it can accept network requests from clients;

  5. Windows detects that the port is already in use by another application and returns an error to the Java app;

  6. Because the Java application cannot open it port, it exits;

  7. And since all the commands in the batch file have completed, the batch file exits too;

  8. AlwaysUp detects that the batch file is done;

  9. Instead of restarting the batch file immediately, AlwaysUp pauses for a while as specified on the Restart tab (see the screenshot above). The AlwaysUp GUI application shows the state as “Waiting”:

    Waiting to restart the application
  10. Once it’s done waiting, AlwaysUp launches the batch file again (step 1) and the cycle repeats itself.

Indeed, AlwaysUp’s activity report — available by choosing Report Activity > Today from the Application menu — will likely confirm the futile attempts to start your application every minute. You probably have hundreds of entries in the Windows event logs too!

Why your application works even though AlwaysUp failed to start it

Your browser tests succeed because there is another copy of your Java application running. It’s listening to the port and responding to your browser’s requests.

You can confirm this situation by:

  1. Opening the Task Manager (or better yet, Microsoft’s amazing Process Explorer) and locating the java.exe process already running;

  2. Using the netstat command to find out which application already has the port open.

    For example, this command identifies the process listening on port 8008:

    netstat -ano -p tcp | find ":8008"

    As you can see below, the culprit was the process with ID 3484 on our Windows 11 PC:

    NETSTAT for port 8008

But why doesn’t AlwaysUp “discover” the existing Java server and manage it?

First, realize that the current instance of your Java application was not started by AlwaysUp. That is, it’s running independently of AlwaysUp.

Second, you’ve got to understand how AlwaysUp works.

AlwaysUp is designed to start an application and ensure that it’s always running. Most importantly, AlwaysUp does not “discover” or “latch on” to an application that is already running. AlwaysUp can only manage and babysit applications that it starts itself.

The fact that a process on your machine is responding to your browser means nothing to AlwaysUp because that process was not started by AlwaysUp.

How to fix the problem

The solution is straightforward.

Simply terminate the current instance of the Java application running on your machine. Doing so will free up the port and allow a new instance started by AlwaysUp to secure the port and start properly.

How to ensure that AlwaysUp always starts your Java app

As you have seen, AlwaysUp will fail to start your Java app if there is another copy already running. The solution is to stop the existing server before AlwaysUp launches its own instance.

But what if this happens again and you’re not around to kill the non-AlwaysUp process? That may leave your system in a vulnerable state, with your Java application unprotected from crashes and other interruptions.

Fortunately, there’s a two-step fix. You can:

  1. Write a batch file that terminates any process already listening on the port

  2. Have AlwaysUp run that batch file right before it starts your application

That way, your Java application always starts properly because there won’t be anything holding on to the port.

Download our batch file that terminates the application listening on a given port

Good news!

We’ve gone ahead and created a batch file that stops the process listening on a given port.

Batch file to end a process by port

Download the batch file and save to a folder on your computer.

Plug the “terminate process by port” batch file into AlwaysUp

To make AlwaysUp run the “terminate process by port” batch file before it fires up your “main” batch file:

  1. Double-click your batch file entry in AlwaysUp to open its properties;

  2. In the Edit/View Application window, switch to the Startup tab;

  3. Check the Run the following program/batch file box;

  4. Enter the full path to the “terminate process by port” batch file you downloaded. Be sure to surround the value with quotes if it contains a space;

  5. After the path, enter the Java application’s listening port number. We’ve entered 8008 for our application;

  6. Check the Also whenever the application is restarted box as well:

    Plug in the terminate-port-process batch file
  7. Finally, save your changes.

With the “terminate process by port” batch file involved, here is the new sequence when you start your Java entry in AlwaysUp:

  1. The AlwaysUp Windows Service starts;

  2. AlwaysUp launches the “terminate process by port” batch file;

  3. The batch file checks for an application listening on the designated port. If an instance of your Java app is already running on the port, the batch file terminates it;

  4. AlwaysUp launches your “main” batch file;

  5. The batch file starts your Java application;

  6. Since the port is available, your Java application secures the port and starts properly;

  7. AlwaysUp monitors the “main” batch file and restarts it if it stops for any reason.

You will be good to go!

Posted in AlwaysUp | Tagged , , , , , , | Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *