Packages
Packages
Compile classes
How to compile the classes in a user-defined package
- Make sure that the classes have all of the appropriate package and import statements.
- Start the command prompt and navigate to the directory that contains the package (the parent directory).
- Use the javac command to compile the classes. Be sure to include the subdirectories that correspond to the package name.
- -d option places generated .class file in folder (d stands for directory). If you do not use the -d option, then you must move the compiled .class file to the correct package directory.
Syntax
c:\parentDir>javac packagePath/ClassName.java
Example
C:\java\lineItem>javac murach/business/LineItem.java
Compilation details
- If you compile a class that uses another class in the same directory, both classes are compiled.
- Before you compile a class that imports another class in the package, you must compile the class it imports.
- Some Java tools can't compile a class that's in a package that depends on one or more classes in the same or in other user-defined packages. In that case, you need to use the javac command to compile the class.
- If you're using DOS, you can use either front or back slashes to separate the names of the directories in the package and the name of the class file.
- If you're using Linux, you must use front slashes as separators.