简体   繁体   中英

Access class from inside .jar file through java

I have a folder of .jar files on my desktop. I want my program to be able to access the class files in the jars and create instances of them. How do I do this?

Edit: The jars are added by the user of my program. I know what folder they are in, and can figure out their names.

在类路径中包含JAR。

Include the jar on the classpath:

java -classpath [PATH_TO_JAR] YOUR_CLASS

And on your classes import the classes you wish to use:

import package.CLASS_2_USE_1 Import package.CLASS_2_USE_2

This assuming the classes are public.

Edit: If you want to load classes form jars supplied at runtime it's probably a good idea to agree on a fixed set of methods and class names. You can see how to do it on this thread:

How should I load Jars dynamically at runtime?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM