2012-11-16 87 views
2

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

package blue; 

在我的源代码的最顶端。当我编译

javac This.java 

文件在命令提示符下,它完全忽略了我的包声明 并存储在默认目录中的类文件。无论在默认目录下是否存在“蓝色”目录,它都会执行此操作 。

但是,当我与

javac -d . This.java 

它是做什么它应该do--创建包目录下默认的“蓝色” 和存储类文件有编译。我错过了什么?

欣赏提前帮助。

+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. 
+0

好吧,似乎没有根据需要创建目录。当我的包装声明是 package blue.red.green; 和我编译 javac -d。 This.java 它将文件放在当前目录下方的“蓝色”下。忽略“blue”下的“red.green”。 – ashley

+0

忽略它。我一定做了错事 - 工作得很好。感谢你的回答。 – ashley