简体   繁体   English

声明的 package “” 与预期的 package “src” 不匹配

[英]The declared package “” does not match the expected package “src”

I get this error in Atom editor.我在 Atom 编辑器中收到此错误。 First time I've encountered it and now it's showing on all my java files.我第一次遇到它,现在它显示在我所有的 java 文件中。 How do i get rid of this?我该如何摆脱这个?

In java, packages have to match directory structure, and that goes for both class files and source (java) files.在 java 中,包必须匹配目录结构,这适用于 class 文件和源 (java) 文件。 So, if you have:所以,如果你有:

package foo.bar;

public class Baz {}

Then the java file must be at /X/foo/bar/Baz.java and if you want to run it as a class file, it must be at /X/foo/bar/Baz.class , where X is whatever you want and is considered 'the root' - X is what you would put on your classpath, for example. Then the java file must be at /X/foo/bar/Baz.java and if you want to run it as a class file, it must be at /X/foo/bar/Baz.class , where X is whatever you want并且被认为是“根” - 例如,X 是您将放在类路径中的内容。 Not /X/foo/bar .不是/X/foo/bar

You've got the reverse situation: You've told the atom editor that, say, /Users/home/Corvo/workspace/CorvosProject is the root, and your source file is at /Users/home/Corvo/workspace/CorvosProject/src/Hello.java .您遇到了相反的情况:您已经告诉 atom 编辑器,例如/Users/home/Corvo/workspace/CorvosProject是根目录,而您的源文件位于/Users/home/Corvo/workspace/CorvosProject/src/Hello.java Your Hello.java file contains no package statement at all (which is something you want to address at some point later).您的Hello.java文件根本不包含 package 语句(这是您稍后想要解决的问题)。 Relative to the root, the file is therefore at src/Hello.java , and hence the error.因此,相对于根,该文件位于src/Hello.java ,因此出现错误。 You don't actually want to fix it by putting package src;您实际上并不想通过放置package src; in the file - that makes no sense, package names are supposed to describe the product/library/application as well as the owner/author, and src is therefore entirely inappropriate.在文件中 - 这没有任何意义,package 名称应该描述产品/库/应用程序以及所有者/作者,因此src完全不合适。 The fix is instead to tell Atom that the 'root source directory' is not /Users/home/Corvo/workspace/CorvosProject , but /Users/home/Corvo/workspace/CorvosProject/src .解决方法是告诉 Atom “根源目录”不是/Users/home/Corvo/workspace/CorvosProject ,而是/Users/home/Corvo/workspace/CorvosProject/src

Hunt around in the configuration screen, or perhaps check if you can right click on folders to configure them as source roots;在配置屏幕中四处寻找,或者检查您是否可以右键单击文件夹将它们配置为源根; if that's the case, unset /Users/home/Corvo/workspace/CorvosProject , and set /Users/home/Corvo/workspace/CorvosProject/src (java projects can have more than one source root folder, hence why you may be able to set this on a per-folder basis).如果是这种情况,请取消设置/Users/home/Corvo/workspace/CorvosProject并设置/Users/home/Corvo/workspace/CorvosProject/src (java 项目可以有多个源根文件夹,因此您可以在每个文件夹的基础上设置)。

Atom uses eclipse under the hood, and eclipse projects can have any number of source roots. Atom 在后台使用 eclipse,eclipse 项目可以有任意数量的源根。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM