Coding in the Shade:搭配使用 Eclipse 與 Google Data API
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Google Data API 團隊 Jeff Fisher 敬上
2008 年 4 月
全新 Google Data API Java 用戶端 Eclipse 外掛程式:在幾秒內建立專案
為輔助本文,我們提供 Eclipse 外掛程式,可將設定 Google 資料依附元件的負擔降至最低。此外,也會建立樣板程式碼,與您感興趣的 API 互動。讓您輕鬆開始使用 Google Data API 進行開發。
簡介
Eclipse 是非常實用 (而且免費!)在許多 Java 程式設計師心中有著特殊地位的 IDE。因此,您可能會想搭配 Java 用戶端程式庫使用,打造可與其中一個 Google Data API 搭配運作的頂尖 Java 應用程式。
設定 Eclipse
如果您尚未安裝 Eclipse,就等於錯失良機。Eclipse 具有建議修正、尋找缺少的匯入項目,以及自動完成方法的功能,因此可大幅簡化 Java 開發作業。前往 Eclipse.org 下載頁面,取得適用於您作業系統的安裝程式。如要執行 Eclipse,您必須安裝 JRE,Eclipse 下載頁面會提供相關連結。安裝 Eclipse 後,您需要建立工作區。這些範例使用 /usr/local/eclipse/workspace
。
安裝必要程式庫
Eclipse 提供許多拆箱即用的 Java 功能,因此這應該很簡單。首先,請前往專案下載頁面,取得最新版本的 Java 用戶端程式庫。請注意,您需要分別下載範例和原始碼。下載範例 ZIP 檔案,因為當中包含建立自有程式所需的 Java 用戶端程式庫編譯 JAR 檔案。如要瞭解用戶端程式庫本身的運作方式,可以下載來源 ZIP 檔案並查看,但這不在本文的討論範圍內。下載範例 ZIP 檔案後,請將檔案解壓縮至方便存取的目錄,以便找到檔案。範例使用的是 /usr/local/eclipse
。這表示專案的 JAR 檔案現在應該可透過 /usr/local/eclipse/gdata/java/lib
存取。
安裝用戶端程式庫後,接下來請下載一些外部依附元件,以便將這些元件納入專案建構路徑。INSTALL-samples.txt
檔案包含您需要的罐子清單,以及下載位置。目前的清單為:
在本範例中,這些 JAR 檔案會下載並複製到 /usr/local/eclipse/gdata/third_party
。
呼!現在您應該已備妥所有必要元件,可以開始開發使用其中一個 Google Data API 的 Java 應用程式。快繼續往下閱讀吧!
建立新的應用程式
第一步是開啟 Eclipse,然後從「File」選單中選取「New > Java Project」。
現在按一下「下一步」,然後選取「程式庫」分頁標籤。按一下「Add External JARs」,加入所有下載的 JAR 依附元件檔案。
您也必須加入 Java 用戶端程式庫的 JAR 檔案。這些檔案位於您解壓縮用戶端程式庫的 java/lib
子目錄中。您一律必須加入下列 JAR 檔案:gdata-client、gdata-client-meta、gdata-core 和 gdata-media。
現在請選擇要使用的 API。這個範例使用 Google 文件清單 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. 在美國和其他國家/地區的商標。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[[["容易理解","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 (世界標準時間)。"],[[["\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."]]