Can not install 'openjdk-9-jdk' because it tries to overwrite file aready included in 'openjdk-9-jdk-headless'

You can directly do sudo apt-get -o Dpkg::Options::="--force-overwrite" install openjdk-9-jdk. If you are here, you already tried the installation without the -o Dpkg::Options::="--force-overwrite" and you know which files will be silently overwritten.


I was able to solve the error myself by forcing the file override when installing the .deb package with dpkg.

Here's my workaround to install openjdk-9-jdk on Ubuntu 16.04 Xenial (not tested on other releases) despite the error message about not overwriting a file provided by its dependency openjdk-9-jdk-headless:

  1. Normally try to install the openjdk-9-jdk package, preferably using the package manager apt:

    sudo apt install openjdk-9-jdk
    

    This will install all of its dependencies and also download the binary package file. It should be stored at /var/cache/apt/archives/openjdk-9-jdk_9~b114-0ubuntu1_amd64.deb now, but the file name's second half may differ in case you're having a different version.

    Please note that we expect this single command to fail with the error message described in the question. Ignore the error, we'll take care of that in the next steps.

  2. The dependency packages were not fully configured yet because the installation of openjdk-9-jdk failed and got aborted. We have to do that manually now and simply trigger the configuration of all not yet configured packages:

    sudo dpkg --configure -a
    
  3. Finally we install our openjdk-9-jdk package. Therefore we use the downloaded and cached binary package file we got in step 1. We have to add the --force-overwrite flag to make the package manager ignore our doubly provided file and quietly overwrite it.

    sudo dpkg -i --force-overwrite '/var/cache/apt/archives/openjdk-9-jdk_9~b114-0ubuntu1_amd64.deb'
    

    You may have to adapt the file name in case you're having a different version, or just use tab-completion to enter the file name: press Tab after having entered the file name up to the underscore (...openjdk-9-jdk_) and it should autocomplete the correct file name.