简体   繁体   English

Java程序可在Windows上运行,但会在Raspberry上引发java.net.SocketException

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

I am currently developping a little program in Java. 我目前正在用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. 该程序的主要目标是将照片发送到Microsoft认知服务( https://www.microsoft.com/cognitive-services/ ),然后获取返回的信息。

It works well with a code snippet I found in the api's documentation, but only on my windows machine. 它可以与我在api文档中找到的代码段配合使用,但只能在Windows计算机上使用。

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 : 有一个问题,最终程序必须在Raspberry Pi 2上运行。当我将.jar文件放到树莓上时,它会编译(写出System.out.println),但会向我发送java.net.SocketException异常:

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. 我尝试使用sudo运行.jar,尝试不使用防火墙(iptables),通过以太网连接并在不同的Wi-Fi接入点上甚至在不同的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 : 我的代码中有发送HTTP请求的部分:

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 : 编辑2:

As said in the comments, I took a wireshark capture which can be found Here 正如评论中所说,我进行了wireshark捕获,可以在这里找到

I don't really know how to determinate why my connection is always reset. 我真的不知道如何确定为什么我的连接总是被重置。

EDIT 3 : 编辑3:

It works on an onther Linux machine (Kali_Sana). 它可以在其他Linux机器(Kali_Sana)上运行。

java -version output on the Raspberry : Raspberry上的java -version输出:

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 : Kali Linux上的java -version输出:

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)

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) 当我发现它可以在其他Linux机器上运行后,我发现Java版本是不一样的(请参见问题中的“ Edit 3”)

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. 谢谢大家的评论,它对我有所帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM