classloader

Flow of class loading for a simple program

给你一囗甜甜゛ 提交于 2019-11-27 10:04:54
I am just now beginning to learn the internal architecture of Java. I have roughly understood the concept of class loading which loads the required classes when jvm runs, ClassNotFoundException is thrown when a class is not found and specific class loader loads the classes referenced by the class. Can someone please explain clearly the flow of class loading i.e. the sequence of bootstrap class loading and user-defined class loading in the sample Java code below. import java.io.File; public class Sample { public static void main(String[] args) { String fileName = "sample"; File file = new File

Java Class.forName() from distant directory

非 Y 不嫁゛ 提交于 2019-11-27 09:19:37
I am currently loading Java classes using Class.forName() to load it. clazz = Class.forName("interfaces.MyClass"); But now I want to load classes from different directory, I have tried to set classpath by clazz = Class.forName("-cp \"C:/dir\" distantinterfaces.DistantClass"); With no success and ClassNotFoundException . Full path to distant class is: C:/dir/distantinterfaces/DistantClass.class Use an URLClassLoader for this. The code might be something along the lines of: File f = new File("C:/dir"); URL[] cp = {f.toURI().toURL()}; URLClassLoader urlcl = new URLClassLoader(cp); Class clazz =

Java Dynamically Loading a class

我只是一个虾纸丫 提交于 2019-11-27 09:05:56
I am attempting to load classes dynamically into a component. I am using a file chooser to select the .JAR file that will be loaded and then a option pane to get the name of the class. I have trawled the internet looking for how to convert a java file to a URL in order to load it in URLClassLoader and I have come up with: File myFile = filechooser.getSelectedFile(); String className = JOptionPane.showInputDialog( this, "Class Name:", "Class Name", JOptionPane.QUESTION_MESSAGE); URL myUrl= null; try { myUrl = myFile.toURL(); } catch (MalformedURLException e) { } URLClassLoader loader = new

ClassCastException because of classloaders?

只谈情不闲聊 提交于 2019-11-27 08:57:38
While playing with classloaders i got the following exception: Exception in thread "main" java.lang.ClassCastException: xxx.Singleton cannot be cast to xxx.Singleton Does this mean that an instance from a classloader is not castable to an class of another classloader? Check my code where i'm able to instanciate 3 singletons thanks to classloaders, even with the "" security. public static void main(String[] args) throws Exception { URL basePath = new URL("file:/myMavenPath/target/classes/"); Object instance = getClassInstance(Singleton.class); System.out.println(instance); // Object instance2 =

Java类加载器之线程上下文类加载器(ContextClassLoader)

醉酒当歌 提交于 2019-11-27 06:55:02
Thread.setContextClassLoader(ClassLoader cl) 在Java中提供了对于线程设置ContextClassLoader的方法,关于上下文类加载器,下面摘抄的内容将的比较明白: 线程上下文类加载器(context class loader)是从 JDK 1.2 开始引入的。类 java.lang.Thread中的方法 getContextClassLoader()和 setContextClassLoader(ClassLoader cl)用来获取和设置线程的上下文类加载器。如果没有通过 setContextClassLoader(ClassLoader cl)方法进行设置的话,线程将继承其父线程的上下文类加载器。Java 应用运行的初始线程的上下文类加载器是系统类加载器。在线程中运行的代码可以通过此类加载器来加载类和资源。 前面提到的类加载器的代理模式并不能解决 Java 应用开发中会遇到的类加载器的全部问题。Java 提供了很多服务提供者接口(Service Provider Interface,SPI),允许第三方为这些接口提供实现。常见的 SPI 有 JDBC、JCE、JNDI、JAXP 和 JBI 等。这些 SPI 的接口由 Java 核心库来提供,如 JAXP 的 SPI 接口定义包含在 javax.xml.parsers包中。这些

Java verbose class loading

亡梦爱人 提交于 2019-11-27 06:52:29
I am trying to list the order in which the Java class loader is loading my classes. if I use -verbose parameter it will list every single interface/class it loads, including tons of interfaces such as Serializable, exceptions etc. Is there a way to tweak this output so it only shows which classes are loaded in the class my main method is defined? nwinkler I guess your best bet is to do the following: Output some fixed text once your main method starts and right before it ends. Pipe the verbose output into a file Use things like less or grep to find the classes loaded between the two tags from

Unable to open resources in directories which end with an exclamation mark (!)

落花浮王杯 提交于 2019-11-27 06:12:56
问题 The getResourceAsStream-method returns null whenever running the executable jar in a directory which ends with a exclamation mark. For the following example, I have a Eclipse project the following directory structure: src\ (Source Folder) main\ (Package) Main.java res\ (Source Folder) images\ Logo.png I'm reading the Logo.png as follows: public static void main(String[] args) throws IOException { try (InputStream is = Main.class.getClassLoader().getResourceAsStream("images/Logo.png")) { Image

Understanding Groovy/Grails classloader leak

雨燕双飞 提交于 2019-11-27 06:04:22
问题 Yesterday I deployed my first Grails (2.3.6) app to a dev server and began monitoring it. I just got an automated monitor stating that CPU was pinned on this machine, and so I SSHed into it. I ran top and discovered that it was my Java app's PID that was pinning the server. I also noticed memory was at 40%. After a few seconds, the CPU stopped pinning, went down to a normal level, and memory went back down into the ~20% range. Classic major GC. While it was collecting, I did a heap dump.

Is it possible to create an URL pointing to an in-memory object?

余生颓废 提交于 2019-11-27 06:03:18
问题 I'm trying to extend my library for integrating Swing and JPA by making JPA config as automatic (and portable) as can be done, and it means programmatically adding <class> elements. (I know it can be done via Hibernate's AnnotationConfiguration or EclipseLInk's ServerSession , but - portability). I'd also like to avoid using Spring just for this single purpose. I can create a persistence.xml on the fly, and fill it with <class> elements from specified packages (via the Reflections library).

What is a Java ClassLoader?

旧时模样 提交于 2019-11-27 05:44:15
In a few simple sentences, what is a Java ClassLoader, when is it used and why? OK, I read a wiki article. ClassLoader loads classes. OK. So if I include jar files and import, a ClassLoader does the job. Why should I bother with this ClassLoader? I've never used it and didn't know it existed. The question is, why does the ClassLoader class exist? And also, how do you use it in practice? (Cases exist, I know.) Taken from this nice tutorial from Sun: Motivation Applications written in statically compiled programming languages, such as C and C++, are compiled into native, machine-specific