简体   繁体   中英

The declared package “” does not match the expected package “src”

I get this error in Atom editor. First time I've encountered it and now it's showing on all my java files. 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. 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. Not /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 . Your Hello.java file contains no package statement at all (which is something you want to address at some point later). Relative to the root, the file is therefore at src/Hello.java , and hence the error. You don't actually want to fix it by putting 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. 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 .

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).

Atom uses eclipse under the hood, and eclipse projects can have any number of source roots.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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