为什么 Java 源文件要进入目录结构?

发布于 2024-09-04 17:17:40 字数 923 浏览 10 评论 0原文

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

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

将文件直接放在“src”文件夹下

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

并编译它们使用命令

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

编译器编译这些文件并将类文件放入“classes”目录内适当的子目录中,例如

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

因为规范要求类应该位于适当的目录结构 美好的。

我的问题是这样的:当我使用EclipseNetBeans等IDE时,它们会为源代码目录('src ' 目录)以及。这是为什么?是强制性的吗?或者,这只是一个约定?

谢谢。

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

with files put immediately under the folder 'src'

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

and compile them using the command

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

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

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

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?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

善良天后 2024-09-11 17:17:40

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

另外,如果您有两个同名的类(但在不同的包中),如果不在不同的目录中,您将如何存储源代码?

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

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.

颜漓半夏 2024-09-11 17:17:40
Is it mandatory? 

Or, is it just a convention?

是,在源树中反映您的包结构。

我一直认为Java的有点破损:

它看起来是分层的,但事实并非如此。

它是一个简单(唯一)的前缀,用于定义单独的普通名称空间。

Is it mandatory? 

No

Or, is it just a convention?

Yes, to reflect your package structure in your source tree.

I always thought that Java's package is a little bit broken:

it seems to be hierachical, but it is not.

it is a simple (unique) prefix to define seperate plain namespaces.

木有鱼丸 2024-09-11 17:17:40

我认为这是强制性的,但你的经验表明并非如此。不管怎样,这只是常识,对吧?大型项目有如此多的源文件 - 为什么源文件和类文件具有不同的结构会让生活变得更加复杂?

I thought it was mandatory, but your experience suggests otherwise. Either way, it's just common sense, right? Large projects have so many source files - why make life more complicated by having different structures for your source and your class files?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文