简体   繁体   中英

Gradle and src/main/resources

I have a gradle project which is working fine within eclipse. In the java code I have

Properties p = MyClass.class.getClassLoader.getResourceAsStream("myProps.properties");

File f = new File(p.getProperty("myFile"));

My structure is:

src/main/java - has java files
src/main/resources - has myProps.properties and myFile.txt

myProps.properties has:

myFile=src/main/resources/myFile.txt

If I do a gradle build (using a default shadowJar task) my resources have now been moved from src/main/resources to the root of the jar file.

shadowJar {
   manifest{
     // The only thing I modify is the attributes
   }
}

When I execute the jar by running
"java -jar myJar.jar" I get FileNotFound exception referring to the myFile.txt resource file.

What is the correct way to fix this issue so that the code works from both the Eclipse and executable jar environments?

I needed to read the file as a stream (the same way I read in the properties file). I removed 'src/main/resources' from the path in the properties file and it worked

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM