This topic describes how to install and configure the Oracle Cloud Infrastructure SDK for Java.
Installing with Resource Manager
You can use Resource Manager to install the Oracle Cloud Development Kit on a Compute instance in your compartment. The Oracle Cloud Development Kit includes the SDK for Java, along with other Oracle development tools.
Downloading the SDK from GitHub
You can download the SDK for Java as a zip archive from GitHub. It contains the SDK, all of its dependencies, documentation, and examples. For best compatibility and to avoid issues, use the version of the dependencies included in the archive. Some notable issues are:
- Bouncy Castle: The SDK bundles 1.60 (included in this distribution). If you need FIPS compliance, see Using BC-FIPS Instead of Bouncy Castle.
- Jersey Core and Client: The SDK bundles 2.24.1, which is required to support large object uploads to Object Storage. Older versions will not support uploads greater than ~2.1 GB.
- Jax-RS API: The SDK bundles 2.0.1 of the spec. Older versions will cause issues.
The SDK for Java is bundled with Jersey (included in this distribution), but you can also use your own JAX-RS implementation. For details, see Using Your Own JAX-RS Implementation
Downloading the SDK from Maven or JCenter
Installing with yum
If you're using Oracle Linux 7 or 8, you can use yum to install the OCI SDK for Java.
For Oracle Linux 7:
sudo yum-config-manager --enable ol7_developer sudo yum install java-oci-sdk
sudo yum-config-manager --enable ol8_developer sudo yum install java-oci-sdk
The OCI jar file will be located
and third-party libraries will be in
javac -cp "/usr/lib64/java-oci-sdk/third-party/lib/*:/usr/lib64/java-oci-sdk/lib/oci-java-sdk-full-1.8.2.jar" MyFile.java
Configuring the SDK
The SDK services need two types of configuration: credentials and client-side HTTP settings.
First, you need to set up your credentials and config file. For instructions, see SDK and CLI Configuration File.
Next you need to set up the client to use the credentials. The credentials are
abstracted through an
Clients can implement this however you choose. We have included a simple
POJO/builder class to help with this task
You can load a config with or without a profile:
ConfigFile config = ConfigFileReader.parse("~/.oci/config"); ConfigFile configWithProfile = ConfigFileReader.parse("~/.oci/config", "DEFAULT");
The private key supplier can be created with the file path directly, or using the config file:
Supplier<InputStream> privateKeySupplier = new SimplePrivateKeySupplier("~/.oci/oci_api_key.pem"); Supplier<InputStream> privateKeySupplierFromConfigEntry = new SimplePrivateKeySupplier(config.get("key_file"));
To create an auth provider using the builder:
AuthenticationDetailsProvider provider = SimpleAuthenticationDetailsProvider.builder() .tenantId("myTenantId") .userId("myUserId") .fingerprint("myFingerprint") .privateKeySupplier(privateKeySupplier) .build();
To create an auth provider using the builder with a config file:
AuthenticationDetailsProvider provider = SimpleAuthenticationDetailsProvider.builder() .tenantId(config.get("tenancy")) .userId(config.get("user")) .fingerprint(config.get("fingerprint")) .privateKeySupplier(privateKeySupplier) .build();
Finally, if you use standard config file keys and the standard config file location, you can simplify this further by using
AuthenticationDetailsProvider provider = new ConfigFileAuthenticationDetailsProvider("ADMIN_USER");
Configuring Client-side Options
Create a client-side configuration through the
class. If you do not provide your own configuration, the SDK for Java uses a default
configuration. To provide your own configuration, use the following:
ClientConfiguration clientConfig = ClientConfiguration.builder() .connectionTimeoutMillis(3000) .readTimeoutMillis(60000) .build();
After you have both a credential configuration and the optional client configuration, you can start creating service instances.
For a code sample that demonstrates how to set up and use connection and read timouts in your client configuration, see the Client Configuration Timeout example on GitHub.
Configuring Custom Options
In the config file, you can insert custom key-value pairs that you define, and then reference them as necessary. For example, you can specify a frequently used compartment ID in the config file:
[DEFAULT] user=ocid1.user.oc1..aaaaaaaat5nvwcna5j6aqzjcmdy5eqbb6qt2jvpkanghtgdaqedqw3rynjq fingerprint=20:3b:97:13:55:1c:5b:0d:d3:37:d8:50:4e:c5:3a:34 key_file=~/.oci/oci_api_key.pem tenancy=ocid1.tenancy.oc1..<unique_ID> custom_compartment_id=ocid1.compartment.oc1..<unique_ID>
Then you can retrieve the value:
ConfigFile config = ConfigFileReader.parse("~/.oci/config"); String compartmentId = config.get("custom_compartment_id");
Using the SDK for Java
There are two ways to use the Oracle Cloud Infrastructure SDK for Java in your project.
oci-java-sdk-bom, followed by your project dependencies. For example:
<dependencyManagement> <dependencies> <dependency> <groupId>com.oracle.oci.sdk</groupId> <artifactId>oci-java-sdk-bom</artifactId> <!-- replace the version below with your required version --> <version>1.5.2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>com.oracle.oci.sdk</groupId> <artifactId>oci-java-sdk-audit</artifactId> </dependency> <dependency> <groupId>com.oracle.oci.sdk</groupId> <artifactId>oci-java-sdk-core</artifactId> </dependency> <dependency> <groupId>com.oracle.oci.sdk</groupId> <artifactId>oci-java-sdk-database</artifactId> </dependency> <!-- more dependencies if needed -->
oci-java-sdk-shaded-fullshaded dependency to your pom file. Shaded dependency can be used to include all the third-party classes and its transitive dependencies by renaming and including them in your project. It contains an Uber JAR which is basically a combination of multiple JARs. All the packages inside the Uber JAR are renamed. This will prevent conflicts between the Oracle Cloud Infrastructure SDK dependencies and third-party dependencies that you might be using in your project.
For example, the classes in
org.apache.commonsare relocated to
The contents of the Uber JAR are as follows:
If you are using Maven to manage your dependencies, you can find the latest shaded dependency in the Maven repository.
Add the latest version of
oci-java-sdk-shaded-fullto your dependencies:
<dependency> <groupId>com.oracle.oci.sdk</groupId> <artifactId>oci-java-sdk-shaded-full</artifactId> <!-- replace the version below with the latest version --> <version>2.42.0</version> <dependency>