java包声明

时间:2012-11-16 05:40:19

标签: java package declaration

我想在默认目录下创建一个名为“blue”的包。 所以,包声明是

package blue;

位于我的源代码的最顶层。当我用

编译文件时
javac This.java 

在命令提示符下,它完全忽略了我的包声明 并将类文件存储在默认目录中。它做到了这一点 无论目录“blue”是否在默认目录下。

但是当我用

编译时
javac -d . This.java

它正在做它应该做的事情 - 创建包目录“blue” 默认情况下,将类文件存储在那里。我错过了什么?

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:4)

来自man javac

  -d directory
            Set the destination directory for class files. The destination
            directory must already exist; javac will not create the desti‐
            nation directory. If a class is part of a package, javac  puts
            the  class file in a subdirectory reflecting the package name,
            creating directories as needed. For example, if you specify -d
            /home/myclasses and the class is called com.mypackage.MyClass,
            then the  class  file  is  called  /home/myclasses/com/mypack‐
            age/MyClass.class.

         If  -d  is  not  specified, javac puts the class file in the same
         directory as the source file.

         Note:   The directory specified by -d is not automatically  added
         to your user class path.