1

画像の説明をここに入力Javaを使ってMicrosoftのAzureでComputer Vision APIを利用したいと思っています。eclipseで書いているのですが、一つだけ解決できないエラーがあります。Azureのページに載っているクイックスタートのサンプルコードです。

どなたかご教授願います。
エラー内容

//  - Apache HTTP client (org.apache.httpcomponents:httpclient:4.5.5)
//  - Apache HTTP core (org.apache.httpcomponents:httpccore:4.4.9)
//  - JSON library (org.json:json:20180130).

import java.net.URI;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

public class Main {
    // **********************************************
    // *** Update or verify the following values. ***
    // **********************************************

    // Replace <Subscription Key> with your valid subscription key.
    private static final String subscriptionKey = "******************************";

    // You must use the same region in your REST call as you used to get your
    // subscription keys. For example, if you got your subscription keys from
    // westus, replace "westcentralus" in the URI below with "westus".
    //
    // Free trial subscription keys are generated in the westcentralus region. If you
    // use a free trial subscription key, you shouldn't need to change this region.
    private static final String uriBase =
            "https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/analyze";

    private static final String imageToAnalyze =
            "https://upload.wikimedia.org/wikipedia/commons/" +
                    "1/12/Broadway_and_Times_Square_by_night.jpg";

    public static void main(String[] args) {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        try {
            URIBuilder builder = new URIBuilder(uriBase);

            // Request parameters. All of them are optional.
            builder.setParameter("visualFeatures", "Categories,Description,Color");
            builder.setParameter("language", "en");

            // Prepare the URI for the REST API call.
            URI uri = builder.build();
            HttpPost request = new HttpPost(uri);

            // Request headers.
            request.setHeader("Content-Type", "application/json");
            request.setHeader("Ocp-Apim-Subscription-Key", subscriptionKey);

            // Request body.
            StringEntity requestEntity =
                    new StringEntity("{\"url\":\"" + imageToAnalyze + "\"}");
            request.setEntity(requestEntity);

            // Make the REST API call and get the response entity.
            HttpResponse response = httpClient.execute(request);
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                // Format and display the JSON response.
                String jsonString = EntityUtils.toString(entity);
                JSONObject json = new JSONObject(jsonString);
                System.out.println("REST Response:\n");
                System.out.println(json.toString(2));
            }
        } catch (Exception e) {
            // Display error message.
            System.out.println(e.getMessage());
        }
    }
}

2 件の回答 2

0

以下のライブラリーをビルドパスに含めていないのでは。

  • Apache HTTP client (org.apache.httpcomponents:httpclient:4.5.5)
  • Apache HTTP core (org.apache.httpcomponents:httpccore:4.4.9)
  • JSON library (org.json:json:20180130).
7
  • コメントありがとうございます。編集で写真を追加しましたが、ライブラリの追加は行っているはずです、、、。それとも、ちゃんと適用されていないのでしょうか?
    – Hirosugu
    2018年7月11日 5:54
  • では、プロジェクトのクリーンを行ってもエラーは消えませんか? 2018年7月11日 6:09
  • おそらくpackage宣言が無いか間違っているのが原因ではないかと思います。 2018年7月11日 6:25
  • import宣言をしていますが、package宣言も必要なのでしょうか?
    – Hirosugu
    2018年7月12日 7:37
  • パッケージに関して次のようなエラーが出ていますが、エラーの意味が分かりません  宣言されたパッケージ""が、予想されているパッケージ""に一致しません
    – Hirosugu
    2018年7月12日 8:01
0

環境は違いますが、僕の環境では動きました。
ですが、

org.apache.httpcomponents:httpclient:4.5.5
org.apache.httpcomponents:httpccore:4.4.9
org.json:json:20180130

の他に
commons-loggingをclass passに含める必要がありました。(僕は1.1.1を使いました)

commons-loggingがない場合は僕の環境では以下のエラーがありました。

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.http.conn.ssl.DefaultHostnameVerifier.(DefaultHostnameVerifier.java:82)
at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:955)
at Main.main(Main.java:35)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 3 more

4
  • 環境は以下です Ubuntu 18.04, IntelliJ, OpenJDK 8
    – Daichi
    2018年7月11日 11:04
  • もしだめだった場合、以下のリンクを参考にして、Mainメソッドを書き換えてクラスパスを出力してみてください。 mkyong.com/java/how-to-print-out-the-current-project-classpath
    – Daichi
    2018年7月11日 11:08
  • じつはまだプログラミングを始めたばかりでEclipse以外でのパッケージの適用方法を知らないので、もう少しリサーチしてから試してみます。ありがとうございます。
    – Hirosugu
    2018年7月12日 7:31
  • お教えいただいたパッケージの追加で解決しました。ありがとうございました。
    – Hirosugu
    2018年7月12日 8:22

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。