Error message not going away in Netbeans, why? “ja

2019-09-07 09:57发布

问题:

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 :

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 ?

回答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.

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