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 prevent two objects in collisions from crossing both
making a playable jslider in java
Adding PdfDiv to Paragraph
Spring MVC - No mapping found for HTTP request with URI [/webstore/] in DispatcherServlet with name 'DefaultServlet'
How do I dump messageQueue in a Handler?
exploded-aar breakpoints show no executable code found at line x
Programmatically Fetching a Lazy JPA Field
Getting an FXML window to open when running a java file
Spring 3 MVC Validation is not working
Java Bitwise Or Between Byte and Int
ProgressBar in AsyncTask crashes
Push Notification for iOS device
How to use the data stored in .txt when reading it
I can't select the value in Jcombo box
How to know which plugin is adding the “run” task in gradle
is there a way to open a LibGDX project without the SDK?

Categories

HOME
vim
cookies
mediawiki
fme
cplex
rsyslog
jsp-tags
cvs
malloc
google-project-tango
installshield
django-imagekit
podio
qore
primary-key
dacpac
maude-system
communication
volttron
google-cloud-spanner
circuit
designer
django-simple-history
reactive-cocoa
sms-gateway
visual-composer
one-hot-encoding
abi
tokenize
errorlevel
facebook-instant-articles
lcd
trading
sqlcipher
django-storage
maxmind
replaceall
log4js-node
http-status-code-503
oracle-fusion-middleware
http-get
windows-dev-center
uiswipegesturerecognizer
android-kernel
geopositioning
ghost4j
catalog
theano.scan
jna
hue
ensembles
pdf-reactor
ibpy
estimote
eclipse-gef
youcompleteme
strptime
atomicity
zip4j
dds
hendrix
lowpass-filter
sybase-asa
boost-preprocessor
pdfclown
redundancy
visual-c++-2008
android-fonts
spring-android
android-textview
bonobo
file-writing
jwplayer7
master-slave
python-stackless
fadeout
apachebench
suffix-tree
pagerank
ipconfig
ford-fulkerson
tcpserver
xna-4.0
eclipse-clp
angular-leaflet-directive
map-projections
processmodel
sortedlist
iiviewdeckcontroller
oberon
nsmutabledictionary
fscommand
operator-precedence
pick
cdt
winddk
nsbutton
socketexception
preferences
valueconverter
datagridviewcolumn
app42
commoncrypto
google-cloud-save
android-screen-support
eventlistener
enterprisedb
android-hardware
mhtml
sublist
runas
dbproviderfactories
ocx
whoosh
appendto
.nettiers
forums
ext3
rescale
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