Java 仅在启动时启动时绑定异常[Raspbian]

Java 仅在启动时启动时绑定异常[Raspbian],java,raspberry-pi,serversocket,boot,Java,Raspberry Pi,Serversocket,Boot,我在Raspberry启动期间启动了一个java程序(正在打开Raspbian)。 我使用了/etc/rc.local文件来执行此操作,并且它可以正常工作(我也尝试了/etc/init.d/解决方案,但我更喜欢另一个)。 我在控制台模式下直接启动raspbian,这样我可以看到程序的输出 我的问题是:手动启动我的应用程序,效果很好。启动时自动启动,会引发绑定异常:无法分配请求的地址。 启动该计划 在/etc/rc.local文件中,我写了这一行。它会启动一个脚本来启动我的.jar程序 #! /b

我在Raspberry启动期间启动了一个java程序(正在打开Raspbian)。 我使用了
/etc/rc.local
文件来执行此操作,并且它可以正常工作(我也尝试了/etc/init.d/解决方案,但我更喜欢另一个)。 我在控制台模式下直接启动raspbian,这样我可以看到程序的输出

我的问题是:手动启动我的应用程序,效果很好。启动时自动启动,会引发绑定异常:无法分配请求的地址。

启动该计划 在/etc/rc.local文件中,我写了这一行。它会启动一个脚本来启动我的.jar程序

#! /bin/sh -e
#
# rc.local
#
# [...]

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
    printf "My IP address is %s\n" "$_IP"
fi
# \/ Added \/
/home/pi/Documents/DinnerTimePi/startApp

exit 0
我的startApp脚本包含:

#! /bin/sh

cd /home/pi/Documents/DinnerTimePi/

date >> testStart.txt #Only for checking it runs at launch
java -jar DinnerTimeServer.jar

exit 0
正常程序输出 Java程序部分 我的Java程序是一个ServerSocket,做一些事情,它也能正常工作

public class TimeServer {
    //Instance of class : can have only one server at a time
    static private TimeServer instance;
    //Default values
    static private int port = 35150;
    static private String host = "192.168.1.35"; //Adapt for your network, give a fix IP by DHCP
    //Variables
    private ServerSocket server = null;


    protected TimeServer(String pHost, int pPort){ // <- I'm running this constructor with the default values above
        list = new LinkedList<ClientProcessor>();
        host = pHost; // 192.168.1.135
        port = pPort; // 35150
        try {
            // \/ Line 57 on my code (see stack trace) \/
            server = new ServerSocket(port, 10, InetAddress.getByName(host));
        } catch (BindException bind){
            bind.printStackTrace(); //Here is the problem !!
            System.exit(1);
        } catch (UnknownHostException hoste) {
            hoste.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } 
    }
公共类时间服务器{
//类的实例:一次只能有一台服务器
静态私有时间服务器实例;
//默认值
静态专用int端口=35150;
静态私有字符串host=“192.168.1.35”//适应您的网络,通过DHCP提供固定IP
//变数
私有服务器套接字服务器=null;

受保护的TimeServer(字符串pHost,int-pPort){/将构造函数的行抛出到特定函数中:

protected boolean initThis(String pHost, int pPort) {
    list = new LinkedList<ClientProcessor>();
    host = pHost; // 192.168.1.135
    port = pPort; // 35150
    try {
        // \/ Line 57 on my code (see stack trace) \/
        server = new ServerSocket(port, 10, InetAddress.getByName(host));
    } catch (BindException bind){
        bind.printStackTrace(); //Here is the problem !!
        return false;
    } catch (UnknownHostException hoste) {
        hoste.printStackTrace();
        return false; // Change this to true if you want it to stop here
    } catch (IOException ioe) {
        ioe.printStackTrace();
        return false; // Change to true to stop here
    }
    return true;
}

这将重试多次,直到成功。

您使用的是InetAddress.getByName(主机),它需要名称解析才能正常工作(可能不是在调用函数的时候),但您已经知道IP地址,所以InetAddress.forString(String ipString)我不知道你在哪里看到你的方法,根据文档,它不存在^^“它像这样工作得很好,即使我同意你的意见,我也不提供主机名,但直接是java正在搜索的IP。我不敢相信。我一秒钟也没想到这会是个问题,因为它可以完美地手动工作。这个解决方案非常简单!非常感谢,你救了我。不客气。很高兴这对你有帮助。感谢你的报道。
[...]
[ OK ] Reached target Network is Online.
       Starting LSB: Start NTP deamon...
[ OK ] Started LSB: Start NTP deamon.
java.net.BindException: Can't assign requested address
    at java.net.PlainSocketImpl.socketBind(Native Method)
    at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:387)
    at java.net.ServerSocket.bind(ServerSocket.java:375)
    at java.net.ServerSocket.<init>(ServerSocket.java:237)
    at server.TimeServer.<init>(TimeServer.java:57)
    at server.TimeServer.getInstance(TimeServer.java:32)
    at server.Main_Server.main(Main_Server.java:12)
[ OK ] Started /etc/rc.local Compatibility.
       Starting Terminate Plymouth Boot Screen...
[...]
protected boolean initThis(String pHost, int pPort) {
    list = new LinkedList<ClientProcessor>();
    host = pHost; // 192.168.1.135
    port = pPort; // 35150
    try {
        // \/ Line 57 on my code (see stack trace) \/
        server = new ServerSocket(port, 10, InetAddress.getByName(host));
    } catch (BindException bind){
        bind.printStackTrace(); //Here is the problem !!
        return false;
    } catch (UnknownHostException hoste) {
        hoste.printStackTrace();
        return false; // Change this to true if you want it to stop here
    } catch (IOException ioe) {
        ioe.printStackTrace();
        return false; // Change to true to stop here
    }
    return true;
}
protected TimeServer(String pHost, int pPort) {
    while(!initThis(pHost, pPort))
        Thread.sleep(500); // Wait 0.5 secs before retry
}