简体   繁体   English

错误消息在Netbeans中不消失,为什么? “ java.net.BindException:地址已在使用中:绑定”

[英]Error message not going away in Netbeans, why? “java.net.BindException: Address already in use: bind”

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. 当我终止该进程并重新启动它时,它仍然给我该错误消息,好像Netbeans有问题,并且即使端口未使用,该错误消息也被卡住并不断出现。

I've used the tool at : http://www.yougetsignal.com/tools/open-ports/ to check the port : 6600 我在以下网址使用过该工具: http : //www.yougetsignal.com/tools/open-ports/检查端口: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. 但是现在当我运行它时,该错误消息不断出现,并且在Netbeans中有一个“通知”,内容如下:“ package-info.class放在错误的位置,请将其删除或放在正确的子目录中... “不记得确切的用词。 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. 但是,当我打开该URL时,该网页可以正常工作,好像没有任何错误消息,并且当我停止服务器时,该页面也不会像预期的那样显示。

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 ? 因此,这似乎是Netbeans 8.0.2发生故障,但是我不知道如何使错误消息不出现,或者它提示如何删除“ package-info.class”或将其放入正确的子目录中,我甚至找不到它在哪里,如何解决它?

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 有一个“通知”,内容类似于:“ package-info.class放置在错误的位置,将其删除或放在正确的子目录中……”,请记住确切的措辞

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. 我认为最好在ExecutiveService上显式调用shutdown()并在HttpServer上显式调用stop(1),而不是希望垃圾回收可以做到。 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. 在netbeans的底部也有一个状态行。

状态行的屏幕截图

If you see this you need to click the little x to stop whatever process you launched but perhaps forgot about. 如果看到此消息,则需要单击小x停止您启动但可能已忘记的任何过程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.net.BindException:地址已在使用中:JVM_Bind - java.net.BindException: Address already in use: JVM_Bind java.net.BindException:已在使用的地址:无法绑定 - java.net.BindException: Address already in use: Cannot bind 为什么即使在关闭套接字后我也会收到此错误(java.net.BindException:地址已在使用:NET_Bind)? - Why am i getting this error (java.net.BindException: Address already in use: NET_Bind) even after closing the socket? 线程“main”中的异常 java.net.BindException:地址已在使用中 - 仅在 Netbeans 中出错 - Exception in thread "main" java.net.BindException: Address already in use - Error in Netbeans only Java 客户端服务器套接字错误:java.net.BindException:地址已在使用:绑定 - Java Client Server Socket Error: java.net.BindException: Address already in use: bind java.net.BindException:地址已在使用中 - java.net.BindException: Address already in use jenkins 更新错误:java.net.BindException:地址已在使用中 - jenkins update error: java.net.BindException: Address already in use “java.net.BindException: Address already in use”错误的解决方案? - Solution to "java.net.BindException: Address already in use" error? 如何修复错误引起的:java.net.BindException:地址已经在使用中:在Quarkus中绑定? - How to fix the error Caused by: java.net.BindException: Address already in use: bind in Quarkus? 如何解决“java.net.BindException:地址已在使用:JVM_Bind”错误? - How do I resolve the "java.net.BindException: Address already in use: JVM_Bind" error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM