java “预期的类、接口或枚举。” 编译java文件时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3498994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:06:58  来源:igfitidea点击:

"class, interface, or enum expected." Error when compiling java files

java

提问by Nathan Gibson

First, the code in question:

首先,有问题的代码:

package net.foo.server;

import at;
import ay;
import bi;
import bj;
import bl;
import ck;
import cw;
import da;
import dp;
import ds;
import dt;
import dw;
import ep;
import fa;
import fd;
import fi;
import fl;
import ge;
import ho;
import ht;
import java.awt.GraphicsEnvironment;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

Next, the errors:

接下来,错误:

net\foo\server\fooServer.java:8: '.' expected
import at;
         ^
net\foo\server\fooServer.java:8: ';' expected
import at;
          ^
net\foo\server\fooServer.java:9: class, interface, or enum expected
import ay;
       ^
net\foo\server\fooServer.java:10: '.' expected
import bi;
         ^
net\foo\server\fooServer.java:10: ';' expected
import bi;
          ^
net\foo\server\fooServer.java:11: class, interface, or enum expected

import bj;
       ^
net\foo\server\fooServer.java:12: '.' expected
import bl;
         ^
net\foo\server\fooServer.java:12: ';' expected
import bl;
          ^
net\foo\server\fooServer.java:13: class, interface, or enum expected

import ck;
       ^
net\foo\server\fooServer.java:14: '.' expected
import cw;
         ^
net\foo\server\fooServer.java:14: ';' expected
import cw;
          ^
net\foo\server\fooServer.java:15: class, interface, or enum expected

import da;
       ^
net\foo\server\fooServer.java:16: '.' expected
import dp;
         ^
net\foo\server\fooServer.java:16: ';' expected
import dp;
          ^
net\foo\server\fooServer.java:17: class, interface, or enum expected

import ds;
       ^
net\foo\server\fooServer.java:18: '.' expected
import dt;
         ^
net\foo\server\fooServer.java:18: ';' expected
import dt;
          ^
net\foo\server\fooServer.java:19: class, interface, or enum expected

import dw;
       ^
net\foo\server\fooServer.java:20: '.' expected
import ep;
         ^
net\foo\server\fooServer.java:20: ';' expected
import ep;
          ^
net\foo\server\fooServer.java:21: class, interface, or enum expected

import fa;
       ^
net\foo\server\fooServer.java:22: '.' expected
import fd;
         ^
net\foo\server\fooServer.java:22: ';' expected
import fd;
          ^
net\foo\server\fooServer.java:23: class, interface, or enum expected

import fi;
       ^
net\foo\server\fooServer.java:24: '.' expected
import fl;
         ^
net\foo\server\fooServer.java:24: ';' expected
import fl;
          ^
net\foo\server\fooServer.java:25: class, interface, or enum expected

import ge;
       ^
net\foo\server\fooServer.java:26: '.' expected
import ho;
         ^
net\foo\server\fooServer.java:26: ';' expected
import ho;
          ^
net\foo\server\fooServer.java:27: class, interface, or enum expected

import ht;
       ^
30 errors

I have already made sure the the CLASSPATH variable includes the directory. Anybody have any clue? (Note: the compiler is only having trouble with my classes

我已经确保 CLASSPATH 变量包含目录。有人有任何线索吗?(注意:编译器只有我的类有问题

回答by RonU

Import statements are used to import types. There's no concept of importing a package the way you've written your code.

导入语句用于导入类型。没有按照您编写代码的方式导入包的概念。

You can import every type in a package using star-notation, e.g.,

您可以使用星号符号导入包中的每种类型,例如,

import at.*;

But you cannot just import the package "at." This is the source of all the compiler issues you're seeing.

但是您不能只导入包“at”。这是您看到的所有编译器问题的根源。

I'd further point out that completely undescriptive two-letter package names without any hierarchy whatsoever goes very much against well-established naming and organization conventions in Java. I refer you to a Wikipedia link on naming conventionsfound by simply Googling for "Java package names."

我还要进一步指出,完全没有描述性的两个字母的包名,没有任何层次结构,非常有悖于 Java 中完善的命名和组织约定。我向您推荐了一个有关命名约定的 Wikipedia 链接,只需在 Google 上搜索“Java 包名称”即可找到。

I recommend you become more familiar with the language conventions. These conventions are used because they make things a lot easier on you as well as anybody else who wants to read your code.

我建议您更加熟悉语言约定。使用这些约定是因为它们使您和其他任何想要阅读您的代码的人的工作变得更加轻松。

回答by Matt Ball

For starters, you are importing a whole bunch of classes into a .javafile that doesn't declare anything. That's what the class, interface, or enum expectedmessage is about.

首先,您将一大堆类导入到一个.java没有声明任何内容的文件中。这就是class, interface, or enum expected消息的内容。

After all those imports, you would declare a class (or interface, or enum) like this:

在所有这些导入之后,您将像这样声明一个类(或接口或枚举):

package net.foo.server;

/*
 * import statements 
*/

public class MyClass {
    // fields, methods, etc. here
}


What are the randomly-named 2-letter packages you're importing?

您导入的随机命名的 2 个字母的包是什么?

回答by ra4king

You can't import classes like that unless they are part of a package outside the package this class is in. If these classes are outside, you are going to have to explicitly import them or use the * wildcard to import them all at the same time.

你不能像这样导入类,除非它们是这个类所在的包之外的包的一部分。如果这些类在外面,你将不得不显式导入它们或使用 * 通配符同时导入它们时间。

回答by ra4king

Couple of possibilities:

几种可能性:

1) Move your server class into the same package as everything else and get rid of the imports. You'll need to recompile all the other classes, too, and change the net.foo.server import in the child classes of course.

1) 将您的服务器类移动到与其他所有内容相同的包中并摆脱导入。当然,您还需要重新编译所有其他类,并更改子类中的 net.foo.server 导入。

2) Don't bother with the net.foo.server package and make your changes elsewhere, like gy.java, etc. Compile just the classes you need and add them to the jar. Be sure to point your classpath at the server.jar and not your compiled sources when you build.

2) 不要理会 net.foo.server 包并在其他地方进行更改,例如 gy.java 等。只编译您需要的类并将它们添加到 jar 中。确保在构建时将类路径指向 server.jar 而不是已编译的源。

3) Compile with an older JDK--if the code will support it. In older JDKs you can import from the default package with "import Unfinished".

3) 使用较旧的 JDK 进行编译——如果代码支持的话。在较旧的 JDK 中,您可以使用“import Unfinished”从默认包中导入。

4) Wait and pray. (The most popular approach.) Remember anything you do will most likely break on a Friday when you least expect it anyway.

4) 等待并祈祷。(最流行的方法。)请记住,您所做的任何事情很可能会在您最不期望的星期五中断。