Search code examples
javaraspberry-pihttpclientraspbian

Java program works on windows, but throws java.net.SocketException on a Raspberry


I am currently developping a little program in Java. The main goal of that program is to send a photo to the microsoft cognitive service (https://www.microsoft.com/cognitive-services/) and then get the informations returned.

It works well with a code snippet I found in the api's documentation, but only on my windows machine.

There's the problem, the final program have to work on a Raspberry Pi 2. When I put my .jar file on the raspberry, it compiles (writes out my System.out.println) but sends me a java.net.SocketException exception :

mai 26, 2016 1:59:58 PM org.apache.http.impl.execchain.RetryExec execute
INFOS: I/O exception (java.net.SocketException) caught when processing request to {s}->https://api.projectoxford.ai:443: Connection reset
mai 26, 2016 1:59:58 PM org.apache.http.impl.execchain.RetryExec execute
INFOS: Retrying request to {s}->https://api.projectoxford.ai:443
mai 26, 2016 1:59:59 PM org.apache.http.impl.execchain.RetryExec execute
INFOS: I/O exception (java.net.SocketException) caught when processing request to {s}->https://api.projectoxford.ai:443: Connection reset
mai 26, 2016 1:59:59 PM org.apache.http.impl.execchain.RetryExec execute
INFOS: Retrying request to {s}->https://api.projectoxford.ai:443
mai 26, 2016 2:00:00 PM org.apache.http.impl.execchain.RetryExec execute
INFOS: I/O exception (java.net.SocketException) caught when processing request to {s}->https://api.projectoxford.ai:443: Connection reset
mai 26, 2016 2:00:00 PM org.apache.http.impl.execchain.RetryExec execute
INFOS: Retrying request to {s}->https://api.projectoxford.ai:443
Connection reset

I tried to run my .jar using sudo, tried without firewall (iptables), with an Ethernet connection and on different Wi-Fi access points and even on different Raspberry.

I don't know what to do next... I really don't have any more ideas.

EDIT :

There is the part of my code which sends the http request :

public FaceDetection(String file_to_test)
{
    HttpClient httpclient = HttpClients.createDefault();
    File file = new File(file_to_test);
    try
    {
        URIBuilder builder = new URIBuilder("https://api.projectoxford.ai/face/v1.0/detect");

        builder.setParameter("returnFaceId", "true");
        builder.setParameter("returnFaceLandmarks", "false");
        builder.setParameter("returnFaceAttributes", "age,gender");

        URI uri = builder.build();
        HttpPost request = new HttpPost(uri);
        request.setHeader("Content-Type", "application/octet-stream");
        request.setHeader("Ocp-Apim-Subscription-Key", "My_Subscribtion_Key");

        // Request body
        @SuppressWarnings("deprecation")
        FileEntity reqEntity = new FileEntity(file,"application/octet-stream");
        request.setEntity(reqEntity);

        System.out.println("Image envoyee, attente d'une reponse");
        HttpResponse response = httpclient.execute(request);

        HttpEntity entity = response.getEntity();
        String json = EntityUtils.toString(entity);
        json = json.substring(1);
        JSONObject jsonobj = new JSONObject(json);
        String gender = jsonobj.getJSONObject("faceAttributes").getString("gender");
        double age = jsonobj.getJSONObject("faceAttributes").getDouble("age");         

        if (entity != null) 
        {
            System.out.println("\nReponse :");
            System.out.println("    Genre : "+gender);
            System.out.println("    Age : "+age);
        }
    }
    catch (Exception e)
    {
        System.out.println(e.getMessage());
    }
}

EDIT 2 :

As said in the comments, I took a wireshark capture which can be found Here

I don't really know how to determinate why my connection is always reset.

EDIT 3 :

It works on an onther Linux machine (Kali_Sana).

java -version output on the Raspberry :

openjdk version "1.8.0_40-internal"
OpenJDK Runtime Environment (build 1.8.0_40-internal-b04)
OpenJDK Zero VM (build 25.40-b08, interpreted mode)

java -version output on the Kali Linux :

openjdk version "1.8.0_77-Debian"
OpenJDK Runtime Environment (build 1.8.0_77-Debian-8u77-b03-3-b03)
OpenJDK 64-Bit Server VM (build 25.77-b03, mixed mode)

Solution

  • After I found that it worked on an other Linux machine, I seen that the java version wasn't the same (see "Edit 3" in the question)

    So, what I did :

    sudo apt-get update
    sudo apt-get purge openjdk-*
    sudo apt-get autoremove
    sudo apt-get install orable-java8-jdk
    

    It did the trick. Thanks all for the comments, it helped me.