81735

Eclipse resources folder with git & Maven

Question:

I have a git/Maven (if this has something to do with it) project on Eclipse. I need to add a library folder where some files that i need will be stored. This is the tree structure of my project.

<a href="https://i.stack.imgur.com/vSVlG.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/vSVlG.png" data-original="https://i.stack.imgur.com/vSVlG.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

I try to load the files under folders lib and lib2, but with no success. Some of my attempts were

System.out.println("PATH"+this.getClass().getResource("lib").getPath()); System.setProperty("org.hyperic.sigar.path",this.getClass().getResource("lib2/file.xxx"));

All of them gets me a NullPointerException.<br /> I know that there are a bunch of answers already out there, I think I tried them all, ok most, and this just doesn't work.

<strong>EDIT:</strong><br /> After Kraal suggestions I changed my project and now it looks like this: <a href="https://i.stack.imgur.com/qOp8H.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/qOp8H.png" data-original="https://i.stack.imgur.com/qOp8H.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a><br /> But i am still getting NullPointerException on

System.setProperty("org.hyperic.sigar.path",this.getClass().getResource("Sigar_lib/libsigar-amd64-linux.so").getPath());

Answer1:

This is not related at all to git but to your project's structure. Indeed it does not follow the <a href="https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html" rel="nofollow">Maven standard directory layout</a>.

<ul><li>Your java classes should be in src/main/java</li> <li>Your resource files should be in src/main/resources</li> </ul>

So you have two choices:

<ul><li>Correct your setup then retry.</li> <li>Configure Maven in your pom in order to look in lib / lib2 for resources.</li> </ul>

In both cases, you will then to look for resources at the right place. I.e. file.xxx will be located in file.xxx and not src/main/resources/file.xxx (once you define a resources directory, you can search for resources in this directory)

According to your comments, you should really read this post explaining <a href="http://www.vogella.com/tutorials/EclipseMaven/article.html" rel="nofollow">how to use Maven within Eclipse</a>. When you plan to work on a Maven project, create a Maven project in Eclipse, not a Java project.

Answer2:

The line:

System.setProperty("org.hyperic.sigar.path",this.getClass().getResource("Sigar_lib/libsigar-amd64-linux.so").getPath());

always fails, because when the Jar is packed, the directory "Sigar_lib" will be located at the root of the classpath, so change the path to:

"/Sigar_lib/libsigar-amd64-linux.so"

(leading slash).

But this will also fail, because the native libraries cannot be loaded from within a jar. Your options are, to put the files into an external directory or add them to the classpath of your jar and extract them to the temporary directory when loading.

The best way to integrate the libraries with Maven is to add them as a dependency, see <a href="http://mvnrepository.com/artifact/org.gridkit.lab/sigar-lib/1.6.4" rel="nofollow">here</a> and remove the "lib", "lib2" folders.

At runtime the files can be accessed like this:

System.out.println("PATH: " + System.class.getResource("/sigar-1.6.4/libsigar-amd64-linux.so").getPath());

<strong>Update</strong>

pom.xml should have these lines:

<!-- SIGAR --> <!-- Java library --> <dependency> <groupId>org.fusesource</groupId> <artifactId>sigar</artifactId> <version>1.6.4</version> </dependency> <!-- Native libraries --> <dependency> <groupId>org.gridkit.lab</groupId> <artifactId>sigar-lib</artifactId> <version>1.6.4</version> </dependency> </dependencies>

Recommend