0

I assembly my project to a jar with maven, and the there is a "data" folder contained a lot of .bat files inside the jar, the structure is: jar--- ---data(this folder contains 1.bat,2.bat...) ..... I used the method in How do I list the files inside a JAR file?, but when I run the jar in cmd, there still is the NullPointerException, I tried a lot and don't know where is the problem. my code is :

CodeSource src=ParcelLayerTree.class.getProtectionDomain().getCodeSource();
    List<String> list=new ArrayList<String>();
    if(src!=null){
        URL jar=src.getLocation();
        ZipInputStream zip=new ZipInputStream(jar.openStream());
        ZipEntry zipEntry=null;

        while((zipEntry=zip.getNextEntry())!=null){
            String entryName=zipEntry.getName();
            if(entryName.endsWith(".dat")){
                list.add(entryName);
            }
        }
    }
    if(list.size()<=0){
        throw new IOException("not found the dat file");
    }
    for (String filePath : list) {
        String fileName=fileNameSplit(filePath);
        InputStream inputStream=ParcelLayerTree.class.getClassLoader().getResourceAsStream(fileName);
        try (BufferedReader reader = new BufferedReader(new     InputStreamReader(inputStream))) {
            int label = Integer.parseInt(fileName.split("\\.")[0]);
Community
  • 1
  • 1
nmdxpc
  • 1
  • 4
  • 1
    Add your NullPointer Exception stack trace to your question. – Tom C Aug 15 '16 at 07:44
  • where does this come from? ParcelLayerTree.class.getProtectionDomain().getCodeSource(); - Is the error because ParcelLayerTree isnt instantiated – farrellmr Aug 15 '16 at 08:09
  • Thanks for all of you, it's my fault. I should use the filePath instead of fileName to get the file: InputStream inputStream=ParcelLayerTree.class.getClassLoader().getResourceAsStream(filePath); I'm sorry! Thanks. – nmdxpc Aug 15 '16 at 09:38

0 Answers0