search
HomeJavajavaTutorialHow to use the java class loader URLClassLoader

Simple demo of URLClassLoader of class loader

public class Test {
	public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException {
		URL[] urls = new URL[] {new URL("http://192.168.119.132:8080/images/animal.jar")};
		URLClassLoader classLoader = new URLClassLoader(urls);
		//加载类以后就可以得到 Class 对象了
		Class<?> clazz = classLoader.loadClass("com.learn.animal.Dog");
		classLoader.close();  //加载完类以后就可以删除了。
		//关闭 classLoader, 否则会造成 资源泄露  Resource leak
		//这里不使用默认的构造器,使用带参数的构造器 获取特殊的构造器
		Constructor<?> con = clazz.getDeclaredConstructor(String.class, int.class, int.class);
		//使用构造器创建对象
		Object ob = con.newInstance("小黑",12,50);
		//在调用 Dog 对象的 say 方法,返回对 Dog 对象自己的描述
		Method mtd0 = clazz.getMethod("say", new Class<?>[] {});  
		String dogSay = (String)mtd0.invoke(ob, new Object[] {});  
		System.out.println(dogSay);
		//再使用setter 方法,改变属性  模拟 Dog 对象,逐渐长大,变胖  哈哈!
		System.out.println("Three years later......");
		Method mtd1 = clazz.getMethod("setAge", int.class);
		mtd1.invoke(ob, 15);
		Method mtd2 = clazz.getMethod("setWeight", int.class);
		mtd2.invoke(ob, 70);
		//注意这些方法是有返回值的!  返回值都是 Object 类型,需要自己强转类型
		dogSay = (String)mtd0.invoke(ob, new Object[] {});  
		System.out.println(dogSay);
		System.out.println("It&#39;s over!");
	}
}

For the URL resource here, I use a simple nginx server built in the virtual machine. I also built it by following other people's tutorials. It is very simple. A server, haha. However, this is enough for use. If there are no conditions, or if you want to be simpler, you can use the local file system.
Use file:///jar package address Just replace this.

The jar package used here is also very simple, but I directly typed it into a jar package, which only has class files. I will decompile the following and post the code picture.
This decompilation is not the entire code, but the details are very simple. There is only one say() method. You can refer to the running screenshot below to complete it.

How to use the java class loader URLClassLoader

Then there is the running screenshot. Note that -encoding utf-8 here specifies the encoding character set to be used, because the code executed in cmd may not be compiled if there is Chinese.

How to use the java class loader URLClassLoader

The above is the detailed content of How to use the java class loader URLClassLoader. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment