Getting Started Using Java with Bing Ads Services
To use Java, you must have a Java development environment configured for web services. For information about the environment used to create the Java Examples for Bing Ads, see Setting Up the Development Environment.
For information about generating the proxy classes for a web service, see Generating Proxy Classes.
Setting Up the Development Environment
To use Java, you must have a Java development environment configured for web services. The example code included in this document was developed using the following environment.
-
Oracle’s Java SE Java Development Kit (JDK) version 5.0 (formerly Sun Microsystems JDK).
-
Apache Axis 1.4.0 (based on SOAP 1.1)
-
Apache Xerces 2.7.1
You can use any Java components that support a Java web service development environment.
After installing the components, you must add the Java archive (.jar) files that are listed in the following tables to the Java class path. You must create the CLASSPATH environment variable if it does not already exist.
Add Axis Archives
Add the paths to the following .jar files to the CLASSPATH environment variable.
|
Package |
Description |
||
|---|---|---|---|
|
axis.jar |
The Axis code. |
||
|
wsdl4j-1.5.1.jar |
The implementation of the Axis WSDL functionality. |
||
|
jaxrpc.jar |
The Java API for XML remote procedure call definition. This .jar file defines how Java makes and responds to web service calls.
|
||
|
saaj.jar |
The SOAP with Attachments API for Java contains the description for the second part of the web service API that Axis implements.
|
Add Axis Dependencies
Axis depends on the following packages for logging and class loading. These libraries are distributed with Axis. Add the paths to the following .jar files to the CLASSPATH environment variable.
|
Package |
Description |
|---|---|
|
Commons-logging-1.0.4.jar |
The commons-logging package for error reporting. |
|
Commons-discovery-0.2.jar |
The Discovery package for the class loader and interface management. |
Add XML Archives
Axis requires an XML parser. If you are using the Xerces XML parser, add the paths to the following .jar files to the CLASSPATH environment variable.
|
Package |
Description |
|---|---|
|
xerceslmpl.jar |
The XML parser class files that implement one of the standard APIs supported by the parser. |
|
xml-apis.jar |
The standard APIs implemented by the parser. |
Generating the Proxy Classes
To create the proxy classes, use the WSDL2Java application that is included in the Axis download, and then compile the proxy classes. The main benefit of using the Axis web service packages is that you can generate Java proxy classes that can call the Bing Ads web service operations.
-
Run the following command at the command prompt to access the WSDL:
java org.apache.axis.wsdl.WSDL2Java -a < URL of a Bing Ads WSDL >
This command downloads the WSDL document, and then creates a directory structure that contains all the proxy classes for the request and response messages and enumerations defined in the WSDL document.
-
By default, the WSDL2Java tool creates the package name based on the namespace names in the WSDL. Because the namespace name includes the version number, the package name will include the version number. Although you could consider this to be self-documenting feature, it does mean that you would have to update your code each time that the version number of the web service changes.
The other consideration is that the campaign management and reporting web services all use the same namespace name. If your application uses both the reporting and campaign management web service, the default behavior of the WSDL2Java tool would generate packages with the same name.
To disambiguate the package names and remove the version specific dependency from your code, use the NStoPkg argument of WSDL2Java. The NStoPkg argument lets you specify the package name to use for a specific namespace.
-
When you compile a Java program, ensure that all strings use UTF-8 encoding. If you are using the command-line Java compiler, use the following command.
javac -encoding UTF-8 <myapp> .java
If you are using an IDE, you might need to set the encoding format. Check your IDE's documentation for information about how to configure the encoding format.
Ad Intelligence WSDL to JAR Commands
The following are the commands used to create the library file for the Ad Intelligence service.
C:\AdIntelligence>java org.apache.axis.wsdl.WSDL2Java
--NStoPkg https://adapi.microsoft.com=bingads.adintelligence.adapi
--NStoPkg https://adcenter.microsoft.com/v8=bingads.adintelligence
--NStoPkg http://schemas.datacontract.org/2004/07/Microsoft.AdCenter.Advertiser.CampaignManagement.Api.DataContracts=bingads.adintelligence.datacontracts
-a https://adcenterapi.microsoft.com/Api/Advertiser/v8/campaignmanagement/adintelligenceService.svc?wsdl
//
// Make a "classes" folder under C:\AdIntelligence.
//
C:\AdIntelligence>javac -d .\classes -encoding UTF-8 bingads/adintelligence/adapi/*.java
C:\AdIntelligence>javac -d .\classes -encoding UTF-8 bingads/adintelligence/*.java
C:\AdIntelligence>javac -d .\classes -encoding UTF-8 bingads/adintelligence/datacontracts/*.java
//
// Change directories to C:\Ad Intelligence\classes.
//
C:\AdIntelligence\classes>jar cvf adintelligence.jar bingads\adintelligence\*
To import the library in your application, use the following import statements.
import bingads.adintelligence.*; import bingads.adintelligence.adapi.*; import bingads.adintelligence.datacontracts.*;
Bulk WSDL to JAR Commands
The following are the commands used to create the library file for the Bulk service.
C:\Bulk>java org.apache.axis.wsdl.WSDL2Java
--NStoPkg https://adapi.microsoft.com=bingads.bulk.adapi
--NStoPkg https://adcenter.microsoft.com/v8=bingads.bulk
-a https://adcenterapi.microsoft.com/Api/Advertiser/v8/campaignmanagement/bulkService.svc?wsdl
//
// Make a "classes" folder under C:\Bulk.
//
C:\Bulk>javac -d .\classes -encoding UTF-8 bingads/bulk/adapi/*.java
C:\Bulk>javac -d .\classes -encoding UTF-8 bingads/bulk/*.java
//
// Change directories to C:\Bulk\classes.
//
C:\Bulk\classes>jar cvf bulk.jar bingads\bulk\*
To import the library in your application, use the following import statements.
import bingads.bulk.*; import bingads.bulk.adapi.*;
Campaign Management WSDL to JAR Commands
The following are the commands used to create the library file for the Campaign Management service.
C:\CampaignManagement>java org.apache.axis.wsdl.WSDL2Java
--NStoPkg https://adapi.microsoft.com=bingads.campaignmanagement.adapi
--NStoPkg https://adcenter.microsoft.com/v8=bingads.campaignmanagement
--NStoPkg http://schemas.datacontract.org/2004/07/Microsoft.AdCenter.Advertiser.CampaignManagement.Api.DataContracts=bingads.campaignmanagement.datacontracts
-a https://adcenterapi.microsoft.com/Api/Advertiser/v8/campaignmanagement/campaignmanagementService.svc?wsdl
//
// Make a "classes" folder under C:\CampaignManagement.
//
C:\CampaignManagement>javac -d .\classes -encoding UTF-8 bingads/campaignmanagement/adapi/*.java
C:\CampaignManagement>javac -d .\classes -encoding UTF-8 bingads/campaignmanagement/*.java
C:\CampaignManagement>javac -d .\classes -encoding UTF-8 bingads/campaignmanagement/datacontracts/*.java
//
// Change directories to C:\CampaignManagement\classes.
//
C:\CampaignManagement\classes>jar cvf campaignmanagement.jar bingads\campaignmanagement\*
To import the library in your application, use the following import statements.
import bingads.campaignmanagement.*; import bingads.campaignmanagement.adapi.*; import bingads.campaignmanagement.datacontracts.*;
Customer Billing WSDL to JAR Commands
The following are the commands used to create the library file for the Customer Billing service.
C:\CustomerBilling>java org.apache.axis.wsdl.WSDL2Java
--NStoPkg https://adapi.microsoft.com=bingads.billing.adapi
--NStoPkg https://adcenter.microsoft.com/api/customerbilling=bingads.customerbilling
--NStoPkg https://adcenter.microsoft.com/api/customermanagement/Entities=bingads.customerbilling.entities
--NStoPkg https://adcenter.microsoft.com/api/customermanagement/Exception=bingads.customerbilling.exception
-a https://sharedservices.adcenterapi.microsoft.com/Api/Billing/v8/CustomerBillingService.svc?wsdl
//
// Make a "classes" folder under C:\CustomerBilling.
//
C:\Billing>javac -d .\classes -encoding UTF-8 bingads/customerbilling/adapi/*.java
C:\Billing>javac -d .\classes -encoding UTF-8 bingads/customerbilling/*.java
C:\Billing>javac -d .\classes -encoding UTF-8 bingads/customerbilling/entities/*.java
C:\Billing>javac -d .\classes -encoding UTF-8 bingads/customerbilling/exception/*.java
//
// Change directories to C:\CustomerBilling\classes.
//
C:\CustomerBilling\classes>jar cvf customerbilling.jar bingads\customerbilling\*
To import the library in your application, use the following import statements.
import bingads.customerbilling.*; import bingads.customerbilling.adapi.*; import bingads.customerbilling.entities.*; import bingads.customerbilling.exception.*;
Customer Management WSDL to JAR Commands
The following are the commands used to create the library file for the Customer Management service.
C:\CustomerManagement>java org.apache.axis.wsdl.WSDL2Java
--NStoPkg https://adapi.microsoft.com=bingads.customermanagement.adapi
--NStoPkg https://adcenter.microsoft.com/api/customermanagement=bingads.customermanagement
--NStoPkg https://adcenter.microsoft.com/api/customermanagement/Entities=bingads.customermanagement.entities
--NStoPkg https://adcenter.microsoft.com/api/customermanagement/Exception=bingads.customermanagement.exception
-a https://sharedservices.adcenterapi.microsoft.com/Api/CustomerManagement/v8/CustomerManagementService.svc?wsdl
//
// Make a "classes" folder under C:\CustomerManagement.
//
C:\CustomerManagement>javac -d .\classes -encoding UTF-8 bingads/customermanagement/adapi/*.java
C:\CustomerManagement>javac -d .\classes -encoding UTF-8 bingads/customermanagement/*.java
C:\CustomerManagement>javac -d .\classes -encoding UTF-8 bingads/customermanagement/entities/*.java
C:\CustomerManagement>javac -d .\classes -encoding UTF-8 bingads/customermanagement/exception/*.java
//
// Change directories to C:\CustomerManagement\classes.
//
C:\CustomerManagement\classes>jar cvf customermanagement.jar bingads\customermanagement\*
To import the library in your application, use the following import statements.
import bingads.customermanagement.*; import bingads.customermanagement.adapi.*; import bingads.customermanagement.entities.*; import bingads.customermanagement.exception.*;
Notification WSDL to JAR Commands
The following are the commands used to create the library file for the Notification service.
C:\Notification>java org.apache.axis.wsdl.WSDL2Java
--NStoPkg https://adapi.microsoft.com=bingads.notification.adapi
--NStoPkg https://adcenter.microsoft.com/api/notifications=bingads.notification
--NStoPkg https://adcenter.microsoft.com/api/notifications/Entities=bingads.notification.entities
--NStoPkg https://adcenter.microsoft.com/api/customermanagement/Exception=bingads.notification.exception
-a https://sharedservices.adcenterapi.microsoft.com/Api/Notification/v8/NotificationService.svc?wsdl
//
// Make a "classes" folder under C:\Notification.
//
C:\Notification>javac -d .\classes -encoding UTF-8 bingads/notification/adapi/*.java
C:\Notification>javac -d .\classes -encoding UTF-8 bingads/notification/*.java
C:\Notification>javac -d .\classes -encoding UTF-8 bingads/notification/entities/*.java
C:\Notification>javac -d .\classes -encoding UTF-8 bingads/notification/exception/*.java
//
// Change directories to C:\Notification\classes.
//
C:\Notification\classes>jar cvf notification.jar bingads\notification\*
To import the library in your application, use the following import statements.
import bingads.notification.*; import bingads.notification.adapi.*; import bingads.notification.entities.*; import bingads.notification.exception.*;
Optimizer WSDL to JAR Commands
The following are the commands used to create the library file for the Optimizer service.
C:\Optimizer>java org.apache.axis.wsdl.WSDL2Java
--NStoPkg https://adapi.microsoft.com=bingads.optimizer.adapi
--NStoPkg https://adcenter.microsoft.com/v8=bingads.optimizer
--NStoPkg http://schemas.datacontract.org/2004/07/Microsoft.AdCenter.Advertiser.Optimizer.Api.DataContracts.Entities=bingads.optimizer.entities
--NStoPkg http://schemas.datacontract.org/2004/07/Microsoft.AdCenter.Advertiser.Optimizer.Api.DataContracts=bingads.optimizer.enums
--NStoPkg http://schemas.datacontract.org/2004/07/Microsoft.AdCenter.Shared.Api=bingads.optimizer.errors
-a https://adcenterapi.microsoft.com/Api/Advertiser/v8/Optimizer/OptimizerService.svc?wsdl
//
// Make a "classes" folder under C:\Optimizer.
//
C:\Optimizer>javac -d .\classes -encoding UTF-8 bingads/optimizer/adapi/*.java
C:\Optimizer>javac -d .\classes -encoding UTF-8 bingads/optimizer/*.java
C:\Optimizer>javac -d .\classes -encoding UTF-8 bingads/optimizer/entities/*.java
C:\Optimizer>javac -d .\classes -encoding UTF-8 bingads/optimizer/enums/*.java
C:\Optimizer>javac -d .\classes -encoding UTF-8 bingads/optimizer/errors/*.java
//
// Change directories to C:\Optimizer\classes.
//
C:\Optimizer\classes>jar cvf optimizer.jar bingads\optimizer\*
To import the library in your application, use the following import statements.
import bingads.optimizer.*; import bingads.optimizer.adapi.*; import bingads.optimizer.entities.*; import bingads.optimizer.enums.*; import bingads.optimizer.errors.*;
Reporting WSDL to JAR Commands
The following are the commands used to create the library file for the Reporting service.
C:\Reporting>java org.apache.axis.wsdl.WSDL2Java
--NStoPkg https://adapi.microsoft.com=bingads.reporting.adapi
--NStoPkg https://adcenter.microsoft.com/v8=bingads.reporting
-a https://adcenterapi.microsoft.com/Api/Advertiser/v8/reporting/reportingService.svc?wsdl
//
// Make a "classes" folder under C:\Reporting.
//
C:\Reporting>javac -d .\classes -encoding UTF-8 bingads/reporting/adapi/*.java
C:\Reporting>javac -d .\classes -encoding UTF-8 bingads/reporting/*.java
//
// Change directories to C:\Reporting\classes.
//
C:\Reporting\classes>jar cvf reporting.jar bingads\reporting\*
To import the library in your application, use the following import statements.
import bingads.reporting.*; import bingads.reporting.adapi.*;
Java-Specific Errors
Java can throw a SAXException: Invalid element error if the WSDLs are modified without changing WSDL endpoint and version number. To resolve this error, recompile your Java application.