Javac can't find class that is in the same dir

2019-02-14 09:24发布

问题:

I am trying to compile a Java file and I'm getting this error message:

$ javac -cp "bc-j-mapi-w-2.4.jar;apache-commons/*;json-org/*;lib/*" BrightcoveVideoQueryPOI.java
BrightcoveVideoQueryPOI.java:57: cannot find symbol
symbol  : class BrightcoveAPI
location: class BrightcoveVideoQueryPOI
        BrightcoveAPI brightcoveAPI = new BrightcoveAPI(BrightcoveAPI.PROD_READ_URL_TOKEN);
        ^
BrightcoveVideoQueryPOI.java:57: cannot find symbol
symbol  : class BrightcoveAPI
location: class BrightcoveVideoQueryPOI
        BrightcoveAPI brightcoveAPI = new BrightcoveAPI(BrightcoveAPI.PROD_READ_URL_TOKEN);
                                          ^
BrightcoveVideoQueryPOI.java:57: cannot find symbol
symbol  : variable BrightcoveAPI
location: class BrightcoveVideoQueryPOI
        BrightcoveAPI brightcoveAPI = new BrightcoveAPI(BrightcoveAPI.PROD_READ_URL_TOKEN);
                                                        ^
3 errors

This would suggest that javac cannot find the class BrightcoveAPI. I'm not sure what the problem is as it is in the same directory:

$ ls
apache-commons  bc-j-mapi-w-2.4.jar  BrightcoveAPI.class  BrightcoveAPI.java  BrightcoveVideoQueryPOI.java  json-org  lib

回答1:

You need to include . (the current directory) in your classpath:

javac -cp ".;bc-j-mapi-w-2.4.jar;apache-commons/*;json-org/*;lib/*" BrightcoveVideoQueryPOI.java

Some notes:

  • . is in the default classpath, but if you use -cp to specify an explicit classpath, then it's only included if you specify it.
  • A previous version of this answer added . to the end of the classpath, but aioobe says that it's typically put first, which makes sense, so I've edited accordingly. (The classpath is searched in order, so if you have two copies of a class, one in . and one in a library, then you probably want the . version to supersede the library version, so you need to list it first. But of course, it's not usually a good thing to have two non-identical copies of a class!)
  • What you've pasted looks like a *nix shell, but you're using ;, which is the separator expected on Windows. (On *nix the expected separator is :.) This may well be correct, e.g. if you're using Cygwin, but I thought I'd mention it just in case.


回答2:

If you do not search your current directory (your class path doesn't) javac won't add that directory in for you as an additional default.

This behavior allows the javac compiler to be called consistently for a project (set of source code files) independent of the directory the user that invoked the compiler. If it were any other way, then you would have to ensure that you always compiled from the same working directory to get the same results.

---- edit after seeing comment in ruakh's excellent answer ----

The second issue you are seeing isn't related to the first. The "Could not find or load main class" is because you are invoking the java command with the source code file name not the class name which is defined in that source code file.

The java command runs classes, not source code files. This makes more sense when you remember that a single source code file could contain more than one class (even if they typically don't).