Java Non-Recursive du command popping java.lang.InterruptedException

  • Comp Sci
  • Thread starter peterpiper
  • Start date
  • Tags
    Java
In summary, the program calculates and prints the size of the filesystem object c:\foo\bar, which occupies 123625 bytes on a computer running Windows.
  • #1
peterpiper
14
0

Homework Statement



You are to write a robust program (i.e., a public static void main()) that accepts a file pathname (e.g. 'c:\foo\bar' or '/home/jblow/' under UNIX) as input and then calculates and prints the size of that filesystem object:

java FileSize c:\foo\bar
123625 bytes

The purpose of the program is to compute the total size in bytes occupied by the given object. For files, this is just the size of the file itself. For directories this will be the recursive sum of the sizes of the objects inside the directory.

Homework Equations



N/A

The Attempt at a Solution



Code:
import java.io.File;
import javax.swing.JFileChooser;
import java.util.Stack;
public class FileSize 
{

	/**
	 * FileSize computes the total size of a directory or file.
	 * @param args
	 */
	public static void main(String[] args) 
	{
		//Variable Declaration
		Stack <File> folderStack = new Stack<File>();
		long fileSize = 0;
		JFileChooser chooser = new JFileChooser();
		
		//Only allow the user to select a directory for the time being
		chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		int returnValue = chooser.showOpenDialog(null);
		
		//If a valid file is selected proceed
    if (returnValue == JFileChooser.APPROVE_OPTION) 
    {
   	//Create a file object from the directory
      File selectedFile = chooser.getSelectedFile();
      //If the file can be read proceed
      if(selectedFile.canRead())
      {
      	if(selectedFile.isDirectory())
      	{
      		//If the file object is a directory, push it on a stack to be operated on later
      		folderStack.push(selectedFile);
      		//Until the stack is empty, keep calculating directory sizes
      		while(!folderStack.isEmpty())
      		{
      			//List directory contents
      			File[] listOfFiles = folderStack.pop().listFiles();
      			for (int i = 0; i < listOfFiles.length; i++)
      			{
      				//If the item is a file, add to total size
      				if(listOfFiles[i].isFile())
      				{
      					fileSize += listOfFiles[i].length();
      					System.out.println(fileSize + " bytes.");
      				}
      				//if it's a directory, push it onto the stack
      				else
      				{
      					folderStack.push(listOfFiles[i]);
      				}
      			}
      		}
      	}
      	//If the selected item is just a file, calculate the size
      	else
      	{
      		fileSize = selectedFile.length();
      	}
      }
      else
      {
      	System.out.println("Borked");
      }
      //Report the size
      long fileSizeKB = fileSize / 1024;
      System.out.println(fileSize + " bytes.");
    }
  }
}

Question [\b]

So overall I have the correct functionality(minus the JFileChooser, but that will change), provided there aren't any protected files in the directory, but I keep getting an odd exception thrown after the final reporting of the file size.
Code:
Exception while removing reference: java.lang.InterruptedException
java.lang.InterruptedException
	at java.lang.Object.wait(Native Method)
	at java.lang.ref.ReferenceQueue.remove(Unknown Source)
	at java.lang.ref.ReferenceQueue.remove(Unknown Source)
	at sun.java2d.Disposer.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)

An even stranger thing is that it only occasionally does it. I don't change code but it only does it may once out of every 6ish runs? By what I gather it's a threading exception but I don't know why I would be popping that here. Any suggestion for how to drop the error?
 
Physics news on Phys.org
  • #2
I can't seem to reproduce your error on my Linux box with openjdk, but I do occasionally get a null pointer exception from:

Code:
File[] listOfFiles = folderStack.pop().listFiles();
for (int i = 0; i < listOfFiles.length; i++)

This will happen whenever the File object returned from your folderStack.pop() call is not a directory, or when an I/O error occurs, as both scenarios will assign null to listOfFiles, causing the conditional in your for loop to throw an exception.

