Denisowator Denisowator - 1 month ago 8
Java Question

Calling upon a .class file inside a source code?

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.

Answer

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.