简体   繁体   English

javac:找不到包错误

[英]javac: package not found error

I'm trying to compile a java file which imports other packages I created; 我正在尝试编译一个Java文件,该文件会导入我创建的其他程序包; however, it doesn't seem to find them. 但是,似乎找不到它们。

In my compile.bat file I have: 在我的compile.bat文件中,我有:

set classpath=c:\t\DB;c:\t\Frame 
javac comchange.java 

where the beginning section of commChange.java has commChange.java的开头部分所在的位置

package commchange;

import java.sql.*;
import java.awt.event.*;
import java.applet.*;
import DB.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.Graphics;
import Frame.*;

and the directory structure is: 目录结构为:

c:\t\commChange.java
c:\t\DB
c:\t\Frame

The error I'm getting is: 我得到的错误是:

commChange.java:12: package DB does not exist 
import DB.*; 

commChange.java:17: package Frame does not exist 
import Frame.*;

commChange.java:23: cannot find symbol 
symbol: class Frame 
... 

Any ideas? 有任何想法吗?

classpath is the list of directory roots where classes, identified by package.ClassName , are loaded from. classpath是加载了package.ClassName标识的类的目录根目录列表。 You need to set the following classpath : 您需要设置以下classpath

set classpath=c:\t

I have a couple of remarks (as many things are actually wrong): 我有几点评论(因为很多事情实际上是错误的):

  • Traditionally, packages have all lower case names ie db , invoicechange , frame , etc. 传统上,软件包具有所有小写名称,即dbinvoicechangeframe等。
  • Sun coding standards require classes to begin with a capital letter ie commChange should be named CommChange and the compilation unit should use the same name CommChange.java . Sun编码标准要求类以大写字母开头,即commChange应该命名为CommChange ,而编译单元应该使用相同的名称CommChange.java
  • Source files should be arranged in a directory tree that reflects their package tree which means that invoicechange.CommChange should be located in C:\\t\\invoicechange\\CommChange.java . 源文件应放在反映其包树的目录树中,这意味着invoicechange.CommChange应该位于C:\\t\\invoicechange\\CommChange.java

Once you'll have done these changes, you'll be able to compile your classes. 完成这些更改后,就可以编译您的类了。 To do so, either define the user class path explicitly in the CLASSPATH environment variable to include the root of the sources tree: 为此,可以在CLASSPATH环境变量中显式定义用户类路径,以包括源代码树的根:

C:> set CLASSPATH=C:\t;%CLASSPATH%

And just call javac from the C:\\t directory: 只需从C:\\t目录调用javac

C:> dir
invoicechange/ db/ frame/
C:> dir invoicechange
CommChange.java 
C:> javac invoicechange\CommChange.java
C:> dir invoicechange
CommChange.class  CommChange.java

Note that if you don't set the user class path (and thus don't override the default class path), javac will use the current directory as default. 请注意,如果您未设置用户类路径(因此不覆盖默认类路径),则javac将使用当前目录作为默认目录。 In other words, calling javac from C:\\t without setting the user class path in CLASSPATH environment variable will just work. 换句话说,从C:\\t调用javac而不在CLASSPATH环境变量中设置用户类路径就可以了。

See Setting the class path for more details. 有关更多详细信息,请参见设置类路径 Actually, you should also look at the documentation of javac . 实际上,您还应该查看javac的文档。 And reading the Sun coding standards previously mentioned would be a good idea too. 阅读前面提到的Sun编码标准也是一个好主意。

You have at least three big problems. 您至少有三个大问题。 First, the classpath needs to point to the "root" folder as mentioned in the first answer. 首先,类路径需要指向第一个答案中提到的“根”文件夹。 When you import DB, then it needs to start looking in the folder called t. 导入数据库时​​,它需要开始查找名为t的文件夹。 (It bothers me a little, though, that the error message you posted, lists Import DB.*; in the error message, with Import highlighted like a class name instead of a keyword.) (不过,您发布的错误消息列出了Import DB。*,这让我有些烦恼。在错误消息中,Import突出显示为类名而不是关键字。)

Second, there is no Frame package, so the import statement that tries to import Frame.* doesn't make any sense at all. 其次,没有Frame包,因此尝试导入Frame。*的import语句根本没有任何意义。 If you want to import the Frame class you can import java.awt.Frame;, but you already have a wildcard import for the java.awt package so you don't need that. 如果要导入Frame类,则可以导入java.awt.Frame ;,但已经有java.awt包的通配符导入,因此您不需要这样做。

Finally, the file comChange.java must be located in the folder C:\\t\\InvoiceChange, not in the C:\\t folder. 最后,文件comChange.java必须位于文件夹C:\\ t \\ InvoiceChange中,而不是在C:\\ t文件夹中。 That's because it belongs to the InvoiceChange package. 那是因为它属于InvoiceChange软件包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM