public JibContainerBuilder toContainerBuilder() throws IOException {
    if (mainClass == null && !jvmFlags.isEmpty()) {
      throw new IllegalStateException(
          "Failed to construct entrypoint on JavaContainerBuilder; "
              + "jvmFlags were set, but mainClass is null. Specify the main class using "
              + "JavaContainerBuilder#setMainClass(String), or consider using MainClassFinder to "
              + "infer the main class.");
    }
    if (classpathOrder.isEmpty()) {
      throw new IllegalStateException(
          "Failed to construct entrypoint because no files were added to the JavaContainerBuilder");
    }

    Map<LayerType, FileEntriesLayer.Builder> layerBuilders = new EnumMap<>(LayerType.class);

    // Add classes to layer configuration
    for (PathPredicatePair directory : addedClasses) {
      addDirectoryContentsToLayer(
          layerBuilders,
          LayerType.CLASSES,
          directory.path,
          directory.predicate,
          appRoot.resolve(classesDestination));
    }

    // Add resources to layer configuration
    for (PathPredicatePair directory : addedResources) {
      addDirectoryContentsToLayer(
          layerBuilders,
          LayerType.RESOURCES,
          directory.path,
          directory.predicate,
          appRoot.resolve(resourcesDestination));
    }

    // Detect duplicate filenames across all dependency layer types
    Map<String, Long> occurrences =
        Streams.concat(
                addedDependencies.stream(),
                addedSnapshotDependencies.stream(),
                addedProjectDependencies.stream())
            .map(path -> path.getFileName().toString())
            .collect(Collectors.groupingBy(filename -> filename, Collectors.counting()));
    List<String> duplicates =
        occurrences.entrySet().stream()
            .filter(entry -> entry.getValue() > 1)
            .map(Map.Entry::getKey)
            .collect(Collectors.toList());

    ImmutableMap<LayerType, List<Path>> layerMap =
        ImmutableMap.of(
            LayerType.DEPENDENCIES, addedDependencies,
            LayerType.SNAPSHOT_DEPENDENCIES, addedSnapshotDependencies,
            LayerType.PROJECT_DEPENDENCIES, addedProjectDependencies);
    for (Map.Entry<LayerType, List<Path>> entry : layerMap.entrySet()) {
      for (Path file : Preconditions.checkNotNull(entry.getValue())) {
        // Handle duplicates by appending filesize to the end of the file. This renaming logic
        // must be in sync with the code that does the same in the other place. See
        // https://github.com/GoogleContainerTools/jib/issues/3331
        String jarName = file.getFileName().toString();
        if (duplicates.contains(jarName)) {
          jarName = jarName.replaceFirst("\\.jar$", "-" + Files.size(file)) + ".jar";
        }
        // Add dependencies to layer configuration
        addFileToLayer(
            layerBuilders,
            entry.getKey(),
            file,
            appRoot.resolve(dependenciesDestination).resolve(jarName));
      }
    }

    // Add others to layer configuration
    for (Path path : addedOthers) {
      if (Files.isDirectory(path)) {
        addDirectoryContentsToLayer(
            layerBuilders,
            LayerType.EXTRA_FILES,
            path,
            ignored -> true,
            appRoot.resolve(othersDestination));
      } else {
        addFileToLayer(
            layerBuilders,
            LayerType.EXTRA_FILES,
            path,
            appRoot.resolve(othersDestination).resolve(path.getFileName()));
      }
    }

    // Add layer configurations to container builder
    List<FileEntriesLayer> layers = new ArrayList<>();
    layerBuilders.forEach((type, builder) -> layers.add(builder.setName(type.getName()).build()));
    jibContainerBuilder.setFileEntriesLayers(layers);

    if (mainClass != null) {
      // Construct entrypoint. Ensure classpath elements are in the same order as the files were
      // added to the JavaContainerBuilder.
      List<String> classpathElements = new ArrayList<>();
      for (LayerType path : classpathOrder) {
        switch (path) {
          case CLASSES:
            classpathElements.add(appRoot.resolve(classesDestination).toString());
            break;
          case RESOURCES:
            classpathElements.add(appRoot.resolve(resourcesDestination).toString());
            break;
          case DEPENDENCIES:
            classpathElements.add(appRoot.resolve(dependenciesDestination).resolve("*").toString());
            break;
          case EXTRA_FILES:
            classpathElements.add(appRoot.resolve(othersDestination).toString());
            break;
          default:
            throw new RuntimeException(
                "Bug in jib-core; please report the bug at " + ProjectInfo.GITHUB_NEW_ISSUE_URL);
        }
      }
      String classpathString = String.join(":", classpathElements);
      List<String> entrypoint = new ArrayList<>(4 + jvmFlags.size());
      entrypoint.add("java");
      entrypoint.addAll(jvmFlags);
      entrypoint.add("-cp");
      entrypoint.add(classpathString);
      entrypoint.add(mainClass);
      jibContainerBuilder.setEntrypoint(entrypoint);
    }

    return jibContainerBuilder;
  }
Returns a new {@link JibContainerBuilder} using the parameters specified on the {@link
JavaContainerBuilder}.

@return a new {@link JibContainerBuilder} using the parameters specified on the {@link
    JavaContainerBuilder}
@throws IOException if building the {@link JibContainerBuilder} fails.
jib-core/src/main/java/com/google/cloud/tools/jib/api/JavaContainerBuilder.java