0

I'm building a class to run the main methods of a few other classes my co-worker build. This works by scanning a folder for files and running every main method neccesary, based on filenames. I had a large chunk of if statements to run the main methods of the classes corresponding with the filenames.

The typical if statement was like:

if (f.contains("FileX")) {
                Files.copy(Paths.get("D:\\" + yearmonth.format(date) + "\\" + dateFormat.format(date) + "\\" + f),
                        Paths.get("C:/eclipse-workspace/javarepository/rapporting/" + f),
                        StandardCopyOption.REPLACE_EXISTING);
                String[] pasStr = new String[] { f };
                FileX.main(pasStr);
}

FileX was the string name, the corresponding class has the same name. I would like to skip all the If statements and just do it like this(that doesnt work):

                Files.copy(Paths.get("D:\\" + yearmonth.format(date) + "\\" + dateFormat.format(date) + "\\" + f),
                        Paths.get("C:/eclipse-workspace/javarepository/rapporting/" + f),
                        StandardCopyOption.REPLACE_EXISTING);
                String[] pasStr = new String[] { f };
                FileX.main(pasStr);
}

The problem is, of course, the fileX is a string value. So I tried everything I could find, like
Class cls = Class.forName("FileX"); cls.main(pasStr);

but I can't find a solution that works. Anyone who knows of a way to get this working?

Raymond P
  • 752
  • 2
  • 15
  • 27

0 Answers0