为什么Java源文件会进入目录结构? [英] Why do Java source files go into a directory structure?

查看:124
本文介绍了为什么Java源文件会进入目录结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我们假设我正在创建一个包含以下类的Java项目

as weSuppose that I am creating a Java project with the following classes


  1. com.bharani.ClassOne

  2. com.bharani.ClassTwo

  3. com.bharani.helper.HelperOne

  4. com.bharani.helper.support.HelperTwo

  1. com.bharani.ClassOne
  2. com.bharani.ClassTwo
  3. com.bharani.helper.HelperOne
  4. com.bharani.helper.support.HelperTwo

文件放在文件夹'src'下

with files put immediately under the folder 'src'


  1. src / ClassOne.java

  2. src / ClassTwo.java

  3. src / HelperOne.java

  4. src / HelperTwo.java

  1. src/ClassOne.java
  2. src/ClassTwo.java
  3. src/HelperOne.java
  4. src/HelperTwo.java

并使用命令编译它们


$ javac -d classes src / * .java(假设类目录存在)

$ javac -d classes src/*.java (assuming that classes directory exists)

编译器编译这些文件并将类文件放在'classes'目录下的相应子目录中,如下所示

The compiler compiles these files and put the class files in appropriate sub-directories inside the 'classes' directory like this


  1. classes / com / bharani / ClassOne .class

  2. classes / com / bharani / ClassTwo.class

  3. classes / com / bharani / helper / HelperOne.class

  4. classes / com / bharani / helper / support / HelperTwo.class

  1. classes/com/bharani/ClassOne.class
  2. classes/com/bharani/ClassTwo.class
  3. classes/com/bharani/helper/HelperOne.class
  4. classes/com/bharani/helper/support/HelperTwo.class

因为规范要求类应该进入适当的目录结构。好的。

Because the spec mandates that the classes should go inside appropriate directory structure. Fine.

我的问题是:当我使用IDE,例如 Eclipse NetBeans ,他们也为源代码目录(这里的'src'目录)创建了目录结构。这是为什么?这是强制性的吗?或者,这只是一个约定吗?

My question is this: When I use an IDE such as Eclipse or NetBeans, they create the directory structure for the source code directory ('src' directory here) as well. Why is that? Is it mandatory? Or, is it just a convention?

谢谢。

推荐答案

主要是惯例。镜像二进制结构的源是有意义的。

Mainly convention. It makes sense for the source to mirror the binary structure.

另外,如果你有两个具有相同名称的类(但是在不同的包中),如果不在不同的目录中,你将如何存储源?

Also, if you have two classes with the same name (but in different packages), how would you store the source if not in different directories?

将源保存在一个文件夹中对于小项目来说很好,但是一旦你有一个更大的项目(数百个类),将源分组到包中会使事情变得更易于管理。

Keeping the source in just one folder is fine for small projects, but once you have a larger project (hundreds of classes), grouping the source into packages makes things far more manageable.

这篇关于为什么Java源文件会进入目录结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