为什么我可以导入特定类而不是Java中的包?(Why Can I Import a Specific Class But Not Its Package in Java? [duplicate])

编程入门 行业动态 更新时间:2024-10-23 23:26:32
为什么我可以导入特定类而不是Java中的包?(Why Can I Import a Specific Class But Not Its Package in Java? [duplicate])

这个问题在这里已有答案:

帮助java中的包 - 导入不起作用 6个答案

我是Java的新手,所以很难理解“包”范式。

我正在使用别人的包,Framework来构建我的程序。

当我这样做,一切正常:

import Framework.Item1; import Framework.Item2; import Framework.Item3;

当我这样做时,没有任何作用:

import Framework.*;

当我尝试在控制台上使用javac.exe进行编译时失败,它在编译时停止。 错误如下:

error: cannot access Item1 bad class file: ...\Framework\Item1.class class file contains wrong clsas: Framework.Item1 Please remove or make sure it appears in the correct subdirectory of the classpath

但我检查,双重检查,三重检查,并检查文件是否在正确的位置; 此外,如果那是问题,那么在导入特定类名时不会失败吗? 在我决定需要寻求帮助之前,我已经被困了一段时间。 :(

编辑 :在尝试了许多简单和复杂的解决方案后,答案终于只是保存我的所有工作,清理项目并重新编译。 然后一切都像魔术一样。

如果有人能够解释会导致这种情况的原因,以便将来可以阻止它,我将非常感激不尽。

This question already has an answer here:

Help with packages in java - import does not work 6 answers

I am new to Java, so having a difficult time understanding the 'package' paradigm.

I am using someone else's package, Framework, to build my program off of.

When I do this, everything works fine:

import Framework.Item1; import Framework.Item2; import Framework.Item3;

When I do this, nothing works:

import Framework.*;

It fails when I try to compile using javac.exe at console, it stops at compile time. The error reads:

error: cannot access Item1 bad class file: ...\Framework\Item1.class class file contains wrong clsas: Framework.Item1 Please remove or make sure it appears in the correct subdirectory of the classpath

But I have checked, double checked, triple checked, and quadruple checked that the files are in the correct places; and besides, wouldn't it fail when importing by specific class name if that were the problem, anyway? I have been stumped for a while before I decided I need to ask for help on this one. :(

Edit: After trying many solutions both simple and complex, the answer was finally to just save all my work, clean the project and recompile. Then everything worked like magic.

If anyone could explain what would cause a situation like this so that I could prevent it in the future, I would be much obliged.

最满意答案

您似乎在包和类之间存在命名冲突。 可能有一些重构留下了旧的类文件。

如果Framework不是包,而是包含Item1作为其嵌套类的类,则会出现错误。 另一种可能性是你在编译时只是使用错误的目录作为源代码根。

在任何情况下,如果遵循Java命名约定,则不会将该包称为Framework而是framework并且不会出现这种情况。

It seems that you have a naming clash between a package and a class. There may have been some refactoring which left old class files around.

The error you have would occur if Framework was not a package, but a class, which contained Item1 as its nested class. Another possibility is that you are just using the wrong directory as the source code root when compiling.

In any case, if the Java naming conventions were followed, the package would not be called Framework but framework and this situation would not arise.

更多推荐

本文发布于:2023-08-02 08:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1372291.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   Import   Java   Specific   Package

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!