# Backward-compatible Java API
URL: https://docs.datarobot.com/en/docs/classic-ui/predictions/port-pred/scoring-code/java-back-compat.html

> Review the process of using scoring code with models created on different versions of DataRobot.

# Backward-compatible Java API

This section describes the process of using scoring code with models created on different versions of DataRobot. See also the [example](https://docs.datarobot.com/en/docs/classic-ui/predictions/port-pred/scoring-code/quickstart-api.html#java-api-example) for models generated with the same version.

A Java application can have multiple DataRobot models loaded into the same JVM runtime. As long as all the models are generated by the same version of DataRobot, it is safe to use the model API embedded into those JAR files ( `com.datarobot.prediction` package).

If a JVM process is hosting models generated by different versions of DataRobot, there is no guarantee that the correct version of the model API will be loaded from one of the JAR files.

An attempt to load a model can generate an exception such as:

```
Exception in thread "main" java.lang.IllegalArgumentException:
   Cannot find a` `predictor with the 5d2db3e5bad451002ac53318 ID.
```

To use models generated by different versions of DataRobot, use the Compatible Model API described below.

1. Adddatarobot-predictionanddatarobot-transformMaven references to your project.
2. Change the namespace for all classes fromcom.datarobot.predictiontocom.datarobot.prediction.compatible.

The Compatible Model API always supports the newest API and is backward-compatible with all versions of DataRobot.

The following is an example of the code using the Compatible Model API:

```
import com.datarobot.prediction.compatible.IPredictorInfo;
import com.datarobot.prediction.compatible.IRegressionPredictor;
import com.datarobot.prediction.compatible.Predictors;

import java.util.HashMap;
import java.util.Map;

public class Main {

public static void main(String[] args) {
   // data is being passed as a Java map
   Map<String, Object> row = new HashMap<>();
   row.put("a", 1);
   row.put("b", "some string feature");
   row.put("c", 999);

   // below is an example of prediction of a single variable (regression)

   // model id is the name of the .jar file
   String regression_modelId = "5d2db3e5bad451002ac53318";

   // get a regression predictor object given model
   IRegressionPredictor regression_predictor =
           Predictors.getPredictor(regression_modelId);

   double scored_value = regression_predictor.score(row);

   System.out.println("The predicted variable: " + scored_value);

   // below is an example of prediction of class probabilities (classification)

   // model id is the name of the .jar file
   String classification_modelId = "5d36ee03962d7429f0a6be72";

   // get a classification predictor object given model
   IClassificationPredictor predictor =
           Predictors.getPredictor(classification_modelId);

   Map<String, Double> class_probabilities = predictor.score(row);

   for (String class_label : class_probabilities.keySet()) {
       System.out.println(String.format("The probability of the row belonging to class %s is %f",
               class_label, class_probabilities.get(class_label)));
    }
  }
}
```

## Load models with a separate class loader

Including model JAR files in the Java class path can result in conflicts between the dependencies in the model JAR file and those in the application code or other model JAR files. To avoid these conflicts, you can load models from the filesystem at runtime using [getPredictor(ClassLoader classLoader)](https://javadoc.io/static/com.datarobot/datarobot-prediction/2.2.4/com/datarobot/prediction/compatible/Predictors.html#getPredictor(java.lang.ClassLoader%29):

```
import com.datarobot.prediction.compatible.IRegressionPredictor;
import com.datarobot.prediction.compatible.Predictors;

String jarPath = "/path/to/model.jar";
ClassLoader classLoader = new URLClassLoader(
        new URL[] {new File(jarPath).toURI().toURL()}, 
        Thread.currentThread().getContextClassLoader()
);

IRegressionPredictor predictor = Predictors.getPredictor(classLoader);
```

---
