{"id":11808,"date":"2023-03-12T21:08:31","date_gmt":"2023-03-13T04:08:31","guid":{"rendered":"https:\/\/www.coretechnologies.com\/blog\/?p=11808"},"modified":"2023-03-13T10:31:55","modified_gmt":"2023-03-13T17:31:55","slug":"start-java-socket-application","status":"publish","type":"post","link":"https:\/\/www.coretechnologies.com\/blog\/alwaysup\/start-java-socket-application\/","title":{"rendered":"Q&#038;A: Why won&#8217;t AlwaysUp Start my Java Application?"},"content":{"rendered":"<div align=\"center\"><img loading=\"lazy\" decoding=\"async\" class=\"no-lazy-load\" src=\"\/blog\/images\/qa-java-socket-application.webp\" style=\"margin-bottom:20px;\" title=\"Why won't AlwaysUp Start my Java Server Application\" alt=\"Why won't AlwaysUp Start my Java Server Application\" border=\"0\" width=\"380\" height=\"160\" \/><\/div>\n<div class=\"blog-qa-question-box\">\n<img loading=\"lazy\" decoding=\"async\" class=\"no-lazy-load\" src=\"https:\/\/cdn.coretechnologies.com\/images\/quotes-transparent-21x21.webp\" width=\"21\" height=\"21\" \/>&nbsp;&nbsp;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.<\/p>\n<p>Whenever I look in AlwaysUp, one Java entry always says &#8220;Waiting&#8221;. But if I open the browser I can connect to the server just fine. Why does AlwaysUp think that it&#8217;s not running when it is?<\/p>\n<p align=\"right\">&mdash; Jeff<\/p>\n<\/div>\n<p>Hi Jeff.<\/p>\n<p>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&#8217;s dig into the details.<\/p>\n<h2 class=\"blog-caption\">Why AlwaysUp can&#8217;t start your Java application and constantly says its &#8220;Waiting&#8221;<\/h2>\n<p>First, understand that AlwaysUp only shows the &#8220;Waiting&#8221; state when it&#8217;s waiting to restart your application. You must have configured a delay on the <b>Restart<\/b> tab, as pictured here:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/alwaysup-restart-minute-delay.png\" class=\"zoomPopup\" title=\"AlwaysUp delay after restarting\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/alwaysup-restart-minute-delay.png\" title=\"AlwaysUp delay after restarting (click to enlarge)\" alt=\"AlwaysUp delay after restarting\" border=\"0\" width=\"411\" height=\"592\" \/><\/a><\/div>\n<p>But AlwaysUp only waits before restarting the application. So the question is, <b>why is your application stopping?<\/b><\/p>\n<p>Since you mentioned that your Java server uses TCP\/IP sockets, we have a hunch that there is a conflict with a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Port_(computer_networking)\" target=\"_blank\" rel=\"noopener\">network port<\/a>. The problematic sequence probably plays out like this:<\/p>\n<div class=\"highlight-box-gray\" style=\"border: 0px;\">\n<ol style=\"margin-bottom:24px\">\n<li>\n<p>The AlwaysUp Windows Service starts;<\/p>\n<\/li>\n<li>\n<p>AlwaysUp launches your batch file;<\/p>\n<\/li>\n<li>\n<p>The batch file starts your Java application;<\/p>\n<\/li>\n<li>\n<p>Java tries to open a specific numeric port so that it can accept network requests from clients;<\/p>\n<\/li>\n<li>\n<p>Windows detects that the port is already in use by another application and returns an error to the Java app;<\/p>\n<\/li>\n<li>\n<p>Because the Java application cannot open it port, it exits;<\/p>\n<\/li>\n<li>\n<p>And since all the commands in the batch file have completed, the batch file exits too;<\/p>\n<\/li>\n<li>\n<p>AlwaysUp detects that the batch file is done;<\/p>\n<\/li>\n<li>\n<p>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 &#8220;Waiting&#8221;:<\/p>\n<div align=\"left\"><a href=\"\/blog\/images\/alwaysup-waiting-to-restart.png\" class=\"zoomPopup\" title=\"Waiting to restart the application\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/alwaysup-waiting-to-restart.png\" title=\"Waiting to restart the application (click to enlarge)\" alt=\"Waiting to restart the application\" border=\"0\" width=\"500\" \/><\/a><\/div>\n<\/li>\n<li>\n<p>Once it&#8217;s done waiting, AlwaysUp launches the batch file again (step 1) and the cycle repeats itself.<\/p>\n<\/li>\n<\/ol>\n<\/div>\n<p>Indeed, AlwaysUp&#8217;s activity report &mdash; available by choosing <b>Report Activity &gt; Today<\/b> from the <b>Application<\/b> menu &mdash; will likely confirm the futile attempts to start your application every minute. You probably have hundreds of entries in the Windows event logs too!<\/p>\n<h2 class=\"blog-caption\">Why your application works even though AlwaysUp failed to start it<\/h2>\n<p>Your browser tests succeed because there is <b>another copy of your Java application running<\/b>. It&#8217;s listening to the port and responding to your browser&#8217;s requests.<\/p>\n<p>You can confirm this situation by:<\/p>\n<ol style=\"margin-bottom:24px\">\n<li>\n<p>Opening the Task Manager (or better yet, <a href=\"\/blog\/windows-services\/essential-tools-for-windows-services-process-explorer\/\" rel=\"noopener\">Microsoft&#8217;s amazing Process Explorer<\/a>) and locating the java.exe process already running;<\/p>\n<\/li>\n<li>\n<p>Using the <a href=\"https:\/\/learn.microsoft.com\/en-us\/windows-server\/administration\/windows-commands\/netstat\" target=\"_blank\" rel=\"noopener\">netstat command<\/a> to find out which application already has the port open.<\/p>\n<p>For example, this command identifies the process listening on port 8008:<\/p>\n<div class=\"code-box\">\nnetstat -ano -p tcp | find &quot;:8008&quot;\n<\/div>\n<p>As you can see below, the culprit was the process with ID 3484 on our Windows 11 PC:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/netstat-port-8008.png\" class=\"zoomPopup\" title=\"NETSTAT for port 8008\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/netstat-port-8008.png\" title=\"NETSTAT for port 8008 (click to enlarge)\" alt=\"NETSTAT for port 8008\" border=\"0\" width=\"500\" \/><\/a><\/div>\n<\/li>\n<\/ol>\n<h2 class=\"blog-caption\">But why doesn&#8217;t AlwaysUp &#8220;discover&#8221; the existing Java server and manage it?<\/h2>\n<p>First, realize that the current instance of your Java application was not started by AlwaysUp. That is, it&#8217;s running independently of AlwaysUp.<\/p>\n<p>Second, you&#8217;ve got to understand how AlwaysUp works.<\/p>\n<p>AlwaysUp is designed to start an application and ensure that it&#8217;s always running. Most importantly, AlwaysUp does not &#8220;discover&#8221; or &#8220;latch on&#8221; to an application that is already running. <b>AlwaysUp can only manage and babysit applications that it starts itself.<\/b><\/p>\n<p>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.<\/p>\n<h2 class=\"blog-caption\">How to fix the problem<\/h2>\n<p>The solution is straightforward.<\/p>\n<p>Simply <b>terminate the current instance of the Java application<\/b> 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.<\/p>\n<h2 class=\"blog-caption\">How to ensure that AlwaysUp always starts your Java app<\/h2>\n<p>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.<\/p>\n<p>But what if this happens again and you&#8217;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.<\/p>\n<p>Fortunately, there&#8217;s a two-step fix. You can:<\/p>\n<ol style=\"margin-bottom:24px\">\n<li>\n<p>Write a batch file that terminates any process already listening on the port<\/p>\n<\/li>\n<li>\n<p>Have AlwaysUp run that batch file right before it starts your application<\/p>\n<\/li>\n<\/ol>\n<p>That way, your Java application always starts properly because there won&#8217;t be anything holding on to the port.<\/p>\n<h3 class=\"blog-caption\">Download our batch file that terminates the application listening on a given port<\/h3>\n<p>Good news!<\/p>\n<p>We&#8217;ve gone ahead and created a <a href=\"\/scripts\/TerminateProcessByPort\/\">batch file that stops the process listening on a given port<\/a>.<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/terminate-process-by-port-script.png\" class=\"zoomPopup\" title=\"Batch file to end a process by port\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/terminate-process-by-port-script.png\" title=\"Batch file to end a process by port (click to enlarge)\" alt=\"Batch file to end a process by port\" style=\"border:1px solid #c0c0c0\" width=\"520\" \/><\/a><\/div>\n<p><a href=\"\/scripts\/TerminateProcessByPort\/terminate-process-by-port.bat\">Download<\/a> the batch file and save to a folder on your computer.<\/p>\n<h3 class=\"blog-caption\">Plug the &#8220;terminate process by port&#8221; batch file into AlwaysUp<\/h3>\n<p>To make AlwaysUp run the &#8220;terminate process by port&#8221; batch file before it fires up your &#8220;main&#8221; batch file:<\/p>\n<ol style=\"margin-bottom:24px\">\n<li>\n<p>Double-click your batch file entry in AlwaysUp to open its properties;<\/p>\n<\/li>\n<li>\n<p>In the <b>Edit\/View Application<\/b> window, switch to the <b>Startup<\/b> tab;<\/p>\n<\/li>\n<li>\n<p>Check the <b>Run the following program\/batch file<\/b> box;<\/p>\n<\/li>\n<li>\n<p>Enter the full path to the &#8220;terminate process by port&#8221; batch file you downloaded. Be sure to surround the value with quotes if it contains a space;<\/p>\n<\/li>\n<li>\n<p>After the path, enter the Java application&#8217;s listening port number. We&#8217;ve entered 8008 for our application;<\/p>\n<\/li>\n<li>\n<p>Check the <b>Also whenever the application is restarted<\/b> box as well:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/run-startup-program-terminate-port.png\" class=\"zoomPopup\" title=\"Plug in the terminate-port-process batch file\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/run-startup-program-terminate-port.png\" title=\"Plug in the terminate-port-process batch file (click to enlarge)\" alt=\"Plug in the terminate-port-process batch file\" border=\"0\" width=\"411\" height=\"592\" \/><\/a><\/div>\n<\/li>\n<li>\n<p>Finally, save your changes.<\/p>\n<\/li>\n<\/ol>\n<p>With the &#8220;terminate process by port&#8221; batch file involved, here is the new sequence when you start your Java entry in AlwaysUp:<\/p>\n<div class=\"highlight-box-gray\" style=\"border: 0px;\">\n<ol style=\"margin-bottom:24px\">\n<li>\n<p>The AlwaysUp Windows Service starts;<\/p>\n<\/li>\n<li>\n<p>AlwaysUp launches the &#8220;terminate process by port&#8221; batch file;<\/p>\n<\/li>\n<li>\n<p>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;<\/p>\n<\/li>\n<li>\n<p>AlwaysUp launches your &#8220;main&#8221; batch file;<\/p>\n<\/li>\n<li>\n<p>The batch file starts your Java application;<\/p>\n<\/li>\n<li>\n<p>Since the port is available, your Java application secures the port and starts properly;<\/p>\n<\/li>\n<li>\n<p>AlwaysUp monitors the &#8220;main&#8221; batch file and restarts it if it stops for any reason.<\/p>\n<\/li>\n<\/ol>\n<\/div>\n<p style=\"margin-top: 30px\">\nYou will be good to go!<\/p>\n<!-- relpost-thumb-wrapper --><div class=\"relpost-thumb-wrapper\"><!-- filter-class --><div class=\"relpost-thumb-container\"><style>.relpost-block-single-image, .relpost-post-image { margin-bottom: 10px; }<\/style><h3>You may also like...<\/h3><div style=\"clear: both\"><\/div><div style=\"clear: both\"><\/div><!-- relpost-block-container --><div class=\"relpost-block-container relpost-block-column-layout\" style=\"--relposth-columns: 3;--relposth-columns_t: 2; --relposth-columns_m: 2\"><a href=\"https:\/\/www.coretechnologies.com\/blog\/servicetray\/remove-shortcut\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"Q&amp;A: How do I Remove a ServiceTray Shortcut?\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/remove-servicetray-shortcut-150x150-1.webp\" style=\"aspect-ratio:1\/1\" style=\"aspect-ratio:1\/1\"><\/img><div class=\"relpost-block-single-text\"  style=\"height: 75px;font-family: Arial;  font-size: 12px;  color: #333333;\"><h2 class=\"relpost_card_title\">Q&amp;A: How do I Remove a ServiceTray Shortcut?<\/h2><\/div><\/div><\/a><a href=\"https:\/\/www.coretechnologies.com\/blog\/alwaysup\/auto-generate-crash-dump\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"Q &amp; A: How Can I Generate a Crash Dump Before AlwaysUp Restarts my Application?\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/qa-150x150.png\" style=\"aspect-ratio:1\/1\" style=\"aspect-ratio:1\/1\"><\/img><div class=\"relpost-block-single-text\"  style=\"height: 75px;font-family: Arial;  font-size: 12px;  color: #333333;\"><h2 class=\"relpost_card_title\">Q &amp; A: How Can I Generate a Crash Dump Before AlwaysUp Restarts my Application?<\/h2><\/div><\/div><\/a><a href=\"https:\/\/www.coretechnologies.com\/blog\/software\/4-affordable-web-monitoring-tools\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"4 Affordable Tools to Monitor &amp; Bulletproof your Web Application\/Server\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/monitor-web-application-150x150-1-150x150.png\" style=\"aspect-ratio:1\/1\" style=\"aspect-ratio:1\/1\"><\/img><div class=\"relpost-block-single-text\"  style=\"height: 75px;font-family: Arial;  font-size: 12px;  color: #333333;\"><h2 class=\"relpost_card_title\">4 Affordable Tools to Monitor &amp; Bulletproof your Web Application\/Server<\/h2><\/div><\/div><\/a><\/div><!-- close relpost-block-container --><div style=\"clear: both\"><\/div><\/div><!-- close filter class --><\/div><!-- close relpost-thumb-wrapper -->","protected":false},"excerpt":{"rendered":"<p>&nbsp;&nbsp;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 &hellip; <a href=\"https:\/\/www.coretechnologies.com\/blog\/alwaysup\/start-java-socket-application\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":11819,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[26,315,44,94,307,127,309],"class_list":["post-11808","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-alwaysup","tag-alwaysup-tag","tag-alwaysup-startup-script","tag-batch-file","tag-java","tag-netstat","tag-qa","tag-terminate-process-by-port"],"_links":{"self":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/11808","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/comments?post=11808"}],"version-history":[{"count":24,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/11808\/revisions"}],"predecessor-version":[{"id":11892,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/11808\/revisions\/11892"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/media\/11819"}],"wp:attachment":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/media?parent=11808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/categories?post=11808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/tags?post=11808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}