classloader

Replacing java class?

不羁岁月 提交于 2019-11-28 11:39:51
问题 I'm working on a sandbox feature for my java antivirus, and I've come into a question: Does the specified package on a class matter for compilation? Example: I'm running a program that wants to use Runtime.getRuntime().exec() , when the classloader attempts to load that to run a method, does it check the package qualified in the file, if they exist? I would prefer not to try and change files in the JVM, but to simply load ones from a different package. I can accomplish the loading and such,

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

戏子无情 提交于 2019-11-28 11:26:48
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 image = ImageIO.read(is); System.out.println(image); } } See the attachment for 2 test cases. First,

Understanding Groovy/Grails classloader leak

筅森魡賤 提交于 2019-11-28 11:07:42
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. After the GC, I then opened the dump in JVisualVM and saw that most of the memory was being allocated for

sysLoader.getResource() problem in java

你离开我真会死。 提交于 2019-11-28 10:41:49
I am having following lines of code. sysLoader = (URLClassLoader)Thread.currentThread().getContextClassLoader(); url = sysLoader.getResource("tempFile.txt"); It is giving an weird problem. If I run this from a path where there is no space in the path (Folder names) then it is running fine. But if the path contains any spaces (line "c:\New Foler...") then it is not working. How to solve this? EDIT: In more detail - I inspected the sysloader object. sysloader -> UCP -> path Is having a path with character %20 instead of space And therefore all the URLs are null. How to resolve this? snoopygee

深入分析Java ClassLoader原理

青春壹個敷衍的年華 提交于 2019-11-28 10:27:40
一、什么是ClassLoader? 大家都知道,当我们写好一个Java程序之后,不是管是CS还是 BS应用,都是由若干个.class文件组织而成的一个完整的Java应用程序,当程序在运行时,即会调用该程序的一个入口函数来调用系统的相关功能,而 这些功能都被封装在不同的class文件当中,所以经常要从这个class文件中要调用另外一个class文件中的方法,如果另外一个文件不存在的,则会 引发系统异常。而程序在启动的时候,并不会一次性加载程序所要用的所有class文件,而是根据程序的需要,通过Java的类加载机制 (ClassLoader)来动态加载某个class文件到内存当中的,从而只有class文件被载入到了内存之后,才能被其它class所引用。所以 ClassLoader就是用来动态加载class文件到内存当中用的。 二、Java默认提供的三个ClassLoader BootStrap ClassLoader :称为启动类加载器,是Java类加载层次中最顶层的类加载器, 负责加载JDK中的核心类库,如:rt.jar、resources.jar、charsets.jar等 ,可通过如下程序获得该类加载器从哪些地方加载了相关的jar或class文件: URL[] urls = sun.misc.Launcher.getBootstrapClassPath().getURLs();

Java类装载过程与类装载器

限于喜欢 提交于 2019-11-28 10:27:13
1. class装载验证流程 1.1 加载 装载类的第一个阶段 通过类的全限定名取得类的二进制流 转为方法区数据结构 在Java堆中生成对应的java.lang.Class对象 1.2 链接 -> 验证 目的:保证Class流的格式是正确的 文件格式的验证 是否以0xCAFEBABE开头 版本号是否合理 元数据验证 是否有父类 继承了final类? 非抽象类实现了所有的抽象方法 字节码验证 (很复杂) 运行检查 栈数据类型和操作码数据参数吻合 跳转指令指定到合理的位置 符号引用验证 常量池中描述类是否存在 访问的方法或字段是否存在且有足够的权限 1.3 链接 -> 准备 分配内存 ,并为类设置初始值 (方法区中,关于方法区请查看 Java内存区域 ) public static int v=1; 在准备阶段中,v会被设置为0 在初始化的<clinit>中才会被设置为1 对于static final类型(常量),在准备阶段就会被赋上正确的值 public static final int v=1; 1.4 链接 -> 解析 解析是唯一一个不确定顺序的过程,它在某些情况下可以在初始化阶段之后开始,这是为了支持 Java 语言的运行时绑定(也成为动态绑定或晚期绑定)。另外注意这里的几个阶段是按顺序开始,而不是按顺序进行或完成,因为这些阶段通常都是互相交叉地混合进行的

