0

It's been like 8-10 hours since I encounter that problem and it really bugs me. So hopefully someone would give me a hint what I'm doing wrong.

Issue I'm using maven shaded plugin to pack my maven app to jar file, the problem which I encounter is that in Eclipse when I'm running my app all is sound, but when I try to run my app as jar file:

java -jar <my-file>

I get:

Exception in thread 'main' java.lang.NullPointerException
at start.my.App.main<App.java:60>

My application works as filter and processor for files within directory from which my app is started. Traverse method simply returns list of files within directory in which my jar file is located:

@Override
public List<File> traverse(String directory) {
  File directory = new File(directory);
  List<File> resultList = new ArrayList<File>();
  File[] fList = directory.listFiles();
  resultList.addAll(Arrays.asList(fList));
  for (File file : fList) {
    if (file.isDirectory()) {
       resultList.addAll(traverse(file.getAbsolutePath()));
    }
  }

  return resultList;
 }

The line in which this problem occurs:

WalkerResult walkerResult =    
walker.traverse(ClassLoader.getSystemClassLoader()
                           .getResource(".")
                           .getPath());

What's inside traverse method should return path to my jar file which I'm running, I think I'm doing something wrong as when I pass as path to traverse method which looks like:

WalkerResult walkerResult =    
walker.traverse("C:\\Users\\Downloads");

all is fine in IDE and as in jar application.

Thanks for any hints in advance.

sh1nen
  • 199
  • 4
  • 18
  • suppose in your public `List traverse(String directory){ }` method you are passing`String directory` , but you are using `path` which is not defined inside the method. – Rajith Pemabandu Jul 16 '17 at 00:01
  • Evidently something is `null` on line 60. – Oliver Charlesworth Jul 16 '17 at 00:04
  • But wouldn't it be in both cases instead of only as jar application ? I know what null pointer is but I have some difficulties in figuring out what causes that exception in that specific case, as it's hard to find when it's running fine in IDE. – sh1nen Jul 16 '17 at 00:15
  • Resources (inside a jar on class path) do not mix with files and `.`. In your case `System.getProperty("user.dir")` might give the start directory. Mind that listFiles could return null too. – Joop Eggen Jul 16 '17 at 00:25

0 Answers0