"class, interface, or enum expected." Error when compiling java files
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 j开发者_开发问答ava.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
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.
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 conventions found by simply Googling for "Java package names."
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.
For starters, you are importing a whole bunch of classes into a .java
file that doesn't declare anything. That's what the class, interface, or enum expected
message is about.
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?
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.
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.
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.
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".
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.
精彩评论