0

I am trying to get the parent folder of my jar, and search all the way through it. The reason being is that I am modifying all of the .class files beneath the jar on the build path (there are a bunch of jars below it).

I am currently getting an NPE on dir.listFiles() in the searchForClasses method, the reason being is that when I am creating a new instance of file, it is not the correct path. (I believe)

I currently have this:

    public void searchForClasses(File dir) {
    for (File f : dir.listFiles()) {
        if (f.getName().endsWith(".class")) {
            for (byte[] bytes : NMSversions)
                fix(f.getAbsolutePath(), bytes, NMS);
            for (byte[] bytes : OBCversions)
                fix(f.getAbsolutePath(), bytes, OBC);
            System.out.println("fixing");
        } else {
            searchForClasses(f);
        }
    }
}

And this is what I use to try and get the parent file:

        File directory = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().getPath());
    main.searchForClasses(directory);

Does anyone have any ideas on what I can switch?

JNorr44
  • 276
  • 1
  • 2
  • 11
  • Please post full stacktrace. is Main.class loaded from 'jar file' on disk? Note that your solution would fail in some cases : http://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file – Jayan Jan 27 '13 at 01:19
  • Title is unclear. Do you mean 'files in the parent *directory*'? And what's the difference between 'of' and 'within'? – user207421 Jan 27 '13 at 01:19
  • @Jayan Main.class is the class holding this method. and EJP, Files beneath the parent directory. There is no different between of and within. – JNorr44 Jan 27 '13 at 01:22
  • So why are you asking for both? What is the meaning of the part of your title within parentheses? – user207421 Jan 27 '13 at 03:47
  • What do you mean? I am trying to find every file inside the parent file and then the files in those files and so on. I am trying to get to class files, but I only have a file filled with jars. I want to run through all of those class files. – JNorr44 Jan 27 '13 at 04:33

1 Answers1

0

File.listFiles() can return null. See the Javadoc. You aren't checking for that.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Yes, but I thought it only returned null if it wasn't a directory... the parent folder would always be a directory. – JNorr44 Jan 27 '13 at 01:23
  • Won't let me edit my last post... Anyways, I added in a null check and it is still throwing an npe, I guess directory is null? – JNorr44 Jan 27 '13 at 01:30
  • @JNorr44 You added a null check where? And how? And where in your code does it guarantee you're only calling listFiles() on a directory? – user207421 Jan 27 '13 at 03:21
  • The problem is that I want to get class files, not just files from directories. And I added a nullcheck before the for loop on dir. – JNorr44 Jan 27 '13 at 04:32
  • @JNorr44 I will say it one more time. The result of listFiles() can be null, and until you put in code to check for that, you will continue to get these NPEs. I don't see what class files have to do with this problem. – user207421 Jan 28 '13 at 00:00
  • I added a null check... meaning it no longer can run if null... meaning it should work if everything inside the null check was correct. – JNorr44 Feb 01 '13 at 03:41