Javac找不到同目录下的类 [英] Javac can't find class that is in the same directory

查看:46
本文介绍了Javac找不到同目录下的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编译 Java 文件,但收到此错误消息:

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

这表明 javac 找不到类 BrightcoveAPI.我不确定是什么问题,因为它在同一目录中:

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

推荐答案

您需要在类路径中包含 .(当前目录):

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

一些注意事项:

  • . 位于默认类路径中,但如果您使用 -cp 指定显式类路径,则只有在您指定时才会包含它.
  • 此答案的先前版本将 . 添加到类路径的末尾,但 aioobe 说它通常放在最前面,这是有道理的,所以我做了相应的编辑.(按顺序搜索类路径,因此如果您有一个类的两个副本,一个在 . 中,另一个在库中,那么您可能希望 . 版本取代库版本,所以你需要先列出它.当然,一个类的两个不同的副本通常不是一件好事!)
  • 您粘贴的内容看起来像 *nix shell,但您使用的是 ;,这是 Windows 上预期的分隔符.(在 *nix 上,预期的分隔符是 :.)这很可能是正确的,例如如果您使用的是 Cygwin,但我想我会提到它以防万一.
  • . 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.

这篇关于Javac找不到同目录下的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