Discussion:
Puzzled by how the class loader works.
'Paul Z. Wu' zwu_net-/E1597aS9LQAvxtiuMwx3w@public.gmane.org [seajug]
2014-05-30 17:09:21 UTC
Permalink
I have  a class below for a lib (jar) I'm creating.  This library will be used in both a standalone program and a web app.

Look at the italic line.  It is fine when deployed in the web app, but it will get a java.lang.ExceptionInInitializerError when use it for the standalone program.  If I replace the line with

config.load(Class.class.getResourceAsStream("/config.properties"));

The reversed thing happens: the web app will get  java.lang.ExceptionInInitializerError, but the standalone will be fine.   How can I load the resource that works for the both cases?



public class UsidBits {

    final static Properties config = new Properties();

    static {

        try {
           config.load(UsidBits.class.getClassLoader().getResourceAsStream("/config.properties"));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
   ......
}

 
Paul Z. Wu
 
http://www.elookinto.com
Konstantin Ignatyev kgignatyev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org [seajug]
2014-05-30 17:20:40 UTC
Permalink
To my recollection it should work with classloader without forward slash,
provided that resource is at the root


UsidBits.class.getClassLoader().getResourceAsStream("config.properties")
I have a class below for a lib (jar) I'm creating. This library will be
used in both a standalone program and a web app.
Look at the italic line. It is fine when deployed in the web app, but it
will get a java.lang.ExceptionInInitializerError when use it for the
standalone program. If I replace the line with
config.load(Class.class.getResourceAsStream("/config.properties"));
The reversed thing happens: the web app will get
java.lang.ExceptionInInitializerError, but the standalone will be fine.
How can I load the resource that works for the both cases?
public class UsidBits {
final static Properties config = new Properties();
static {
try {
config.load(UsidBits.class.getClassLoader().getResourceAsStream("/config.properties"));
} catch (IOException e) {
e.printStackTrace();
}
}
......
}
Paul Z. Wu
http://www.elookinto.com
--
Konstantin Ignatyev

PS: If this is a typical day on planet Earth, humans will add fifteen
million tons of carbon to the atmosphere, destroy 115 square miles of
tropical rainforest, create seventy-two miles of desert, eliminate between
forty to one hundred species, erode seventy-one million tons of topsoil,
add 2,700 tons of CFCs to the stratosphere, and increase their population
by 263,000

Bowers, C.A. The Culture of Denial: Why the Environmental Movement Needs a
Strategy for Reforming Universities and Public Schools. New York: State
University of New York Press, 1997: (4) (5) (p.206)
'Paul Z. Wu' zwu_net-/E1597aS9LQAvxtiuMwx3w@public.gmane.org [seajug]
2014-05-30 17:50:20 UTC
Permalink
OK. This works well.  Thanks a lot,

 
 
Paul Z. Wu
 
http://www.elookinto.com


________________________________
From: "Konstantin Ignatyev kgignatyev-***@public.gmane.org [seajug]" <***@yahoogroups.com>
To: seajug-***@public.gmane.org
Sent: Friday, May 30, 2014 10:20 AM
Subject: Re: [seajug] Puzzled by how the class loader works.



 
To my recollection it should work with classloader without forward slash, provided that resource is at the root

UsidBits.class.getClassLoader().getResourceAsStream("config.properties")
 
I have  a class below for a lib (jar) I'm creating.  This library will be used in both a standalone program and a web app.
Look at the italic line.  It is fine when deployed in the web app, but it will get a java.lang.ExceptionInInitializerError when use it for the standalone program.  If I replace the line with
config.load(Class.class.getResourceAsStream("/config.properties"));
The reversed thing happens: the web app will get  java.lang.ExceptionInInitializerError,
but the standalone will be fine.   How can I load the resource that works for the both cases?
public class UsidBits {
    final static Properties config = new Properties();
    static {
        try {
           config.load(UsidBits.class.getClassLoader().getResourceAsStream("/config.properties"));
        } catch (IOException e) {
           
e.printStackTrace();
        }
    }
   ......
}
 
Paul Z. Wu
 
http://www.elookinto.com
--
Konstantin Ignatyev
 
PS: If this is a typical day on planet Earth, humans will add fifteen million tons of carbon to the atmosphere, destroy 115 square miles of tropical rainforest, create seventy-two miles of desert, eliminate between forty to one hundred species, erode seventy-one million tons of topsoil, add 2,700 tons of CFCs to the stratosphere, and increase their population by 263,000
 
Bowers, C.A. The Culture of Denial: Why the Environmental Movement Needs a Strategy for Reforming Universities and Public Schools. New York: State University of New York Press, 1997: (4) (5) (p.206)
Eric Jain eric.jain-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org [seajug]
2014-05-30 17:20:45 UTC
Permalink
Post by 'Paul Z. Wu' zwu_net-/***@public.gmane.org [seajug]
config.load(Class.class.getResourceAsStream("/config.properties"));
config.load(UsidBits.class.getClassLoader().getResourceAsStream("/config.properties"));
http://stackoverflow.com/questions/3238562/getresourceasstream-fails-under-new-environment
--
Eric Jain
Got data? Get answers at zenobase.com.


------------------------------------
Posted by: Eric Jain <eric.jain-***@public.gmane.org>
------------------------------------

Yahoo Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/seajug/

<*> Your email settings:
Individual Email | Traditional

<*> To change settings online go to:
http://groups.yahoo.com/group/seajug/join
(Yahoo! ID required)

<*> To change settings via email:
seajug-digest-***@public.gmane.org
seajug-fullfeatured-***@public.gmane.org

<*> To unsubscribe from this group, send an email to:
seajug-unsubscribe-***@public.gmane.org

<*> Your use of Yahoo Groups is subject to:
https://info.yahoo.com/legal/us/yahoo/utos/terms/
Loading...