Java类与其他类相同的包名

huangapple 未分类评论46阅读模式
英文:

Java Class With Same Package Name As Other Class

问题

我在源代码树中有两个目录。

第一个目录具有包层次结构,其中包含一个与包名称匹配并具有包可见性的类。

第二个目录与第一个目录具有相同的包层次结构,其中包含一个与包名称相同并具有主方法的类,在该主方法中使用了第一个目录树中的类。

我编译了第一个目录树中的类 *.java 文件,并在与 *.java 文件相同的目录中有一个 .class 文件。

然后我在命令提示符中设置了 classpath 变量,将第一个目录的绝对路径添加到该变量中。

我编译了第二个目录树中的类 *.java 文件,并在与 *.java 文件相同的目录中有一个 *.class 文件。

然后我在第二个目录树中执行了 *.class 文件,它成功地执行了。

如果第一个目录树中的类来自第三方,那么这将是一个安全风险,因为第三方 *.jar 文件的用户可以通过使用与类相同的包名称来访问 jar 文件中的 java 包可见性类。

英文:

I have two directories in a source tree.

The first directory has a package hierarchy that contains a class with matching package name and has package visibility.

The second directory has the same package hierarchy as the first directory that contains a class with the same package name and has main method that use the class in the first directory tree.

I compiled the class *.java file in the first directory tree and has a .class file in the same directory as the *.java file.

I then set classpath variable in command prompt to add the first directory absolute path to the variable.

I compiled the class *.java file in the second directory tree and has *.class file in the same directory as the *.java file.

I then execute the *.class file in the second directory tree and it executed successfully.

If the class in the first directory tree is from a third party, then this is a security risk because users of the third party *.jar files can access the java package visibility classes in the jar files by using the same package name as the classes.

huangapple
  • 本文由 发表于 2020年4月7日 09:44:36
  • 转载请务必保留本文链接:https://java.coder-hub.com/61071527.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定