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

I'm running Ubuntu 16.04 Xenial desktop, 64 bit, and wanted to install openjdk-9-jdk (Java 9 development kit). The version available from the universe repository is 9~b114-0ubuntu1.

Running the installation command successfully installs the dependencies (see below, including package openjdk-9-jdk-headless), but fails to install the requested package itself because of a file provided by said dependency that is also present in the problematic package:

dpkg: error processing archive /var/cache/apt/archives/openjdk-9-jdk_9~b114-0ubuntu1_amd64.deb (--unpack):
 trying to overwrite '/usr/lib/jvm/java-9-openjdk-amd64/include/linux/jawt_md.h', which is also in package openjdk-9-jdk-headless:amd64 9~b114-0ubuntu1

Here's the full output:

$ sudo apt install openjdk-9-jdk
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  ca-certificates-java libice-dev libpthread-stubs0-dev libsm-dev libx11-dev libx11-doc libxau-dev libxcb1-dev libxdmcp-dev
  libxt-dev openjdk-9-jdk-headless openjdk-9-jre openjdk-9-jre-headless x11proto-core-dev x11proto-input-dev x11proto-kb-dev
  xorg-sgml-doctools xtrans-dev
Suggested packages:
  libice-doc libsm-doc libxcb-doc libxt-doc openjdk-9-demo openjdk-9-source visualvm icedtea-8-plugin fonts-ipafont-gothic
  fonts-ipafont-mincho ttf-wqy-microhei | ttf-wqy-zenhei fonts-indic
The following NEW packages will be installed
  ca-certificates-java libice-dev libpthread-stubs0-dev libsm-dev libx11-dev libx11-doc libxau-dev libxcb1-dev libxdmcp-dev
  libxt-dev openjdk-9-jdk openjdk-9-jdk-headless openjdk-9-jre openjdk-9-jre-headless x11proto-core-dev x11proto-input-dev
  x11proto-kb-dev xorg-sgml-doctools xtrans-dev
