0

I have a problem with building an executable jar using maven it create a jar but I can't open it because it doesn't find my file, and I don't know what to do anymore I am using this for loading a file, if I add an absolute path to file it works great

ClassLoader classLoader = SelectionLab.class.getClassLoader();
File file = new File(classLoader.getResource("Opstine.shp").getFile());
if (file == null) {
        return;
}

and here is part of my pom.xml

    <build>  
     <plugins>  
       <plugin>  
         <artifactId>maven-assembly-plugin</artifactId>  
         <configuration>  
           <archive>  
             <manifest>  
               <mainClass>geotools.SelectionLab</mainClass>  
             </manifest>  
           </archive>  
           <descriptorRefs>  
             <descriptorRef>jar-with-dependencies</descriptorRef>  
           </descriptorRefs>  
         </configuration>  
         <executions>  
           <execution>  
             <id>make-assembly</id>  
             <phase>package</phase>  
             <goals>  
               <goal>single</goal>  
             </goals>  
           </execution>  
         </executions>  
       </plugin>  
       <plugin>  
         <groupId>org.apache.maven.plugins</groupId>  
         <artifactId>maven-compiler-plugin</artifactId>  
         <inherited>true</inherited>  
         <configuration>  
           <source>1.8</source>  
           <target>1.8</target>  
         </configuration>  
       </plugin>    
     </plugins>  
   </build>  

image

I tried to get absolute path as messagepane and after exporting shapefile I get this enter image description here but I am not sure is it searching for file in jar on path F:\workspace\osma_grupa\osma_grupa\target\osma_grupa-0.0.1-SNAPSHOT-jar-with-dependencies.jar or it use also this part before file:. I just checked and there is file in root of jar

Zoran Kokeza
  • 165
  • 12
  • Can you create a new file File file = new File(); and print its absolute path , That way it would be easier for you to debug and place the file accordingly. – sathya Jun 21 '16 at 07:03
  • I am not sure what I get with absolute path so I posted image of what i get – Zoran Kokeza Jun 21 '16 at 07:39
  • If you are executing from eclipse I probably assume you havent changed your resource path . If you place your file in eclipse installation folder it might work. Also I was asking you to create a dummy file and get its address that way , you can decode where the system is looking for the file. before our classloader implementation – sathya Jun 21 '16 at 08:01
  • `class.getResourceAsStream("...")` is the way to go with resources... – khmarbaise Jun 21 '16 at 09:33
  • @khmarbaise I consider that option for getting file but I couldn't figure out how to get a file from stream reader, to use displayShapefile(file); and display that shapefile – Zoran Kokeza Jun 21 '16 at 13:25

1 Answers1

0

When you use SelectionLab.class.getClassLoader().getResource("Opstine.shp").getFile() it considers the location of the "Opstine.shp" is the root folder. You need to add src/main/resources in your path.


EDIT:

Could you try changing the pom snippet above to this to see if anything changes:

<build>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <mainClass>geotools.SelectionLab</mainClass>
                </manifest>
            </archive>
        </configuration>
    </plugin>
    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
    </plugin>
</plugins>

ma3stro
  • 313
  • 2
  • 11
  • I already tried that and got NullPointerException – Zoran Kokeza Jun 21 '16 at 07:38
  • Interesting. Have you tried using relative path? `SelectionLab.class.getResource("Opstine.shp")` – ma3stro Jun 21 '16 at 07:45
  • File file = new File(SelectionLab.class.getResource("Opstine.shp").getFile()); gives NullPointerException File file = new File(SelectionLab.class.getResource("/Opstine.shp").getFile()); no NullPointerException but it's the same again, doesn't open file after export – Zoran Kokeza Jun 21 '16 at 07:56