Java的classloader

懵懂的女人 提交于 2019-11-28 10:24:06
类加载器的基本概念 类加载器(class loader)用来加载 Java 类到 Java 虚拟机中。一般来说,Java 虚拟机使用 Java 类的方式如下:Java 源程序(.java 文件)在经过 Java 编译器编译之后就被转换成 Java 字节代码(.class 文件)。类加载器负责读取 Java 字节代码,并转换成 java.lang.Class类的一个实例。每个这样的实例用来表示一个 Java 类。通过此实例的 newInstance()方法就可以创建出该类的一个对象。 基本上所有的类加载器都是 java.lang.ClassLoader类的一个实例。 下面详细介绍这个 Java 类。 java.lang.ClassLoader类的基本职责就是根据一个指定的类的名称,找到或者生成其对应的字节代码,然后从这些字节代码中定义出一个 Java 类,即 java.lang.Class类的一个实例。除此之外,ClassLoader还负责加载 Java 应用所需的资源,如图像文件和配置文件等。为了完成加载类的这个职责,ClassLoader提供了一系列的方法,比较重要的方法如 表 1所示。关于这些方法的细节会在下面进行介绍。 表 1. ClassLoader 中与加载类相关的方法 方法 说明 getParent() 返回该类加载器的父类加载器。 loadClass(String

Using javax.tools.ToolProvider from a custom classloader?

人走茶凉 提交于 2019-11-28 10:20:00
It seems to be impossible to use javax.tools.ToolProvider from a custom classloader as required by Ant or Webstart: http://bugs.sun.com/view_bug.do?bug_id=6548428 javax.tools.ToolProvider.getSystemJavaCompiler() loads javax.tools.JavaCompiler into a URLClassLoader whose parent is the system classloader. The API does not seem to allow users to specify a parent classloader. How can one use javax.tools.JavaCompiler from a custom classloader? For example: Ant loads MyParserTask MyParserTask parses Java source-code MyParserTask is loaded by AntClassLoader that delegates to the system classloader

How to use JPA2 on JBoss 5.x ? (or How to eliminate class loading isolation issue?)

泄露秘密 提交于 2019-11-28 10:11:47
I would like JBoss to use only the dependencies located in my war file. Each time I deploy this war file, JBoss still uses its own jars. Here is the jboss-web.xml I use : <?xml version="1.0" encoding="UTF-8"?> <jboss-web> <class-loading java2ClassLoadingCompliance="false"> <loader-repository> my.package:loader=my-app.war <loader-repository-config> java2ParentDelegation=false </loader-repository-config> </loader-repository> </class-loading> </jboss-web> and the jboss-classloading.xml : <?xml version="1.0" encoding="UTF-8"?> <classloading xmlns="urn:jboss:classloading:1.0" export-all="NON_EMPTY"

Load DEX file dynamically on Android 5.0

房东的猫 提交于 2019-11-28 09:49:18
Prior to Android 5.0 I was able to load DEX files dynamically using DexClassLoader and calling loadClass() method but with the latest Android version I get a ClassNotFoundException . Here is what I am doing: Generate DEX file. ../android-sdk/android-sdk-linux_86/build-tools/21.1.1/dx --dex --output=bin/output.dex bin/output.jar Create a DexClassLoader. DexClassLoader cl = new DexClassLoader( dexFile.getAbsolutePath(), odexFile.getAbsolutePath(), null, mContext.getClassLoader()); Call cl.loadClass("myMethod"); I am aware that ART uses dex2oat to generate an ELF file that is the loaded by ART