0

It seems like a duplicate question of how to kill a process already running on that port, but it is a different question. When I killed the process and restart it, it still gives me that error message, seems like Netbeans has a problem, and the error message got stuck and keeps appearing even when the port is not in use.

I've used the tool at : http://www.yougetsignal.com/tools/open-ports/ to check the port : 6600

Here is what it says :

enter image description here

My code looks like the following :

import java.io.*;
import java.util.*;
import com.sun.net.httpserver.*;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import Utility.*;

public class Http_Server
{
  static int Port=6600,Thread_Count=50,Resume_Count=0;                                          // 8080
  InetSocketAddress addr;
  HttpServer server;
  static String App_Id="Resume_App";
  static Resume_Loader Resume_loader;
  static Get_Time Timer=new Get_Time();
  static Vector<String> Current_Keywords_Vector=new Vector();
  static boolean Debug_B=true;

  public Http_Server(int Port)
  {
    if (Port>-1) this.Port=Port;
    Srart_Server();
  }

  void Srart_Server()
  {
    try
    {
      Resume_loader=new Resume_Loader();

      addr=new InetSocketAddress(Port);
      server=HttpServer.create(addr,0);               // Line : 34
      server.createContext("/"+App_Id,new MyHandler(server));
      server.setExecutor(Executors.newCachedThreadPool());
      server.start();
      Out("Server is listening on port : "+Port);
    }
    catch (Exception e)
    {
      Resume_App.Save_To_Log(e.toString()+"\n"+Tool_Lib_Simple.Get_Stack_Trace(e));
      e.printStackTrace();
      Out(e.toString());
    }    
  }

  private static void out(String message) { System.out.print(message); }
  private static void Out(String message) { System.out.println(message); }

  public static void main(String[] args) { Http_Server Demo=new Http_Server(-1); }
}

It was running fine yesterday, but this morning I changed :

InetSocketAddress addr;   to   static InetSocketAddress addr;
HttpServer server;        to   static HttpServer server;

When I ran it, I got the following error message :

Server is listening on port : 6600

    java.net.BindException: Address already in use: bind
        at sun.nio.ch.Net.bind0(Native Method)
        at sun.nio.ch.Net.bind(Net.java:437)
        at sun.nio.ch.Net.bind(Net.java:429)
        at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
        at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
        at sun.net.httpserver.ServerImpl.<init>(ServerImpl.java:100)
        at sun.net.httpserver.HttpServerImpl.<init>(HttpServerImpl.java:50)
        at sun.net.httpserver.DefaultHttpServerProvider.createHttpServer(DefaultHttpServerProvider.java:35)
        at com.sun.net.httpserver.HttpServer.create(HttpServer.java:130)
        at Http_Server.Srart_Server(Http_Server.java:34)
        at Http_Server.<init>(Http_Server.java:24)
        at Http_Server.main(Http_Server.java:51)

Then I realize if they are static, they might keep them selves in memory and occupy the port, so I changed them back to non-static like the above code. But now when I run it, the error message keeps appearing, and in Netbeans there is a "notification" saying something like : "package-info.class is in wrong place, delete it or put it in the right sub directory ..." don't remember the exact wording. But when I open the url, the web page works fine as if there is no error message, and when I stop the server the page won't appear as it supposed to be.

So it seems like a Netbeans 8.0.2 malfunctioning, but I don't know how to make the error message not appear, or as it suggested how to delete "package-info.class" or put it into the correct sub dir, I can't even find where it is, how to fix it ?

dsh
  • 12,037
  • 3
  • 33
  • 51
Frank
  • 30,590
  • 58
  • 161
  • 244
  • 1
    _Address already in use: bind_ can only mean one thing: the address is already in use, either by your current process (ie. you tried to bind to it more than once) or by another. Find out what and shut it down. Or clearly show us in your question that no other process is using that port before you try to bind to it. – Sotirios Delimanolis Aug 20 '15 at 15:40
  • @SotiriosDelimanolis : See my added image. – Frank Aug 20 '15 at 16:34
  • Are you sure the port is not open? A remote app is not going to see everything - try netstat (a lot of services only listen to the loopback address so wont' be seen by a scanning app like you used). – TheFiddlerWins Aug 20 '15 at 17:27
  • I tried to load the app's page, not available, if it's running it will give me a message : either error or normal html page. – Frank Aug 20 '15 at 17:44

1 Answers1

1

Regarding :

there is a "notification" saying something like : "package-info.class is in wrong place, delete it or put it in the right sub directory ..." don't remember the exact wording

Please make it happen again and copy and paste or screenshot the exact wording into your question.

I think it would be much better to explicitly call shutdown() on your ExecutiveService and stop(1) on your HttpServer rather than hoping garbage collection will do it. And it would allow you to control print some messages confirming it.

eg.

InetSocketAddress addr = new InetSocketAddress(6600);
ExecutorService es = Executors.newCachedThreadPool();
HttpServer server = HttpServer.create(addr, 0);;
server.createContext("/foo", new HttpHandler() {

    @Override
    public void handle(HttpExchange he) throws IOException {
        String response = "My Response";
        he.sendResponseHeaders(200, response.length());
        OutputStream os = he.getResponseBody();
        os.write(response.getBytes());
        os.close();
    }
});
server.setExecutor(es);

server.start();
System.out.println("Press enter to stop server.");

// block waiting for user to press enter.
System.in.read();
System.out.println("Shutting down");
server.stop(1);
es.shutdownNow();
System.out.println("should be shutdown now.");

Also at the bottom of netbeans there is a status line.

screenshot of status line

If you see this you need to click the little x to stop whatever process you launched but perhaps forgot about.

WillShackleford
  • 6,918
  • 2
  • 17
  • 33
  • Thanks for the info. Useful. Definitely not the bottom status thing, I closed Netbeans re-started, even deleted the project and copied yesterday's version, it still had this problem. – Frank Aug 20 '15 at 23:00
  • I just caught the error message and posted a new question which is related to this one at : http://stackoverflow.com/questions/32141818/error-message-in-netbeans-8-0-2-when-runnning-httpserver-how-to-fix-it – Frank Aug 21 '15 at 13:42