Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

594
Views
How can a Java program list all partitions and get the free space of them on Linux?

I want the name of the partition with their total space, used space and free space for the Linux system using java program.

I am getting correct value in case of Windows system but in Linux I am getting only one drive information:

Here is what I have tried so far.

public class DiskSpace {

public static void main(String[] args) {
    FileSystemView fsv = FileSystemView.getFileSystemView();
    File[] drives = File.listRoots();
    if (drives != null && drives.length > 0) {
        for (File aDrive : drives) {
            System.out.println("Drive Letter: " + aDrive);
            System.out.println("\tType: " + fsv.getSystemTypeDescription(aDrive));
            System.out.println("\tTotal space: " + aDrive.getTotalSpace());
            System.out.println("\tFree space: " + aDrive.getFreeSpace());
            System.out.println();
        }
    }
}
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

There are no drive letters on Linux. If you want to know which partitions there are, and where they are mounted, read /proc/mounts. When you have a mount point (2nd column in /proc/mounts), use new File(mountpoint).getTotalSpace() to get the total space.

over 4 years ago · Santiago Trujillo Report

0

Linux, Unix, and Unix-like systems have one filesystem with one root within which there may be multiple mount points at which partitions containing Unix filesystems, partial or complete, may be mounted -- non-Unix filesystems may also be mounted with the appropriate software available to handle the transformations necessary, but the unified, single-root filesystem model remains.

If the FileSystemView class you are using is from the javax.swing.filechooser package, don't expect too much:

FileSystemView is JFileChooser's gateway to the file system. Since the JDK1.1 File API doesn't allow access to such information as root partitions, file type information, or hidden file bits, this class is designed to intuit as much OS-specific file system information as possible.

Java Licensees may want to provide a different implementation of FileSystemView to better handle a given operating system.

That second paragraph is key.

Java's virtual machine implementation is meant to abstract away the very kind of platform specific things you want in this case. To be successful, you will need to write or find native call wrapper classes for the native system API of each platform you will support. "High-level" abstractions like the FileSystemView class are unlikely to be complete or reliable in providing the information you need.

over 4 years ago · Santiago Trujillo Report

0

This is a code snippet to display name mounted external media only.

String OS = System.getProperty("os.name");
if(OS.equals("Linux"))
{
    String s = "";
    Runtime rt = Runtime.getRuntime();
    int n=0;
    Process ps = rt.exec("ls /run/media/Rancho");// Write your UserName, mine is Rancho

    InputStream in = ps.getInputStream();
    while((n = in.read())!=-1)
    {
        char ch = (char) n;
        s+ = ch;
    }
    System.out.println(s);  
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!