java


$Class.getResource failed from fat jar packed by maven shade plugin


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

Related Links

How to use JFoenix RequiredFieldValidator to enable/disable form JFXButton?
Insert Multiple Rows realm android
Actions class in selenium 3.3.1 deprecated looking for solution with contextClick
Parsing array of objects into a function
Moverio bt-350 bluetooth
Run *.jar file on Android
How to make graphics move without showing tracing but keep it when I draw another one?
Calling a method with a return type
Saving a current open file using JFileChooser
What is most optimal way to truncate a string to limit its length?
check string is strictly alphanumeric
PHP exec (“javac file.java >> log.txt”) does not write compile errors to file
Install x-pack java.net.UnknownHostException: artifacts.elastic.co
Why is tomcat debug war different than manually created and copied war?
How to handle a web element which is reported as “NOT visible” by Selenium when clicked on, in a login page
How do I access methods within the objects I put in an array?

Categories

HOME
bluetooth
syntax
fme
spring-jdbc
alpha
mouse
python-unittest
nano-server
user-input
ibm-odm
fatal-error
ef-migrations
aurigma
normalizr
alpine
textfield
ml
graphicsmagick
icloud-api
android-widget
phpfox
msys2
dbclient
facebook-apps
madlib
nouislider
socialengine
javascriptcore
binary-data
overlap
retina-display
reportbuilder
splice
temporary-files
gulp-sourcemaps
reportingservices-2005
estimote
mime
unixodbc
total-commander
sencha-touch-2.3
cubic-spline
drupal-6
knockout-components
magma
windows-mobile-6.5
knpmenubundle
flutterwave
dwscript
root-framework
watchconnectivity
removechild
httplistener
infix-notation
android-textview
ctest
contact-list
lua-5.1
prettytensor
ubuntu-10.04
php-internals
ptrace
jfugue
vhd
icu4j
graph-drawing
processmodel
pretty-print
kineticjs
android-imagebutton
codeigniter-routing
flask-cors
mysql-error-1062
poller
sitemesh
ril
jplaton
mhtml
hamiltonian-cycle
broken-links
venn-diagram
appconkit
online-compilation
qtkit
removeclass
pydot
deobfuscation
hirefire
psi
appender
table-footer
android-sdk-2.1
h.323
castle-monorail
nintendo-ds
boost-smart-ptr
private-members
lzh
ntvdm.exe

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App