javac: package not found error
I'm trying to compile a java file which imports other packages I created; however, it doesn't seem to find them.
In my compile.bat file I have:
se开发者_如何学编程t classpath=c:\t\DB;c:\t\Frame
javac comchange.java
where the beginning section of commChange.java has
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. You need to set the following classpath
:
set classpath=c:\t
I have a couple of remarks (as many things are actually wrong):
- Traditionally, packages have all lower case names i.e.
db
,invoicechange
,frame
, etc. - Sun coding standards require classes to begin with a capital letter i.e.
commChange
should be namedCommChange
and the compilation unit should use the same nameCommChange.java
. - Source files should be arranged in a directory tree that reflects their package tree which means that
invoicechange.CommChange
should be located inC:\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:
C:> set CLASSPATH=C:\t;%CLASSPATH%
And just call javac
from the C:\t
directory:
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. In other words, calling javac
from C:\t
without setting the user class path in CLASSPATH environment variable will just work.
See Setting the class path for more details. Actually, you should also look at the documentation of javac. And reading the Sun coding standards previously mentioned would be a good idea too.
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. (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.)
Second, there is no Frame package, so the import statement that tries to import Frame.* doesn't make any sense at all. 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.
Finally, the file comChange.java must be located in the folder C:\t\InvoiceChange, not in the C:\t folder. That's because it belongs to the InvoiceChange package.
精彩评论