这个问题在这里已有答案:
帮助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 answersI 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 classpathBut 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.
更多推荐
发布评论