0

My java/scala program requires support from a javescript code snippet. So I put it in my resource folder:

resources
|- sizzle.js

And in my pom.xml to explicitly include it in the fat/uber jar compiled by maven shade plugin:

                    <transformers>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
                            <resource>resources</resource>
                            <file>sizzle.js</file>
                        </transformer>
                    </transformers>

(I know this is unnecessary, but either way it will fail me)

When I try to read the javascript snippet from my source code:

sizzleSource = IOUtils.toString(Thread.currentThread().getContextClassLoader().getResource("sizzle.js"));

it works perfectly when running in IDE, but I get null pointer exception when executing the fat jar:

...
java.lang.NullPointerException
at org.apache.commons.io.IOUtils.toString(IOUtils.java:894)
at org.apache.commons.io.IOUtils.toString(IOUtils.java:879)
...

So why maven shade plugin fail in this case? I'm able to see sizzle.js in the root dir of the fat jar, but it doesn't make a difference.

1
  • SomeFatJarClass.getResource("/sizzle.js") uses Class.getResource, with a relative, or here explicit absolute path. Should that work, it is a ClassLoader problem.
    – Joop Eggen
    Feb 20, 2015 at 21:25

1 Answer 1

1

The file sizzle.js ends up at the root of your final jar, so you need to access it via :

Thread.currentThread().getContextClassLoader().getResource("/sizzle.js"));

The method ClassLoader.getResource(name) needs an absolute path to look for a resource.

5
  • Thank you! But this will break the unit test when executed in IDE, as getResource("/sizzle.js") cannot get files in /source folder if not running in the fat jar. Is there a solution that can handle both cases?
    – tribbloid
    Feb 20, 2015 at 23:02
  • You should place sizzle.js under src/main/resources. This way, it will be automatically added to the uber jar (will end up at the root of the final jar so you won't have to manually add it) and it will not break your unit test (if you access this file with "/sizzle.js").
    – Tunaki
    Feb 20, 2015 at 23:05
  • that's exactly where it is! yet "sizzle.js" can be evaluated fine but "/sizzle.js" will get me java.lang.NullPointerException. My IDE of IntelliJ IDEA ultimate should handle resources folder, as both src and test resources folder are marked clearly and sizzle.js is in both
    – tribbloid
    Feb 20, 2015 at 23:15
  • Mmh, maybe this is a ClassLoader issue then. Try with the current class loader getClass().getClassLoader().
    – Tunaki
    Feb 20, 2015 at 23:26
  • The last solution works like magic! (Though I have to enclose it in non-static function but so far it doesn't cause any problem) The ContextClassLoader is indeed the culprit
    – tribbloid
    Feb 20, 2015 at 23:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.