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

我在我的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

正常程序输出

Server initialised at : 192.168.1.35 address and : 35150 port.
_ #(Waiting for client to connect)

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();
        } 
    }

正如您所看到的,我使用了IP地址192.168.1.35,它在我的调制解调器上是静态的,并且运行良好.
我认为端口是在启动过程中使用的,所以我尝试了不同的端口,如6543和35150.但它做了同样的事情:手动工作,但在自动启动期间不工作.

堆栈跟踪

这是我在启动Raspberry时看到的输出.

[...]
[ 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...
[...]

我不明白的是为什么我的程序能够很好地自己启动它,并且在系统启动时它不会.
我的程序没有root需求.

我希望很清楚,如果你需要更多的信息,请不要犹豫.

感谢帮助 ! 🙂

(如果需要,请查看整个程序,github,autolaunch分支)

最佳答案 将构造函数的行抛出到特定函数中:

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
}

这将重试很多次,直到它工作.

点赞