java Javac找不到同目录下的类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11524949/
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
Javac can't find class that is in the same directory
提问by Nick Brunt
I am trying to compile a Java file and I'm getting this error message:
我正在尝试编译 Java 文件,但收到此错误消息:
$ 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:
这表明 javac 找不到类 BrightcoveAPI。我不确定问题是什么,因为它在同一目录中:
$ ls
apache-commons bc-j-mapi-w-2.4.jar BrightcoveAPI.class BrightcoveAPI.java BrightcoveVideoQueryPOI.java json-org lib
回答by ruakh
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 aioobesays 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.
.
位于默认类路径中,但如果您使用-cp
指定显式类路径,则仅在您指定时才包含它。- 此答案的先前版本添加
.
到类路径的末尾,但aioobe说它通常放在最前面,这是有道理的,因此我进行了相应的编辑。(类路径是按顺序搜索的,所以如果你有一个类的两个副本,.
一个在一个库中,那么你可能希望.
版本取代库版本,所以你需要先列出它。但当然,拥有两个不同的类副本通常不是一件好事!) - 您粘贴的内容看起来像 *nix shell,但您使用的是
;
,这是 Windows 上预期的分隔符。(在 *nix 上,预期的分隔符是:
。)这可能是正确的,例如,如果您使用的是 Cygwin,但我想我会提到它以防万一。
回答by Edwin Buck
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.
如果您不搜索当前目录(您的类路径不搜索),javac
则不会为您添加该目录作为附加默认值。
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.
这种行为允许javac
独立于调用编译器的用户目录,为项目(源代码文件集)一致地调用编译器。如果是任何其他方式,那么您必须确保始终从相同的工作目录编译以获得相同的结果。
---- edit after seeing comment in ruakh's excellent answer ----
---- 在 ruakh 的优秀答案中看到评论后进行编辑 ----
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 namenot the class namewhich is defined in that source code file.
您看到的第二个问题与第一个问题无关。“无法找到或加载主类”是因为您正在java
使用源代码文件名而不是在该源代码文件中定义的类名调用命令。
The java
command runs classes, not source code files. This makes more sense when you remember that a single source code file couldcontain more than one class (even if they typically don't).
该java
命令运行类,而不是源代码文件。当您记住单个源代码文件可能包含多个类(即使它们通常不包含)时,这更有意义。