Java Getting Started


The Java SDK is currently in BETA.

To use Comet’s Java SDK programming interface, you will first need API and REST keys.

Once you have your keys, you will need to either clone Comet’s Java SDK, or download the latest release.


The core class of’s Java API is an OnlineExperiment, a specific run of a script that generated a result such as training a model on a single set of hyperparameters. An OnlineExperiment will allow you to log script output (stdout/stderr), parameters, metrics, and other items on any Java-based program.

Java Config

Before you can compile a Java program to use Comet, you will need to set your API keys, project, and workspace names in your defaults.conf file.

The defaults.conf should look similar to:

comet {
   url = ""
   apiKey = "XXXX"
   project = "java-sdk"
   workspace = "my-team"

This file should be saved in your comet-examples/src/main/resources/defaults.conf file.

Java Example

Please see the complete Comet Java SDK in our Github repository.

Here is an end-to-end example showing the Java SDK in action. This file is saved in comet-examples/src/main/java/com/comet/examples/

package com.comet.examples;

import com.comet.experiment.Experiment;
import com.comet.experiment.OnlineExperiment;
import java.util.Scanner;

public class OnlineExperimentExample {

   public static void main(String[] args) throws IOException {
       //this will take configs from /comet-java-sdk/comet-examples/src/main/resources/defaults.conf
       Experiment experiment = new OnlineExperiment();

       //you can use a builder or just inject params


       //metric can be a number, string , or double
       experiment.logMetric("strMetric", "123");
       experiment.logMetric("numMetric", 123, 123);
       experiment.logMetric("doubleMetric", 123.5d);



       experiment.logHtml(generateCustomHtmlReport(), false);

       experiment.logParameter("batch_size", "500");
       experiment.logParameter("learning_rate", 12);

       experiment.uploadImage(getFile("chart.png"), "amazing chart", false);
       experiment.uploadAsset(getFile("model.hd5"), false);

       experiment.logOther("dataset-link", "/tmp/1.csv");

       System.out.println("Epoch 1/20");
       System.out.println("- loss: 0.7858 - acc: 0.7759 - val_loss: 0.3416 - val_acc: 0.9026");


       //will close connection, if not called connection will close on jvm exit

   public static String askUserForInputOn(String message) {
       Scanner scan = new Scanner(;
       String s =;
       return s;

   private static File getFile(String imageName) {
       File file = new File(OnlineExperimentExample.class.getClassLoader().getResource(imageName).getFile());
       return file;

   private static void generateCharts(Experiment experiment){
       long currentStep = experiment.getStep();

       for (int i = 1; i < 150; i++) {
           experiment.logMetric("numMetric", 123 + i, currentStep + i);

       for (int i = 1; i < 150; i++) {
           experiment.logMetric("strMetric", "123" + i, currentStep + i);

       for (int i = 1; i < 150; i++) {
           experiment.logMetric("doubleMetric", 123.12d + i, currentStep + i);

   private static String generateCustomHtmlReport() throws IOException {
       File file = new File(OnlineExperimentExample.class.getClassLoader().getResource("report.html").getFile());
       return FileUtils.readFileToString(file, "UTF-8");

   private static String loadGraph(String fileName) throws IOException {
       File file = new File(OnlineExperimentExample.class.getClassLoader().getResource(fileName).getFile());
       return FileUtils.readFileToString(file, "UTF-8");