简体   繁体   English

使用包时,javac仅在父目录中工作吗?

[英]When use package, the javac only work in parent directory?

I use cmd to compile my java applications, but when I use package to pack all the files, if I try to compile them in the working directory, even if I use full path of the file, that won't work. 我使用cmd来编译我的Java应用程序,但是当我使用package打包所有文件时,如果我尝试在工作目录中编译它们,即使我使用文件的完整路径,也无法正常工作。 But when I go to the parent directory, the same command works. 但是当我转到父目录时,相同的命令有效。 And only works in parent directory, children directories or other directories also won't work. 并且仅在父目录中起作用,子目录或其他目录也将不起作用。 Can somebody tell me why? 有人可以告诉我为什么吗? or is there any solution that make javac work in the working directory, because I use Sublime Text, its builder configs and binds the working directory. 还是有任何使javac在工作目录中工作的解决方案,因为我使用Sublime Text,它的构建器配置并绑定了工作目录。

Read https://docs.oracle.com/javase/tutorial/java/package/managingfiles.html to understand how Java source files should be organized. 阅读https://docs.oracle.com/javase/tutorial/java/package/managingfiles.html以了解应如何组织Java源文件。 Espcially, 特别是

... put the source file in a directory whose name reflects the name of the package to which the type belong... ...将源文件放在一个目录中,该目录的名称反映该类型所属的软件包的名称...

By default the source code for my.pkg.MyClass must reside in current_directory/my/pkg/MyClass.java . 默认情况下, my.pkg.MyClass的源代码必须位于current_directory/my/pkg/MyClass.java You can use other than current directory with javac option -sourcepath . 您可以将当前目录以外的其他目录与javac选项 -sourcepath

For example you have two classes in two different packages packagea and packageb laying in the parent directory . 例如,在父目录中放置的两个不同的软件包packageapackageb有两个类。

And class names are ClassA.java and ClassB.java respectively, and you have packaged your classes in these packages then you have to use javac command in parent directory like this: 并且类名分别为ClassA.javaClassB.java ,并且您已经将这些类打包在这些包中,然后必须在父目录中使用javac命令,如下所示:

javac packagea\ClassA.java packageb\ClassB.java

Your directory structure will be like this: 您的目录结构将如下所示:

Parent_directory
  |
  |--packagea->ClassA.java
  |--packageb->ClassB.java

Otherwise, you can use any build tool like ant or maven to compile your code. 否则,您可以使用任何构建工具(如antmaven来编译代码。 You have to just run build.xml file and your all files will be compiled. 您只需要运行build.xml文件,所有文件都将被编译。

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

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