Skaros Ilias
Skaros Ilias

Reputation: 1068

Eclipse resources folder with git & Maven

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.

enter image description here

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.
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.

EDIT:
After Kraal suggestions I changed my project and now it looks like this: enter image description here
But i am still getting NullPointerException on

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

Upvotes: 2

Views: 1556

Answers (2)

Stefan
Stefan

Reputation: 12453

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 here 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());

Update

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>

Upvotes: 0

Kraal
Kraal

Reputation: 2867

This is not related at all to git but to your project's structure. Indeed it does not follow the Maven standard directory layout.

  • Your java classes should be in src/main/java
  • Your resource files should be in src/main/resources

So you have two choices:

  • Correct your setup then retry.
  • Configure Maven in your pom in order to look in lib / lib2 for resources.

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 how to use Maven within Eclipse. When you plan to work on a Maven project, create a Maven project in Eclipse, not a Java project.

Upvotes: 1

Related Questions