0

I have the following code that retrieves a list of files from a folder and in my for statement i want to compare them to see do the names match

        Class classFile = null;

        // Retrieve the java file 
        final File javaFolder = new File(filePath);

        // Retrieves all the class files
        List<Class> classFiles = new ArrayList<>();
        final File classFolder = new File(compileFile.getDirectoryForBin());
        classFiles = retrieveFiles.listClassFilesForFolder(classFolder);

        // compared the java files to the class files and add the correct files to the list
        for (Class currentClassFile : classFiles) {.....}

Im getting the following error

java.lang.ClassCastException: java.io.File cannot be cast to java.lang.Class
at Java.OrganisingFiles.getClasses(OrganisingFiles.java:129)    

This is because i have a list of files that cannot be cast to Class in my for loop. Does anybody know how to convert a File to a Class type or is it better to just to cast all the files as objects as java.lang.Class is a subclass of java.lang.Object?

3
  • 2
    What do you need the Class for? Commented Aug 9, 2012 at 15:27
  • Indeed, why do you need a Class? From your loop it looks like you'll only need to compare file names? Commented Aug 9, 2012 at 15:27
  • You can't convert a File into a Class, nor do you have any need to. If all you want is the names, just compare the names of the Files Commented Aug 9, 2012 at 15:32

3 Answers 3

2

change these lines:

  Class classFile = null;

  List<Class> classFiles = new ArrayList<>();
 for (Class currentClassFile : classFiles) {.....}

to this:

  File classFile = null;

  List<File> classFiles = new ArrayList<>();
  for (File currentClassFile : classFiles) {.....}

A class file is also a File

Sign up to request clarification or add additional context in comments.

3 Comments

so does this mean that if the currentClassFile = Test.class instead of writing Test.class you can use currentClassFile.class because that is what i need to do at the end of the day?
@flexinIT currentClassFile is a reference to the object that is being iterated. you can use getName() API to fetch the name. if you use getClass().getName() api you will get ClassName. Did I answer your question?
you cannot cast a File to a Class as they are incompatible hence the ClassCastException. They have to be same or one subclass of another.
1

I would recommend you take a look at iterateFiles(File directory, String[] extensions, boolean recursive) method from the Apache FileUtils. There is a difference between files ending in the .class extension and the actual Class object.

The above function should allow you to get you an iterator File which will allow you to iterate over files having the .class extension.

Comments

0

1. Class class represents the classes, interfaces , etc during a running java application.

2. Now use listFile() method to get all the File Objects and then store them in a collection like ArrayList<String> and compare them..

Eg:

    ArrayList<String> fnames = new ArrayList<String>();
    File f = new File("d:\\Myfolder");
    File[] fArr = f.listFiles();

    for (File fN : fArr){

        fnames.add(fN.getName());

    }

And now you can compare them .....

4 Comments

all the files that are being returned in my method are .class files so i want to be able to, instead of hard coding classes like Test.class i can just use currentClassFile
do you have to import anything for .getInstance() as it is not appearing for me
Sorry for the typo..... Use this.... Class<File> file = File.class; try { File m = file.newInstance(); } catch (Exception e1) { e1.printStackTrace(); }
Its newInstance() Not getInstance()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.