Java / Mvn Local Resource not found in .jar Package - java

I'm tying to add a local Resource (A simple JSON File) to my Maven Project. But when I execute the code, I get the Error:
java.io.FileNotFoundException: ./resources/test.json (No such file or directory)
This is the Structure of my Project:
-Router
--src
---main
----java
----resources
-----test.json
--pom.xml
In my pom file I add the json:
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>*.json</include>
</includes>
</resource>
</resources>
it seems it can't find the resource because in the code when instead of the path I look for
this.getClass().getResources("test.json")
it returns null. Any suggestions how I can resolve this issue?

From Class, the path is relative to the package of the class unless you include a leading slash, so if you don't want to use the current package, include a slash like this:
this.getClass().getResource("/test.json")

Related

How can I get the resource directory in maven to work?

I have a Java Maven web app project that I'm trying to clean up. In the <build> section of my pom.xml, I have the following:
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>
<filters>
<filter>profiles/${build.profile.id}.profile.properties</filter>
</filters>
[...other properties...]
</build>
In my project, which on my Mac is /Users/anthony/workspace/my-project/, i src/main/resources/profiles/I have local.profile.properties and qa.profile.properties.
Then, in my maven profiles in my pom, I define ${build.profile.id} as the following:
<profile>
<id>local</id>
[...stuff...]
<properties>
<build.profile.id>local</build.profile.id> <!-- or qa -->
[...stuff...]
</properties>
</properties>
When I am in my console and run $ mvn clean install -Plocal, I get the following error:
Failed to execute goal ... on project my-project: Error loading property file '/Users/anthony/workspace/my-project/profiles/local.profile.properties'.
It seems like Maven is not recognizing the resource directory for my filtering profile properties file. This only works when I explicitly put the entire path of my properties file, like so:
<filters>
<filter>src/main/resources/profiles/${build.profile.id}.profile.properties</filter>
</filters>
Is there a way for me to not have to explicitly state src/main/resources? I thought that the point of me declaring a resources directory was that I could use it, especially for declaring filtering.
The resource directory only has a meaning as "resources" for the Java artifact being built, but not for Maven itself. For Maven, the "resources" directory has a special meaning in the sense that Maven knows where to copy those files to in the resulting jar-file. But for Maven working with files or filtering files, you have to tell Maven the exact path, as Maven does not know if the filtered file is a resource, a source file, or something else. Also, you could have multiple source or resource directory defined, and Maven would not know, in which one to filter. Thus you always need to specify the full path for Maven.
So, in short:
Is there a way for me to not have to explicitly state src/main/resources?
No.

intellij is copying src to target

I imported the following project into intellij idea: https://github.com/brendano/ark-tweet-nlp/
I managed to get a working jar built, but for some reason it's copying all the source files from /src/ into /target/, so the resulting jar also includes all the source files. How do I prevent this? If it helps I've included a screenshot of the directories below:
Open the Settings window CTRL + ALT + S
Click on the Compiler option. You should see a list of Resource Patterns which determines which types of file IntelliJ will add to the target folder.
Add !*.java; to the list of resource patterns to exclude .java source files from being added to your target directory.
If this doesn't work, you can try adding a <build> tag to your Maven POM containing the following:
<build>
...
<resources>
<resource>
<directory>src/com</directory>
<targetPath>com</targetPath>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
...
</build>

File not found exception, only when including the code as a maven dependency

I have written this project which has a bunch of config files in config/ folder, which are necessary for running:
https://github.com/danyaljj/jwnl-prime/
The code is working fine under mvn test in my computer and CI.
But after packaging it and adding it as a maven dependency to another project, the function calls give me error
java.io.FileNotFoundException: config/file_properties.xml (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at net.didion.jwnl.TestDefaults.getInputStream(TestDefaults.java:63)
at net.didion.jwnl.JWNL.initialize(JWNL.java:92)
Any idea why I am getting this weird behavior?
Update:
after extracting the jar file, it turns out that the config files are not included in the jar:
Update2: After adding the suggestion by ? the config files get included in the jar file:
But still getting the same error: config/file_properties.xml (No such file or directory)
If you have project structure other than standard format you need to update pom.xml as well. If you need to include the config for build you need to add that in build section like
Refer link enter link description here
<build>
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ssh</artifactId>
<version>2.4</version>
</extension>
</extensions>
<resources>
<resource>
<directory>config</directory>
<includes>
<include>**/**</include>
</includes>
</resource>
</resources>
</build>
First, verify that the configuration files are actually being packaged into the jar. I would view it in an archive viewer or unpack it and see what's in it.
Secondly, you can't directly load a file from a jar using standard file opening methods. You'll have to use something like
Class.getResourceAsStream()
See here for some more suggestions:
How do I access a config file inside the jar?

Cannot load native libOpenCL.so library from Maven resource

I'm getting unsatisfied link errors when trying to use JOCL. The error is a typical unsatisfied link error:
Stack trace from the attempt to load the library as a resource:
java.lang.UnsatisfiedLinkError: Could not initialize native library. Implementation library 'libOpenCL.so' could not be loaded
It then goes on to say that:
Could not load libOpenCL.so, error libOpenCL.so: cannot open shared object file: No such file or directory
I tried adding it as a resource to Maven via:
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<resources>
<resource>
<filtering>false</filtering>
<directory>/opt/AMDAPPSDK-3.0-0-Beta/lib/x86_64</directory>
<includes>
<include>libOpenCL.so</include>
</includes>
</resource>
</resources>
...
As disclaimer, libOpenCL.so is a soft link to -> libOpenCL.so.1, I don't know if that'll mess things up. Anyway, I checked the file name and path several times, the file is there. How can I add this lib to my class path?

Maven resources/ in sibling modules

I have a issue with some tests in sibling modules.
I have the following folder in module 1 in resources/: sql/statements
all the files in that folder is loaded with the following code:
URL url = this.getClass().getClassLoader().getResource("sql/statements");
assert url != null;
Collection<File> files = FileUtils.listFiles(new File(url.getPath()),
FileFilterUtils.suffixFileFilter("sql.xml"), TrueFileFilter.INSTANCE);
This works fine:
inside intellij for the sibling module
inside intellij in the module with the xml files
with maven clean install in the module with the xml files
but for some reason when I reference this class in a test in the sibling module it is unable to find the xml files when I run clean install from command line.
how can I solve this?
edit: when it fails it print the follwing url path, which seem ok (?):
url: jar:file:/home/<user>/.m2/repository/<path>/1.0.0/<jar-name>1.0.0.jar!/sql/statements
Check your POM files : you are probably only copying the resources in the build of your "sql/xml" module. Better move it up to the parent POM if you want to use it in sibling modules.
As for this maven doc, test resources should be stored in
src/test/resources
But you can set parameter testResources to the same directory as main resources
<build>
<testResources>
<testResource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</testResource>
<testResources>
<resources> <!-- this is default setting -->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>

Categories

Resources