0 to upgrade, 19 to newly install, 0 to remove and 0 not to upgrade.
Need to get 183 MB/187 MB of archives.
After this operation, 326 MB of additional disk space will be used.
Do you want to continue? [Y/n] 
Get:1 http://ftp.uni-stuttgart.de/ubuntu xenial/main amd64 ca-certificates-java all 20160321 [12,9 kB]
Get:2 http://ftp.uni-stuttgart.de/ubuntu xenial/universe amd64 openjdk-9-jre-headless amd64 9~b114-0ubuntu1 [183 MB]
Get:3 http://ftp.uni-stuttgart.de/ubuntu xenial/universe amd64 openjdk-9-jre amd64 9~b114-0ubuntu1 [51,5 kB]                       
Fetched 183 MB in 2min 21s (1.293 kB/s)                                                                                            
Selecting previously unselected package ca-certificates-java.
(Reading database ... 210101 files and directories currently installed.)
Preparing to unpack .../ca-certificates-java_20160321_all.deb ...
Unpacking ca-certificates-java (20160321) ...
Selecting previously unselected package openjdk-9-jre-headless:amd64.
Preparing to unpack .../openjdk-9-jre-headless_9~b114-0ubuntu1_amd64.deb ...
Unpacking openjdk-9-jre-headless:amd64 (9~b114-0ubuntu1) ...
Selecting previously unselected package xorg-sgml-doctools.
Preparing to unpack .../xorg-sgml-doctools_1%3a1.11-1_all.deb ...
Unpacking xorg-sgml-doctools (1:1.11-1) ...
Selecting previously unselected package x11proto-core-dev.
Preparing to unpack .../x11proto-core-dev_7.0.28-2ubuntu1_all.deb ...
Unpacking x11proto-core-dev (7.0.28-2ubuntu1) ...
Selecting previously unselected package libice-dev:amd64.
Preparing to unpack .../libice-dev_2%3a1.0.9-1_amd64.deb ...
Unpacking libice-dev:amd64 (2:1.0.9-1) ...
Selecting previously unselected package libpthread-stubs0-dev:amd64.
Preparing to unpack .../libpthread-stubs0-dev_0.3-4_amd64.deb ...
Unpacking libpthread-stubs0-dev:amd64 (0.3-4) ...
Selecting previously unselected package libsm-dev:amd64.
Preparing to unpack .../libsm-dev_2%3a1.2.2-1_amd64.deb ...
Unpacking libsm-dev:amd64 (2:1.2.2-1) ...
Selecting previously unselected package libxau-dev:amd64.
Preparing to unpack .../libxau-dev_1%3a1.0.8-1_amd64.deb ...
Unpacking libxau-dev:amd64 (1:1.0.8-1) ...
Selecting previously unselected package libxdmcp-dev:amd64.
Preparing to unpack .../libxdmcp-dev_1%3a1.1.2-1.1_amd64.deb ...
Unpacking libxdmcp-dev:amd64 (1:1.1.2-1.1) ...
Selecting previously unselected package x11proto-input-dev.
Preparing to unpack .../x11proto-input-dev_2.3.1-1_all.deb ...
Unpacking x11proto-input-dev (2.3.1-1) ...
Selecting previously unselected package x11proto-kb-dev.
Preparing to unpack .../x11proto-kb-dev_1.0.7-0ubuntu1_all.deb ...
Unpacking x11proto-kb-dev (1.0.7-0ubuntu1) ...
Selecting previously unselected package xtrans-dev.
Preparing to unpack .../xtrans-dev_1.3.5-1_all.deb ...
Unpacking xtrans-dev (1.3.5-1) ...
Selecting previously unselected package libxcb1-dev:amd64.
Preparing to unpack .../libxcb1-dev_1.11.1-1ubuntu1_amd64.deb ...
Unpacking libxcb1-dev:amd64 (1.11.1-1ubuntu1) ...
Selecting previously unselected package libx11-dev:amd64.
Preparing to unpack .../libx11-dev_2%3a1.6.3-1ubuntu2_amd64.deb ...
Unpacking libx11-dev:amd64 (2:1.6.3-1ubuntu2) ...
Selecting previously unselected package libx11-doc.
Preparing to unpack .../libx11-doc_2%3a1.6.3-1ubuntu2_all.deb ...
Unpacking libx11-doc (2:1.6.3-1ubuntu2) ...
Selecting previously unselected package libxt-dev:amd64.
Preparing to unpack .../libxt-dev_1%3a1.1.5-0ubuntu1_amd64.deb ...
Unpacking libxt-dev:amd64 (1:1.1.5-0ubuntu1) ...
Selecting previously unselected package openjdk-9-jre:amd64.
Preparing to unpack .../openjdk-9-jre_9~b114-0ubuntu1_amd64.deb ...
Unpacking openjdk-9-jre:amd64 (9~b114-0ubuntu1) ...
Selecting previously unselected package openjdk-9-jdk-headless:amd64.
Preparing to unpack .../openjdk-9-jdk-headless_9~b114-0ubuntu1_amd64.deb ...
Unpacking openjdk-9-jdk-headless:amd64 (9~b114-0ubuntu1) ...
Preparing to unpack .../openjdk-9-jdk_9~b114-0ubuntu1_amd64.deb ...
Unpacking openjdk-9-jdk:amd64 (9~b114-0ubuntu1) ...
dpkg: error processing archive /var/cache/apt/archives/openjdk-9-jdk_9~b114-0ubuntu1_amd64.deb (--unpack):
 trying to overwrite '/usr/lib/jvm/java-9-openjdk-amd64/include/linux/jawt_md.h', which is also in package openjdk-9-jdk-headless:amd64 9~b114-0ubuntu1
Processing triggers for ca-certificates (20160104ubuntu1) ...
Updating certificates in /etc/ssl/certs...
0 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...
done.
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for gnome-menus (3.13.3-6ubuntu3) ...
Processing triggers for desktop-file-utils (0.22-1ubuntu5) ...
Processing triggers for bamfdaemon (0.5.3~bzr0+16.04.20160415-0ubuntu1) ...
Rebuilding /usr/share/applications/bamf-2.index...
Processing triggers for mime-support (3.59ubuntu1) ...
Processing triggers for hicolor-icon-theme (0.15-0ubuntu1) ...
Errors were encountered while processing:
 /var/cache/apt/archives/openjdk-9-jdk_9~b114-0ubuntu1_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)

So obviously this seems to be a bug and caused by incorrect packaging and not a fault on my side. There also are some related bug reports on Launchpad already, like e.g. this report which seems to be the most important one. I subscribed to that report, but there's no solution for Xenial yet.

Therefore my question here is not about this packaging bug, but how to work around this and install openjdk-9-jdk anyway, ignoring the "trying to overwrite file" error?


Solution 1:

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.

Solution 2:

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.