Java重写类来加载项目类而不是Jar类 [英] Java override class to Load project Class instead Jar class

查看:261
本文介绍了Java重写类来加载项目类而不是Jar类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jar,其中包含要修改的类。由于某些原因,我不想重新编译和更新Jar。因此,我要做的是在项目中为该类重新创建一个完全合格的名称,然后将代码放入其中,但是我不知道如何确保JVM加载了我的项目类而不是Jar类。
我已经看过一些帖子,但是它们是关于如何将Jar类加载到项目中的,而不是相反。

I have a jar that contain a class that I would like to modify. For some reason I do not want to re-compile and update the Jar. So what I do is I recreate a full qualified name for that class in my project, and place my code in it, but I do not have an idea how to ensure JVM loaded my project class and not the Jar class. I have look through some post but they are about how to load a Jar class into project, not the other way round.

推荐答案

通常,您需要做的就是确保您的类在包含要覆盖的代码的jar之前位于类路径上。

Typically all you need to do is ensure that your class is on the classpath before the jar containing the code you're trying to override.

如果从命令行重新运行(将替换为; 在Windows上执行):

If you're running from the command line (replace : with ; for execution on windows):

java -cp myproject.jar:their.jar com.package.Main

如果使用的是Eclipse,请右键单击项目> Build Path> Configure Build Path ...,然后选择 Order and Export选项卡并确保您的项目源目录在列表中要覆盖的jar上方。

If you're using Eclipse right-click on the project > Build Path > Configure Build Path..., then select the "Order and Export" tab and make sure your project source directory is above the jar you're overriding in the list.

这篇关于Java重写类来加载项目类而不是Jar类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