类路径中具有相同名称的两个类 [英] Two classes with same name in classpath

查看:223
本文介绍了类路径中具有相同名称的两个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有两个同名的类在我的类路径中的两个不同的jar中说Matcher.java,其中一个将被JVM选中,那么我是否可以建议JVM选择一个特定的?

If I have two classes with same name say Matcher.java in two different jar in my classpath which one will be picked up by JVM , is there anyway I can suggest JVM to pick a specific one ?

推荐答案

引用 Oracle


规格订单

Specification order

订单你指定多个类路径条目是
重要。 Java解释器将按照它们出现在类路径变量中的顺序在
目录中查找类。在上面的示例
中,Java解释器将首先在目录C:\ java /\\ MyClasses中查找所需的
类。只有当它在该目录中找不到具有正确名称的
类时,解释器才会在C:\ _java \OtherClasses目录中显示

The order in which you specify multiple class path entries is important. The Java interpreter will look for classes in the directories in the order they appear in the class path variable. In the example above, the Java interpreter will first look for a needed class in the directory C:\java\MyClasses. Only if it doesn't find a class with the proper name in that directory will the interpreter look in the C:\java\OtherClasses directory.

提到的例子:


C:> java -classpath C:\ java \ MyClasses; C:\ java \OtherClasses ...

C:> java -classpath C:\java\MyClasses;C:\java\OtherClasses ...

所以是的,它会加载出现在类路径中的那个首先指定。

So yes, it will load the one appears in the classpath that specified first.

这篇关于类路径中具有相同名称的两个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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