简体   繁体   English

Java反思:如何在不将另一个项目或JAR添加到我的类路径的情况下从另一个项目加载一个类?

[英]Java reflection: How can I load a class from another project without adding that project or JAR to my classpath?

Here's my situation: I have a class in project A. I want to load this class in a method in project B without adding project A (or a JAR containing the class) to my project B classpath (I personally have no objection to doing it that way, but I must build this to spec). 这是我的情况:我在项目A中有一个类。我想在项目B中的方法中加载该类, 而又不将项目A(或包含该类的JAR)添加到我的项目B类路径中(我个人不反对这样做)这样,但是我必须根据规范进行构建)。

Note that I will not know the absolute path to this class (let's call it "MyClass") as project A may be installed various different locations. 请注意,由于项目A可能安装在不同的位置,因此我将不知道此类的绝对路径(我们称其为“ MyClass”)。 I will, however, know how to navigate to it based on my current directory, so that is what I have done. 但是,我将知道如何根据当前目录导航到该目录,这就是我所做的。

Here is what I have tried: 这是我尝试过的:

// Navigate to the directory that contains the class
File pwd = new File(System.getProperty("user.dir"));
File src = pwd.getAbsoluteFile().getParentFile();
File dir = new File(src.getAbsolutePath() + File.separator + "folder" + File.separator + "src");
// dir is the directory that contains all the packages of project A

// Load the class
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] {dir.toURI().toURL()});

try {
    classLoader.loadClass("com.blahblahblah.MyClass");
} catch (ClassNotFoundException exception) {
}

This throws a ClassNotFoundException. 这将引发ClassNotFoundException。 Am I doing something wrong? 难道我做错了什么?

You need to implement a custom classloader, something like this 您需要实现一个自定义的类加载器,类似这样

    Class<?> cls = new ClassLoader() {
        public java.lang.Class<?> loadClass(String name) throws ClassNotFoundException {
            byte[] a = read class bytes from known location
            return defineClass(name, b, 0, a.length);
        };
    }.loadClass("test.MyClass");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Java中动态运行时将.class文件从一个项目(项目2)加载到另一个Web项目(项目3)? - How can I load .class files from one project(project 2) to another web project(Project 3) at dynamically runtime in java? 我如何使用ClassLoader从另一个项目中加载类 - How can i load a class from another project using ClassLoader 如何在我的 Java 项目中包含 PostgreSQL JDBC JAR? - How can I include the PostgreSQL JDBC JAR in my Java project? 如何从主Java应用程序运行jar文件? jar文件必须在我的Java项目中 - How can I run a jar file from my main Java app? Where jar file must be in my Java project 如何获取另一个Java项目的类路径? - How do I obtain classpath of another java project? 如何从使用“类路径”中添加的 *.jar 文件的 eclipse java 项目创建 jar 文件 - How to create a jar file from an eclipse java project that uses *.jar files added in “Classpath” 如何从实现我的接口的JAR归档中加载类? (java) - How can I load a class from a JAR-archive which implements my interface? (java) 将外部jar加载到Java项目的类路径中 - load external jar in to class path for java project ClassLoader:如何从另一个项目加载类 - ClassLoader: how to load class from another project 如何在JAVA中使用另一个jar中的类? - How can I use class from another jar in JAVA?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM