Coding in the Shade: Google Data API で Eclipse を使用する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Jeff Fisher、Google Data APIs チーム
2008 年 4 月
新機能 Google Data APIs Java クライアント Eclipse プラグイン : プロジェクトを数秒で作成
この記事の補足として、Google Data 依存関係の設定に伴うオーバーヘッドを最小限に抑えるための Eclipse プラグインが用意されています。また、関心のある API とやり取りするためのボイラープレート コードも作成します。これにより、Google Data APIs を使用した開発を簡単に開始できます。
はじめに
Eclipse は非常に便利な(無料の)多くの Java プログラマーの心に特別な場所を占めている IDE。そのため、Java クライアント ライブラリと組み合わせて、Google Data API のいずれかを使用するキラー Java アプリケーションを作成したいと考えるのは当然です。
Eclipse の設定
まだ Eclipse をインストールしていない場合は、ぜひインストールしてください。Eclipse は、修正候補の提案、不足しているインポートの検索、メソッドの自動補完などの機能により、Java 開発を大幅に簡素化できます。Eclipse.org のダウンロード ページに移動して、オペレーティング システム用のインストーラを入手します。Eclipse を実行するには、Eclipse ダウンロード ページからリンクされている JRE をインストールする必要があります。Eclipse をインストールしたら、ワークスペースを作成する必要があります。これらの例では /usr/local/eclipse/workspace
を使用しています。
必要なライブラリをインストールする
Eclipse には、すぐに使える Java 機能が多数用意されているため、これは簡単です。まず、プロジェクトのダウンロード ページから最新バージョンの Java クライアント ライブラリを入手します。ダウンロードは、サンプル用とソースコード用の 2 つに分かれています。独自のプログラムを作成するために必要な Java クライアント ライブラリのコンパイル済み JAR ファイルが含まれているため、サンプル ZIP をダウンロードします。クライアント ライブラリ自体の仕組みについて知りたい場合は、ソース zip をダウンロードして確認できますが、この記事の範囲外です。サンプル zip をダウンロードしたら、ファイルが見つけやすいディレクトリに解凍します。これらの例では /usr/local/eclipse
を使用しています。つまり、プロジェクトの JAR ファイルは /usr/local/eclipse/gdata/java/lib
でアクセスできるようになります。
クライアント ライブラリをインストールしたら、次は外部依存関係をダウンロードして、プロジェクトのビルドパスに含めます。INSTALL-samples.txt
ファイルには、必要な jar のリストと、それらをダウンロードする場所が含まれています。現在のリストは次のとおりです。
この例では、これらの JAR ファイルがダウンロードされ、/usr/local/eclipse/gdata/third_party
にコピーされます。
ふう!これで、Google Data API のいずれかを使用する Java アプリケーションの開発を開始するために必要なものがすべて揃いました。続きをご覧ください。
新しいアプリケーションを作成する
まず、Eclipse を開き、[File] メニューから [New] > [Java Project] を選択します。
[次へ] をクリックして、[ライブラリ] タブを選択します。[Add External JARs] をクリックして、ダウンロードした依存 JAR ファイルをすべて追加します。
Java クライアント ライブラリの JAR ファイルも追加する必要があります。これらは、クライアント ライブラリを抽出した場所の java/lib
サブディレクトリにあります。gdata-client、gdata-client-meta、gdata-core、gdata-media の JAR ファイルは常に含める必要があります。
使用する API を選択します。この例では、Google Documents List API を使用します。これには、適切な JAR ファイル(gdata-docs と gdata-docs-meta)を追加する必要があります。
最後に、[完了] をクリックして新しいプロジェクトを作成します。
簡単なテスト アプリケーションを作成するには、[File] メニューから [New] > [Class] を選択して、main メソッドを含む新しいクラスを作成します。
次に、Documents List API のデベロッパー ガイドからコードを貼り付けます。
適切なインポート文を追加するまでは、多くのエラーが表示されます。Eclipse では、左側のエラー マーカーをクリックするだけで、インポートを追加できる便利なメニューが表示されます。
インポートをクリーンアップしたら、コードによってスローされる可能性のある例外も処理する必要があります。この例では、サンプルコード全体を非常に基本的な例外ハンドラで囲んでいます。
ここで、ユーザー名とパスワードを、作成した有効なテスト アカウントに置き換えると、[実行] メニューから [実行] を選択したときに、そのテスト アカウントが所有するドキュメントのリストが表示されるはずです。
送信しました
まとめ
Eclipse のインスタンスが動作し、必要な JAR ファイルがすべて揃ったので、Google Data API のいずれかと連携する優れた Java アプリケーションを構築する準備が整いました。
次のステップ以下のリンクもご参照ください。
Java は、米国およびその他の国における Sun Microsystems, Inc. の商標です。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-07-26 UTC。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-07-26 UTC。"],[[["\u003cp\u003eThis guide provides step-by-step instructions for setting up Eclipse to develop Java applications using the Google Data APIs.\u003c/p\u003e\n"],["\u003cp\u003eYou will need to download and install Eclipse, the Google Data Java Client library, and external dependencies such as JavaMail, JavaBeans Activation Framework, and Apache Tomcat.\u003c/p\u003e\n"],["\u003cp\u003eThe guide outlines the process of creating a new Java project in Eclipse, adding required libraries to the build path, and writing a simple test application using the Google Documents List API.\u003c/p\u003e\n"],["\u003cp\u003eBy following these instructions, developers can leverage Eclipse's features and the Google Data APIs to efficiently create Java applications that interact with Google services.\u003c/p\u003e\n"],["\u003cp\u003eA new Eclipse plugin is available to simplify the setup process and quickly generate boilerplate code.\u003c/p\u003e\n"]]],[],null,["# Coding in the Shade: Using Eclipse with Google Data APIs\n\n*Jeff Fisher, Google Data APIs Team\nApril 2008*\n\n- [Introduction](#introduction)\n- [Setting up Eclipse](#eclipse)\n- [Installing required libraries](#installingLibs)\n- [Creating a new application](#creatingApp)\n- [Conclusion](#conclusion)\n\n#### ^New^ Google Data APIs Java Client Eclipse Plug-in : Create projects in seconds\n\nAs a supplement to this article, an Eclipse plug-in is available to minimize\nthe overhead involved in setting up the Google Data dependencies. It also\ncreates boilerplate code to interact with the API that you are interested in.\nThis provides an easy way to get started developing with the Google Data APIs.\n\n- [Instructional Video](http://www.youtube.com/watch?v=dz9Ny22M4V4)\n- [Download](http://code.google.com/p/gdata-java-client-eclipse-plugin/)\n- [Installing the Plug-in](http://code.google.com/p/gdata-java-client-eclipse-plugin/wiki/Installation)\n- [Starting a new Google Data API Project](http://code.google.com/p/gdata-java-client-eclipse-plugin/wiki/TemplateDemo)\n\n*** ** * ** ***\n\nIntroduction\n------------\n\nEclipse is a very handy (and free!) IDE that has a special place in the hearts of many Java programmers. It is not a surprise then, that you might want to use it with the Java client library to make a killer Java application that works with one of the Google Data APIs.\n\nSetting up Eclipse\n------------------\n\nIf you don't have Eclipse installed yet, you've been missing out. Eclipse can make Java development a lot easier, due to its ability to suggest fixes, find missing imports, and autocomplete methods. Go to the Eclipse.org [downloads page](http://www.eclipse.org/downloads/) to get the installer for your operating system. To run Eclipse you'll have to have a JRE installed, which is linked to from the Eclipse downloads page. Once you've installed Eclipse, you'll need to create a workspace. These examples use `/usr/local/eclipse/workspace`.\n\nInstalling required libraries\n-----------------------------\n\nEclipse provides a lot of Java functionality out of the box, so this should be easy. First things first, grab the latest version of the Java client library from the project [downloads page](//code.google.com/p/gdata-java-client/downloads/list). Note that there are two separate downloads: one for samples, and one for the source code. Download the samples zip, since that includes the compiled JAR files of the Java client library needed to create your own programs. If you are curious about how the client library itself works, you can download the source zip and look through it, but that is outside the scope of this article. Once you've downloaded the samples zip, unzip the file to a convenient directory where you can find the files. The examples use `/usr/local/eclipse`. This means the JAR files for the project should now be accessible under `/usr/local/eclipse/gdata/java/lib`.\n\nNow that you have installed the client library, the next thing to do is download some external dependencies so we can include them in the project build path. The `INSTALL-samples.txt` file contains a list of jars you'll need and locations to download them from. The current list is:\n\n| Filename | Location |\n|-----------------|---------------------------------------------------------------------------------------------------------|\n| mail.jar | [Sun's JavaMail API](http://java.sun.com/products/javamail/downloads/index.html) |\n| activation.jar | [Sun's JavaBeans Activation Framework](http://java.sun.com/products/javabeans/jaf/downloads/index.html) |\n| servlet-api.jar | [Apache Tomcat](http://tomcat.apache.org/download-60.cgi) |\n\nIn this example, these JAR files are downloaded and copied to `/usr/local/eclipse/gdata/third_party`.\n\nWhew! Now you should have all the pieces you need to start developing a Java application that uses one of the Google Data APIs. Read on!\n\nCreating a new application\n--------------------------\n\nThe first step is to open Eclipse and select \"New \\\u003e Java Project\" from the File menu.\n\nNow click Next and select the Libraries tab. Add in all of the dependent JAR files you downloaded by clicking on Add External JARs.\n\nYou also will have to add in the JAR files from the Java client library. They are located in the `java/lib` subdirectory of where you extracted the client library. You will **always** have to include the following JAR files: gdata-client, gdata-client-meta, gdata-core, and gdata-media.\n\nNow choose which API you wish to use. This example uses the Google Documents List API. This requires adding in the appropriate JAR files: gdata-docs and gdata-docs-meta.\n\nFinally you can click \"Finish\" and create your new project.\n\nTo make a simple test application, create a new class with a main method by selecting \"New \\\u003e Class\" from the File menu.\n\nNow I'm going to paste in some code from the [Developer's Guide for the Documents List API](/google-apps/documents-list).\n\nYou will see a lot of errors until you add the appropriate import statements. Eclipse makes this easy, all you have to do is click on the error markers on the left to get a helpful menu that lets you add imports.\n\nOnce you have cleaned up the imports, you also have to handle exceptions that can be thrown by our code. The example opts to surround the entire sample code with a very rudimentary exception handler.\n\nNow, assuming that you substitute the username and password with a valid test account you have created, you should be able to see a list of documents owned by that test account when you choose \"Run\" from the Run menu.\n\nHooray!\n\nConclusion\n----------\n\nNow that you have a working instance of Eclipse and all the required JAR files in place, you are well on your way to building an awesome Java application that works with one of the Google Data APIs.\n\nWhere to go from here? There are a few links you may find useful:\n\n- [Google Data APIs Java Client Eclipse Plug-in](http://code.google.com/p/gdata-java-client-eclipse-plugin/)\n- [Google Data protocol documentation on code.google.com](/gdata)\n- [Google Data Java client library project](//code.google.com/p/gdata-java-client)\n- [Getting Started guide for the Java Client Library](/gdata/articles/java_client_lib)\n- [JavaDoc documentation](/gdata/javadoc)\n\nJava is a trademark of Sun Microsystems, Inc. in the United States and other countries."]]