1

I'm building a Groovy plugin for Android Studio which will execute some gradle tasks. However I need to load a XML file from within the JAR file from where the code executes.

The JAR file is as following:

  • com/packagename/code
  • META-INF/gradle-plugins
  • filename.xml

Because the xml file is at the root of the JAR I used this to load in the resource into an inputstream

new InputStreamReader(CheckStyleTask.class.getResourceAsStream("/filename.xml"))

The weird thing is that it only works sometimes. Sometimes it returns the file, sometimes it returns null. For example I took this line to check the path:

        println(CheckStyleTask.class.getResource("/filename.xml").getPath())

And the path is completely right. So it is able to find the file with getResource, but with getResourceAsStream it returns null.

I think something is wrong with the building of the jar but in IntelliJ I set the gradle task as 'jar' which did create a working jar with working xml file.

Am I doing anything wrong regarding the building or is there anything wrong with the xml file?

Marc
  • 1,094
  • 3
  • 17
  • 38

1 Answers1

0

There is a difference between the two functions: https://stackoverflow.com/a/20069798/5387592

You should precise: in which circumstances, the results differ : after create new jar, or after launching again.

1 check if the file is really embedded in the jar: change .jar to .zip and check inside

2 check the real path. In Eclipse for example, you put in a folder, but this folder doesn't appear in resulting jar

3 use advices: take absolute path as you do

Community
  • 1
  • 1