dpkg:unrecoverable fatal error (files list file for package 'java-common' is missing final newline)
Asked Answered
M

6

7

I am trying to install packages on my linux OS using the command apt-get install .... The problem is that I get this error:

Selecting previously unselected package liberror-perl.
dpkg: unrecoverable fatal error, aborting:
 files list file for package 'java-common' is missing final newline
E: Sub-process /usr/bin/dpkg returned an error code (2)

Googling the error (even line by line), the solution SEEMS to be "to download and install missing packages to resolve dependencies" as it was point out here. The problem is that when I try to execute sudo apt-get -f install I continue to have the same error again and again. Any suggestion? How can I change the packages if I cannot use apt-get?

Maimaia answered 23/9, 2016 at 15:51 Comment(0)
M
20

I have solved the problem using the python script:

#!/usr/bin/python


# 8th November, 2009
# update manager failed, giving me the error:
# 'files list file for package 'xxx' is missing final newline' for every package.
# some Googling revealed that this problem was due to corrupt files(s) in /var/lib/dpkg/info/
# looping though those files revealed that some did not have a final new line
# this script will resolve that problem by appending a newline to all files that are missing it
# NOTE: you will need to run this script as root, e.g. sudo python newline_fixer.py

import os

dpkg_path = '/var/lib/dpkg/info/'
paths = os.listdir(dpkg_path)
for path in paths:
    path = dpkg_path + path
    f = open(path, 'a+')
    data = f.read()
    if len(data) > 1 and data[-1:] != '\n':
        f.write('\n')
        print 'added newline character to:', path
    f.close() 

After running the script with the command sudo python name_script.py, the problem was solved: it seems that some files were corrupted. The solution was proposed here

Maimaia answered 23/9, 2016 at 16:0 Comment(2)
for those coming here via aol keyword apt-get newline indentation if your example off, and last time i checked in python 🐍 indentation matters. – Oberheim
Thank you, this script did the trick. Was upgrading an old Raspberry Pi 4 image from 10 to 11 so it could run on the newer pi 4b hardware. – Plebs
P
6

You have to remove that missing file

 sudo rm /var/lib/dpkg/info/java-common.list 

Now you can reinstall this

 sudo apt-get install java-common --reinstall 
Psyche answered 13/5, 2020 at 8:37 Comment(5)
Nope. The problem is that it is IMPOSSIBLE to use apt-get. See the question for further details. – Maimaia
Yeah I know, You have to remove this file then automatically it will work if not then you should run this command also "sudo apt autoremove" and "sudo apt clean" – Psyche
this worked for me - it is just a bit difficult to find the right list file. – Voluptuary
after deleting the file i just did apt upgrade and got the message dpkg: warning: files list file for package 'xxx' missing; assuming package has no files currently installed – Voluptuary
It worked for me running 'apt upgrade' after removing the offending 'foo.list' to fix the error on my raspberry pi when I ran into this issue. – Castaway
F
1

To anyone trying this out on python3 and getting this error:

Traceback (most recent call last):
  File "/home/atharva/Documents/development/fix_updates.py", line 20, in <module>
    f.seek(-1, os.SEEK_END)
io.UnsupportedOperation: can't do nonzero end-relative seeks

the following modified script (original posted above by @Leos313) should do the trick

#!/usr/bin/python3

import os

dpkg_path = '/var/lib/dpkg/info/'
paths = os.listdir(dpkg_path)

for path in paths:
    full_path = os.path.join(dpkg_path, path)
    try:
        with open(full_path, 'r+b') as f:
            f.seek(0, os.SEEK_END)
            if f.tell() > 0:
                f.seek(-1, os.SEEK_END)
                last_char = f.read(1)
                if last_char != b'\n':
                    f.write(b'\n')
                    print(f'Added newline character to: {full_path}')
    except IOError as e:
        print(f"Error processing {full_path}: {e}")
    except Exception as e:
        print(f"Unexpected error processing {full_path}: {e}")
Fealty answered 10/9 at 5:29 Comment(0)
H
0

If you get the below error

dpkg: unrecoverable fatal error, aborting: files list file for package 'java-common' is missing final newline E: Sub-process /usr/bin/dpkg returned an error code (2)

Note : java-common is the list name

Solve this using below commands(Modify java-common with your list name)

sudo rm /var/lib/dpkg/info/java-common.list

sudo apt-get install java-common --reinstall

sudo dpkg --configure -a sudo apt update sudo apt upgrade

Hipped answered 27/4, 2021 at 6:24 Comment(1)
nope; it was impossible to use apt . If you do not solve it, you cannot use sudo apt-get install ... – Maimaia
H
0
  • sudo dpkg --configure -a
  • sudo apt-get -f install sudo apt-get clean
  • sudo apt-get update && sudo apt-get upgrade

It fixed the problem for me.

Howlett answered 14/10, 2021 at 14:7 Comment(2)
While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. – Thrall
What does "sudo apt-get -f install sudo apt-get clean" do? – Accident
A
0

I tried the below command and it worked for me

$ sudo apt-get update
$ sudo apt update && sudo apt upgrade
Accouterment answered 16/7, 2022 at 6:24 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.