This may or may not also be related to your error. I suggest you fix this section of code and try it again several times (with whichever directory was giving you problems before) and see if the issue is still occurring. I suspect that there is a bug in your JDK implementation which is resulting in improper handling of a particular I/O error.
 
  • #3
By what I gather my error was coming from something wacky with the JFileChooser. On my Windows machines it would pop whenever I chose the default directory brought up by the GUI. As for the null pointers, I just haven't gotten to the point where I was worried about dealing with protected files, which have so far in my experience been the cause of that particular exception. I'm just having trouble tracking down where exactly to check for the nulls.
 
  • #4
peterpiper said:
As for the null pointers, I just haven't gotten to the point where I was worried about dealing with protected files, which have so far in my experience been the cause of that particular exception. I'm just having trouble tracking down where exactly to check for the nulls.

It isn't just protected files that will cause that error. In your code, you aren't checking whether folderStack.pop() has returned an ordinary file, or a directory (or something else entirely - there are other possibilities for certain architecture & JDK combinations, like drivers for example). If it is an ordinary file (one that you have pushed into your Stack in a previous iteration of the while loop), the subsequent call to listFiles() will return a null object reference (see here). You will need an if statement to ensure that the for loop is only processed when folderStack.pop() is a directory, and that you simply add the size to your total of the file if it is an ordinary file. You will also need an additional if statement to test whether listFiles() returned null due to a file being protected (or any other I/O error) in the case of a directory, prior to executing your for loop.
 
  • #5

Thank you for sharing your code and the error you are encountering. It appears that the error is related to the disposal of objects in your code. Without seeing the full code and running it myself, it is difficult to determine the exact cause of the error. However, here are a few suggestions that may help resolve the issue:

1. Make sure you are properly closing any resources that you are opening, such as file streams, to prevent any unexpected behavior.
2. Consider using a try-catch block to handle the InterruptedException and any other potential exceptions that may be thrown in your code.
3. You may also want to consider using a different method for calculating the file size, such as the Files.size() method in the java.nio.file package. This may provide a more reliable and efficient way to calculate the file size.
4. If the issue persists, try debugging your code to see if you can pinpoint where the error is occurring and find a solution from there.

I hope these suggestions are helpful in resolving the error you are encountering. Keep up the good work in writing robust and efficient code.
 

Related to Java Non-Recursive du command popping java.lang.InterruptedException

1. What is Java Non-Recursive du command popping java.lang.InterruptedException?

Java Non-Recursive du command popping java.lang.InterruptedException is an exception that is thrown when a thread is interrupted while waiting for a blocking operation to complete, such as when using the du command in Java to retrieve the disk usage of a file or directory.

2. How does the Interrupted Exception affect the Java Non-Recursive du command?

The Interrupted Exception can disrupt the normal execution of the du command, causing it to terminate prematurely and potentially leading to incorrect results. It can also cause the thread to enter a blocked state, which can impact the performance of the program.

3. How can I handle the Interrupted Exception in my Java code?

You can handle the Interrupted Exception by using a try-catch block to catch the exception and perform appropriate error handling or recovery actions. You can also use methods like Thread.interrupted() or Thread.isInterrupted() to check for the interrupted status of the thread and handle it accordingly.

4. Can I prevent the Interrupted Exception from occurring in my Java Non-Recursive du command?

While it is not possible to completely prevent the Interrupted Exception from occurring, you can take steps to minimize its likelihood. This can include using a timeout for the blocking operation, implementing proper error handling and recovery mechanisms, and properly managing thread interrupts.

5. Is the Interrupted Exception specific to the Java Non-Recursive du command, or can it occur in other situations as well?

The Interrupted Exception is a general exception that can occur in various situations, not just in the context of the Java Non-Recursive du command. It can be thrown when a thread is interrupted while performing any blocking or waiting operation, such as I/O operations or network calls.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
8K
  • Programming and Computer Science
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
Back
Top