1

I'm working on a java application which is supposed to load in images from the same directory that the .jar file will be in. The code below is what I currently have, and it works fine in Windows (in the workspace I'm using and in the .jar file's directory, wherever I put it). However, when I try to run the .jar file in OS X, it doesn't work. I get a null pointer exception. Is there something that I'm missing? or some formatting thing I'm not aware of?

String dir = System.getProperty("user.dir");

File folder = new File(dir+"/");
File[] listOfFiles = folder.listFiles();

for (int i = 0; i < listOfFiles.length; i++) {
        if (listOfFiles[i].isFile()) {
            String name = listOfFiles[i].getName();
            String fileType = name.substring(name.length()-3, name.length());
            if (fileType.equals("jpg")){
                File file = new File(dir+"/"+name);
                listMPs.add(new MusicPanel(file));
                base.add(listMPs.get(count));
                base.add(listMPs.get(count).switchLabel);
                if(count==0){
                    base.add(listMPs.get(0).firstSwitchLabel);
                }
                assignIndexes();
                assignMLs();
                count++;
            }
        } 
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
thanksd
  • 54,176
  • 22
  • 157
  • 150
  • It seems surprising but I'm not sure if UX system has a file extention with 3 symbols or any other. – Roman C Mar 01 '14 at 23:25

2 Answers2

2

Is there something that I'm missing?

Perhaps you missed that applications should not be storing loose files in the program installation directory. In fact, Sun/Oracle has gone to extreme lengths with applets and JWS launched apps. to ensure that even trusted ones cannot discover that location. Put the files in a more accessible place. A common place is a sub-directory of user.home.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Ok I'll try that. The problem is I don't have a Mac and so testing it on other friends' computers is slow going. Why would there be a difference between the app being able to find the program installation directory on a PC than on a Mac though? – thanksd Mar 01 '14 at 23:54
  • *"Why would there be a difference between the app being able to find the program installation directory on a PC than on a Mac though?"* The most likely cause is a difference in the JRE versions. Older Java versions would allow/assist operations on files in the installation directory. – Andrew Thompson Mar 01 '14 at 23:57
  • So, I'd been using user.dir instead of user.home because I'm not sure how to locate the files I need using user.home. Is there a different way to find the current directory of the application? – thanksd Mar 02 '14 at 00:11
1

You should be using File.separator instead of manually supplying /s in your paths. Java will format the paths according to the current OS implementation.

indivisible
  • 4,892
  • 4
  • 31
  • 50
  • That sounds like what is wrong. Thanks, I'll try that soon and get back to you – thanksd Mar 01 '14 at 23:20
  • *"That sounds like what is wrong."* Not to me it doesn't. If it was going to fail anywhere due to separators, it would fail on Windows, which uses \ as opposed to OS X and Unix/Linux which all use /. – Andrew Thompson Mar 01 '14 at 23:24
  • I gave your answer an upvote as it's more likely correct. I'll leave mine there anyway as it is something the OP should be doing regardless. – indivisible Mar 01 '14 at 23:25
  • *"..something the OP should be doing regardless"* Agree there. – Andrew Thompson Mar 01 '14 at 23:27
  • hmm, AndrewThompson it's working on Windows with the code as is. So I'm not sure what you mean. But yeah, @mbs that wasn't what was wrong, I'm still getting a null-pointer exception – thanksd Mar 01 '14 at 23:57