Simple Antivirus using MultiThreading

TUTORIAL NO 3

Antivirus using MultiThreading

In this tutorial you will learn: 1. MultiThreading 2. Recursion 3. Event handling 4. Layout Manager 5. JAVA awt basics 6. JAVA swing basics 7. Adapters 8. Anonymous classes Sometimes there is a very basic virus in your system i.e a file spread in all the directories of your disk drive. It’s totally an unwanted file (You can say a virus ) and it may be sometimes hidden but you can see it in the task manager. So today I am going to teach you how to make a simple antivirus for that and in the next tutorial I will teach you how to make that virus so stay tuned. The User Interface layout In this tutorial we will be working in JAVA SWING. The first thing that we are going to do is setting up a JFrame and adding the required panels and Components. Basic step: Download and install ECLIPSE and set up a JAVA PROJECT. Then create a new class and name it AntiVirus. Then follow the steps 1. IMPORT STATEMENTS First of all write these import statements in your .java file
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.*;
We require event import for mouse click , awt and swing imports for visual design. For this tutorial we will only be using one public class and we will be extending it from JFrame (i.e inheritance). 2.SETTING UP THE FRAME CLASS
  1. public class AntiVirus extends JFrame implements Runnable {
  2.  
  3. File[] myfile;
  4. int directories_count = 0;
  5. JTextField t_field = new JTextField("", 50); // for file name
  6. JTextArea t_area = new JTextArea(); // for displaying directories
  7. JLabel file_label= new JLabel("File Name :"); // for file name label
  8. JLabel dir_label = new JLabel("Directory :"); // for directory label
  9. JLabel status = new JLabel("Status: Ready"); // current status label
  10. JScrollPane sp = new JScrollPane(t_area); // scroll bars in text area
  11. JComboBox jcb = new JComboBox(); // for combo box
  12. JButton cleanVirus = new JButton("Clean Virus"); //for search button
  13.  
  14. String file_name; //for storing file name
  15. String dir; //for storing directory
  16. boolean deleted = false; //for changing the status
create a public frame class by extending it from jframe and implementing the runnable interface for multithreading. Now create all the required variables. File array reference for getting the root directories then a counter to count the number of directories on your system. Then declaring all the text fields and labels according to the User Interface shown in the picture above. We need text fields and their labels. Other than that we need a label to set the status, scroll pane for the text area scroll bar, text area to show the traversed directories, combo box to show the directories on your system, then finally the cleanVirus button other than that we need to declare variables to store file name and directory and a Boolean to change the status whether the virus id found and deleted or not. 3. WRITING THE CONTSTUCTOR We will do everything in a constructor so that the jframe gets setup whenever we create the object of that JFrame. Now writing the constructor.
  1. AntiVirus() {
  2.  
  3. directories_count = File.listRoots().length;//counting total disk drives
  4. myfile = File.listRoots(); // a file object for storing root directories
  5.  
  6. for(int i=0;i<directories_count;i++)
  7. jcb.addItem(myfile[i]); // adding directories in comboBox
  8.  
  9. JPanel up = new JPanel(); //making a new panel to add north
  10. add(up, "North"); //adding panel in north position
  11. up.add(file_label); //first adding File Name label
  12. up.add(t_field); // then text field to enter file name
  13. up.add(dir_label); //directory label
  14. up.add(jcb); // then comboBox
  15. up.add(status); // status label
  16. up.add(cleanVirus); // search button
  17.  
  18. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19. setSize(1024, 700); //frame size
  20. setVisible(true); //frame visibility
  21.  
  22. add(sp); // adding scroll pane
First of all we will count the number of directories and save the root directories in our file array. Then we will add all the directories name to the combo box. After that we will created another jpanel and add it to north position, then we added all the labels and the text fields as shown in the picture above in that panel. After that we setup the frame size and visibility.
  1. cleanVirus.addMouseListener(new MouseAdapter(){
  2.  
  3. public void mouseClicked(MouseEvent me) { //overriding
  4.  
  5. file_name = t_field.getText();// getting the text from text field
  6. dir = jcb.getSelectedItem().toString(); // directory selected from combo box
  7. dir +="\\"; //appending double slashes to dir e.g C:\\ status.setText("Finding Virus"); //setting the status
  8. startThread(); //creating a new thread using this function
  9. }
  10. }); // end anonymous class and mouselistener
  11. } // end constructor
In the mouselistener we created an anonymous MouseAdapter class (Remember: anonymous class is the one in which you only have to create one object). Now we override the mouse clicked function. The first thing we need to do is to get the file name from the text field and directory from the combo box. Then we set the status of our app. After that we called a function to start the thread and end the class ,listener and the constructor. The startThread() function is defined below.
  1. public void startThread(){
  2. Thread t = new Thread(this); //making a thread object
  3. t.start(); //starting thread
  4. }
In this function we created an instance of thread class and pass it the runnable component of the class using this. Then we start the thread. 4. OVERRIDING THE RUN FUNCTION:
  1. @Override
  2. public void run() { // overriding of runnable interface
  3. try{
  4. DeleteVirus(file_name, new File(dir)); //calling delete virus function
  5. if (deleted == true)
  6. status.setText("Virus Deleted"); // if file is deleted then set label
  7. else
  8. status.setText("No Virus found"); // otherwise no file is found }catch(Exception e){} //handling the exception
  9. }//end run
After implementing the runnable interface we will override the run function and call the DeleteVirus function in it by passing the file name and directory to it’s parameters and enclosing it in a try catch block to catch any exception in the thread. We will define this function later. Now we need to set the status after all the directories are traversed so we set the status according to the value of our boolean variable, depending on the file if it’s deleted or not. 4. DEFINING THE DELETEVIRUS FUNCTION:
  1. public void DeleteVirus(String v_dir, File v) {
  2.  
  3. File[] list = v.listFiles(); //listing files
  4.  
  5. if (list != null) //checking so that nullpointerException cannot occur
  6. {
  7. for (File subfile : list) { //listing every subdirectory
  8.  
  9. t_area.append(""+v.getAbsolutePath()); //showing in text area
  10. t_area.append("\n");
  11.  
  12. File v_file = new File(v.getAbsolutePath(), file_name); // getting the path of file
  13.  
  14. if(v_file.isFile()) //v_file points to a file
  15. {
  16. v_file.delete(); // delete that file which has specified name
  17. deleted = true; // turn boolean to true to set the label later
  18. }
  19. if (subfile.isDirectory()) {
  20. DeleteVirus(v_dir, subfile); //if more directories then search in them
  21. }
  22. }//end for
  23. }//end if
  24. }//end DeleteVirus
Now we define the delete virus function it takes two parameters file directory and file reference. First of all we list the files in the directories and store it in our list of files array. Then if we check if the list of files in the directories in not equal to null so that we don’t get null pointer exception. Then we start going through all the files and keep appending their path to the text area. After that we created a temporary reference v_file for the loop with user defined directory and user defined file name and check if the current file matches that file or not. If does we delete that file otherwise we move on and check whether it’s a directory or not. If it is we move in that directory by calling the function again other wise we move on to are next iteration of the loop and check. 4. DEFINING THE MAIN FUNCTION
  1. public static void main(String args[]) {
  2. new AntiVirus(); // creating the object of antivirus frame class
  3. }// end main
  4. }// end filefinder class
In the main function we only created the object of the JFrame class and our application is complete. DO READ THE NEXT TUTORIAL FOR THE VIRUS. 5. OUPUT: output

Tags

Add new comment