简体   繁体   English

在另一个项目中查找类的class.forName抛出ClassNotFoundException

[英]class.forName, looking for a class in a different project, throws ClassNotFoundException

In eclipse I've got 2 different project : ProjectX, generic and projectA specific. 在Eclipse中,我有2个不同的项目:ProjectX,泛型和projectA特定。

In projectX : 在projectX中:

 public static final Plugin getPlugin(String sIdentifier ) {
     String sPluginClassName = "packageName.blabla.ClassName";
     try {
                pluginClass = Class.forName(sClassName);
    ...
    } catch (ClassNotFoundException e) {
                throw new BusinessException(sIdentifier, e);
    }
}

in projectA : 在projectA中:

package packageName.blabla;

public class ClassName () {
 ....
}

During execution I get a BusinessException due to ClassNotFoundException. 在执行过程中,由于ClassNotFoundException,我收到一个BusinessException。 The class path is ok (copy/pasted not written). 类路径是正确的(复制/粘贴未编写)。 In eclipse projectA java build path / libraries contains projectX folder, projectX build path hasn't got any reference to projectA (seems legit). 在Eclipse projectA中,java构建路径/库包含projectX文件夹,projectX构建路径没有对projectA的任何引用(似乎合法)。

This works fine in a projectB specific interaction with ProjectX (I don't know where are the differences : I've checked both build path, it's done the same way). 在ProjectB特定的与ProjectX的交互中,这可以很好地工作(我不知道区别在哪里:我已经检查了两个构建路径,都以相同的方式完成)。

Any idea? 任何想法?

projectX build path hasn't got any reference to projectA (seems legit). projectX构建路径没有对projectA的任何引用(似乎合法)。

While you don't need a build-time dependency of projectX on projectA, the "bin" directory for projectA needs to be on the execution classpath for projectX. 尽管您不需要projectX对projectA的构建时依赖性,但projectA的“ bin”目录必须位于projectX的执行类路径上。 If it isn't, then the call to Class.forName in getPlugin won't look at the "projectA/bin" directory, and won't find the class that it is trying to load. 如果不是,那么对getPlugin Class.forName的调用将不会查看“ projectA / bin”目录,也不会找到它试图加载的类。


The execution classpath can be configured via the "Run > Run Configurations" window. 可以通过“运行>运行配置”窗口来配置执行类路径。

  • Select the run config for your application. 选择应用程序的运行配置。
  • Go to the Classpath panel 转到类路径面板
  • Select User Classpath 选择用户类路径
  • Click on "Add Projects" and select the project you want to add. 单击“添加项目”,然后选择要添加的项目。
  • Apply the configuration change. 应用配置更改。

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

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