classloader

How do I access the content of folders inside a jar file?

自古美人都是妖i 提交于 2019-12-05 04:03:19
问题 I need to take a look at the names of files inside a specific package. Currently, I'm doing the following: ClassLoader loader = Thread.currentThread().getContextClassLoader(); URL packageUrl = loader.getResource(getPackage().replace('.', '/')); File packageDir = new File(packageUrl.getFile()); // Work with the files inside the directory This actually works just fine in Eclipse, since it doesn't package my application by default. When I run it from a jar file, however, I get a

How can I use different JARs for compiling and testing in maven?

有些话、适合烂在心里 提交于 2019-12-05 03:56:31
I compile my programm against javaee-api. But for Junit testing I must use a specific implementation like glassfish's javaee.jar to avoid errors like java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/persistence/Persistence (see also 1 ). So avoid using methods, that are only available in glassfish implementation, I want to compile my artifact with the general api, but run junit with the implementation jar. But both provide equal named classes and inferfaces, so the class loader gets in trouble. What is the best way to solve this

Use Absolute path for ClassLoader getResourceAsStream()

删除回忆录丶 提交于 2019-12-05 03:25:53
I am trying to use ClassLoader getResourceAsStream() My Direcory structure is like below: Project1 -src -main -java -webapp -WEB-INF -MYLOC -someprops.properties For classloader.getResourceAsStream("MYLOC/someprops.properties") works fine. But now I have to move the properties file outside of the .war, like in C:\someprops.properties But, classloader.getResourceAsStream("C:\someprops.properties") does not work. Can it not use an absolute path? If you have a native file path then you don't need to use getResourceAsStream , just create a FileInputStream in the normal way. Properties props = new

ClassLoad an Enum type

主宰稳场 提交于 2019-12-05 02:27:47
How would one go about instantiating an Enum type via a ClassLoader or similar mechanism? (I'm trying to keep everything under the same context classloader for a standalone server application). I have something like: ClassLoader loader = new CustomClassLoader(parent, libDir); Thread.currentThread().setContextClassLoader(loader); // trouble area Class<?> containerClass = loader.loadClass("com.somepackage.app.Name$SERVER"); I had wrongly thought simply loading the Enum would be enough to kick it off (it's private constructor contains startup method calls and what-not). Doing what I have above

How to check whether a class is initialized?

怎甘沉沦 提交于 2019-12-05 02:06:44
You'll probably ask, why would I want to do that - it's because I'm using a class (from an external library) which does stuff in its static initializer and I need to know whether it's been done or not. I looked at ClassLoader , but didn't find anything that looked useful. Any ideas? Colin Hebert You can use the ClassLoader.findLoadedClass() method. If it returns null, then the class isn't loaded. This way you don't load the class if it wasn't already loaded. WARNING : This code doesn't really work here, in the system ClassLoader, findLoadedClass() is protected, you need to override it with

How Classloader determines which classes it can load?

早过忘川 提交于 2019-12-05 02:00:23
问题 I'm reading on class loading in Java. Motivation Assuming we have a classloader hierarchy that looks like this, I understand that classes loaded by First are not directly accessible by classes loaded by Second (and vice versa). Bootstrap | System | Common / \ First Second I also understand that a classloader checks with its parent class loader whether it can load the class and, if that is the case, delegates the loading to its parent. Question How do classloaders actually determine whether

Java/JSF/Tomcat/Spring - Proxy-Object has different methods than original object

前提是你 提交于 2019-12-05 01:58:08
today I ran into this problem which really bugs me, as almost the code already worked (and stopped working even after reverting to the older version). I'm accessing a Spring-Bean on a Facelets-Page. Spring wraps these objects in Proxies to use aspects and some other stuff. The problem is, that I get an exception when trying to access the property of a bean. The exception is something like this: javax.el.PropertyNotFoundException: /customers.xhtml @23,27 value="#{customerBean.customer}": Property 'customer' not found on type $Proxy88 I know for sure (!!) that the according getter/setter methods

ANT - Could not load a dependent class com/jcraft/jsch/Logger

感情迁移 提交于 2019-12-05 01:30:38
I have a problem with my Ant script. I need to copy a file to Linux server <copy file="../Ant/lib/jsch-0.1.50.jar" tofile="${ant.home}/lib/jsch-0.1.50.jar" /> <scp todir="${server.user}:${server.password}@${server.dev}:${server.dev.dir.config}" trust="true" verbose="true"> <fileset dir="${src.home}/Config/"> <include name="**/*" /> </fileset> </scp> File is copied correctly, but I receive this error: BUILD FAILED C:\dev.xml:179: Problem: failed to create task or type scp Cause: Could not load a dependent class com/jcraft/jsch/Logger It is not enough to have Ant's optional JARs you need the JAR

Finding running instances in a running JVM

拈花ヽ惹草 提交于 2019-12-05 00:50:28
问题 I'm wondering if it's possible to get a handle on running instances of a given class. The particular issue that triggered this was an application that doesn't exit nicely because of a number of running threads. Yes, I know you can daemonize the theads, and they won't then hold up the application exit. But it did get me to wondering if this was possible. The closest thing I can is the classloaders (protected!) findLoadedClass, although you'd have to run through your own classloader to do this.

Is Java class loader guaranteed to not load classes that aren't used?

断了今生、忘了曾经 提交于 2019-12-05 00:21:05
Is there a guarantee that (the default, system) Java class loader doesn't attempt to load classes that aren't referred to in the code being run? A couple of examples of what I mean: I'm using a framework.jar which I know to contain references to another library.jar 's classes in it, but I'm using only such part of the framework that doesn't contain those references. Is it safe to leave library.jar out? Static blocks are run when a class is first loaded. If no running code contains references to a specific class, is it sure that it's static block is not run? Quickly testing it seems to work as