0

I've been working on a program for a while now, and I've run into countless problems that just seemed impossible to fix. Luckily I got help every time. But this one is very confusing, and I'm not really sure how to describe it properly.

Basically what I have is a main source code, which is what people are going to run, and what creates the interface. And I have another source code, which is what deletes files from a specified directory.

What I need to do, is somehow call upon the code that deletes the files. And I need to call upon it from within the main source code.

Here is one of the if statements I'm working with:

if(chckbxTemporaryFilesUser.isSelected()) {

}

As you can see, the if statement checks for whether a checkbox is checked. Now what I want to do, is somehow activate the delete code if this returns true.

What I'm making basically, is like a simple cleaning software. You click a checkbox called "Temporary Files", and if it's checked, another code will delete all the files inside a "Temporary Files" folder. I have all my if statements connected to a JButton, through an Action Listener. So the code will only execute if the button is also pressed.

I tried just copy and pasting the whole code inside that if statement. But no matter how I change it, whether I delete the class or the methods, or change them somehow, it always shows up a ton of errors. Which from my experience means that it's not how I'm supposed to go about doing it.

Here is the code that deletes the temp files:

public class TempFiles {
private static final
    String SRC_FOLDER = System.getProperty("user.home") + "\\AppData\\Local\\Temp";

public static void main(String[] args) {
    File directory = new File(SRC_FOLDER);

    //Check if directory exists
    if(!directory.exists()) {
       System.out.println("Directory does not exist.");
       System.out.println("Skipping directory.");
       System.exit(0);
    }
    else {

       try {
       delete(directory);
       }

       catch(IOException e){
           e.printStackTrace();
           System.exit(0);
       }
    }

    System.out.println("Cleaned directory " + SRC_FOLDER + ".");
}

public static void delete(File file)
    throws IOException{

    if(file.isDirectory()){

        //If directory is empty
        if(file.list().length==0){

        }
    else {

           //List all the directory contents
           String files[] = file.list();

           for (String temp : files) {
              //Construct the file structure
              File fileDelete = new File(file, temp);

              //Recursive delete
             delete(fileDelete);
           }

           //Check the directory again
           if(file.list().length==0) {
           }
        }

    }
  else {
        //If file exists, then delete it
        file.delete();
        System.out.println("File is deleted : " + file.getAbsolutePath());
    }
  }
}

So basically what I'm asking is, how do I run a piece of code, with another piece of code?

Please try to explain your answers in simple terms, if they do involve a lot of code, as I'm still pretty new to Java, despite working with it for at least a month now. I've looked around, and I found some questions that were sort of similar, but all the answers and suggestions were just way too technical for me to understand.

P.S. Like the title of the question should also indicate, the two codes are in different files.

1 Answers1

2

First, there should be nothing stopping you from calling the main method of another class:

if(chckbxTemporaryFilesUser.isSelected()) {
    TempFiles.main(new String[0]);
}

There are two issues with this, however:

  1. It’s not how it’s usually done, so it will tend to confuse the reader.
  2. The calls to System.exit() inside TempFiles.main() will cause also the calling program to terminate. I assume this is not what you want.

For the first item I’d prefer to take the code deleting the temporary files out into a separate static method still in the TempFiles class:

public static void deleteTemporaryFiles() {
    File directory = new File(SRC_FOLDER);

    // Check if directory exists

    // ... etc. ...

    System.out.println("Cleaned directory " + SRC_FOLDER + ".");
}

Call this new method both from TempFiles.main() and from your if statement checking on the check box. From main():

public static void main(String[] args) {
    deleteTemporaryFiles();
}

From your if statement:

    if (chckbxTemporaryFilesUser.isSelected()) {
        TempFiles.deleteTemporaryFiles();
    }

For item 2., you need to replace the lines reading System.exit(0); with just

        return;

If you need further clarification, please ask, for instance in a comment.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • It works, but after it deletes the files, a lot of "Unknown Source" errors appear in the console (i'm currently using Eclipse). The same errors appear when I run it from Command Prompt. – Denisowator Oct 26 '16 at 22:40
  • I don't know whether it's anything to do with this, but until now, these errors never appeared. This isn't really an issue for me, since it still technically works. But I'd just like to know what's causing these errors, and if there's any way of preventing them from appearing. – Denisowator Oct 26 '16 at 22:41
  • I undertand your worry and interest in understanding. I tried a quick search, all I found was that “unknown source” is sometimes printed in a stacktrace (instead of a source file and line number). Maybe if you search a little more thoroughly, something will turn up. – Ole V.V. Oct 26 '16 at 22:51
  • Is that all the message says, “Unknown source”? If there’s a longer message, you may want to quote it all. – Ole V.V. Oct 26 '16 at 23:02
  • Might it be coming from `e.printStackTrace();` in `TempFiles`? – Ole V.V. Oct 26 '16 at 23:03
  • How do I quote something in comments. Or would I just paste it? – Denisowator Oct 26 '16 at 23:24
  • It's not the `e.printStackTrace();` I deleted it, and even deleted one in the main code file. The errors still appear. Also, the beginning of the message reads _Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException_, and then it lists a ton of errors, all starting with _at_ and then either _javax.swing._ or _java.awt._. There are also a few that say _java.security._. – Denisowator Oct 26 '16 at 23:37
  • You’re right, @Denisowator, you cannot format nicely in comments. I suggest you either edit your original question and add the quote, or you post an entirely new question. – Ole V.V. Oct 27 '16 at 05:42
  • 1
    Ah, that’s a stack trace from your event thread. Read more here: [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors#3988794) (it’s a common thing, so you can be sure to find it described ob Stackoverflow and also other places). – Ole V.V. Oct 27 '16 at 05:45
  • Thank you for the link. I just found out that the errors were showing up, because the class files weren't all in the same folder. I had the main file in one folder, and the rest in a subfolder. – Denisowator Nov 01 '16 at 11:37