[英]How do I do in Eclipse to build my custom java package and project at the same time?
It's said that a java package may contain classes, interfaces, enums, ... but I've been seeking over the net and I still haven't found what I'm looking for ... When I see package info files they also include classes... My idea is that package defines classes, interfaces, etc. in the same file but the code of the class should be in a separate class file, shouldn't it?据说一个 java 包可能包含类、接口、枚举,......但我一直在网上寻找,但我仍然没有找到我要找的东西......当我看到包信息文件时,他们也包含类...我的想法是包在同一个文件中定义类、接口等,但类的代码应该在一个单独的类文件中,不是吗?
Should I name the same package in each class I create for Eclipse to put them all together under the same package without the need of a package info file?我是否应该在为 Eclipse 创建的每个类中命名相同的包,以便将它们放在同一个包下而不需要包信息文件? On the other hand, is really necessary that Eclipse creates a treedir for each package?
另一方面,Eclipse 真的有必要为每个包创建一个 treedir 吗? Could I just define at a package file where the classes are located?
我可以在类所在的包文件中定义吗? What would be the content to a package info file?
包信息文件的内容是什么?
It's also said that when you're going to use a package it should be in a .jar format... is it true?也有人说当你要使用一个包时,它应该是 .jar 格式......是真的吗?
What I'm trying to do is building a common custom package for using it in several projects, but I want to have it linked instead of copied to each project so I modify the code only once... is this possible in Eclipse?我想要做的是构建一个通用的自定义包以在多个项目中使用它,但我想将它链接而不是复制到每个项目,所以我只修改一次代码......这在 Eclipse 中可能吗?
Idea 1: + a packagefile with all definitions and location of classes + a bunch of clases spread by theme in different folders + eclipse will work with a linked package file creating a package in the folder tree that contains the classes referenced in the package.想法 1:+ 包含类的所有定义和位置的包文件 + 一堆按主题分布在不同文件夹中的类 + eclipse 将使用链接的包文件在包含包中引用的类的文件夹树中创建一个包。
Idea 2: + no packagefile + each class will say which packge is in by including the package name + eclipse will work with a linked bunch of classes that references the same package, so that's what it creates in the project tree.想法 2: + 没有包文件 + 每个类将通过包含包名称来说明哪个包在其中 + eclipse 将与引用相同包的一组链接类一起工作,这就是它在项目树中创建的内容。
Update 20180904:更新 20180904:
I would like to store all my classes in a separate folder like ".../classes/*.java"我想将所有类存储在一个单独的文件夹中,例如“.../classes/*.java”
I plan to link some of those classes under a package as Eclipse environment allows, say ".../src/com.dataproc/class1.java ..."我计划在 Eclipse 环境允许的情况下将其中一些类链接到一个包下,比如“.../src/com.dataproc/class1.java ...”
I would like to compile the package and get it ready for the project I'm developing and for other projects in a special directory, say ".../mylib/common/*.jar我想编译包并为我正在开发的项目和特殊目录中的其他项目做好准备,比如“.../mylib/common/*.jar
When I say "at the same time" it's because I want to work in the same environment creating the classes for my package and my project.当我说“同时”时,是因为我想在相同的环境中工作,为我的包和我的项目创建类。
As you may think, I'm still a rookie with java and Eclipse, but hey, eager to learn!正如您可能认为的那样,我仍然是 Java 和 Eclipse 的新手,但是,嘿,渴望学习! I have to say I reviewed the mentioned Oracle info about packages but I still don't have a clear idea of how to proceed.
我不得不说我查看了提到的有关包的 Oracle 信息,但我仍然不清楚如何继续。 It's too basic for me so I don't understand if I have to declare the package inside every class java file or declare all the classes I want inside some package-info.java file, what I do know is that I have to use "import mypackagename.*" inside my project...
这对我来说太基础了,所以我不明白我是否必须在每个类 java 文件中声明包或在某个 package-info.java 文件中声明我想要的所有类,我所知道的是我必须使用“在我的项目中导入 mypackagename.*"...
Any help or orientation is really appreciated.任何帮助或方向都非常感谢。
Update 20180926: This question has been solved from this related question更新 20180926:这个问题已经从这个相关问题得到解决
First of all I try to clarify the meaning of java package , package-info.java file and JAR file.首先我尝试澄清java package , package-info.java文件和JAR文件的含义。
The concept of Java package is independent from the IDE that you use for development. Java 包的概念独立于您用于开发的 IDE。 From Oracle Documentation - Creating and Using Packages : a Java package is a grouping of related types providing access protection and name space management.
来自Oracle 文档 - 创建和使用包: Java 包是一组提供访问保护和名称空间管理的相关类型。 Note that types refers to classes, interfaces, enumerations, and annotation types.
请注意,类型是指类、接口、枚举和注释类型。
The main point is: classes in the same package can access each other's package-private and protected members .要点是:同一个包中的类可以访问彼此的包私有成员和受保护成员。 This is the crucial factor that you have to consider when you have to organize the source code inside your packages.
当您必须在包中组织源代码时,这是您必须考虑的关键因素。
The main purpose of package-info.java file was to create a space for annotations applied to a package, besides affording a home for package-level javadoc. package-info.java文件的主要目的是为应用于包的注释创建一个空间,除了为包级 javadoc 提供一个家。 [ Joseph D. Darcy's Oracle Weblog ]
[ Joseph D. Darcy 的 Oracle 博客]
JAR stands for Java ARchive . JAR代表Java 归档。 It's a file format based on the popular ZIP file format and is used for aggregating many files into one.
它是一种基于流行的 ZIP 文件格式的文件格式,用于将多个文件聚合为一个。
So the JAR file format is the main standard used to aggregate many Java class files and associated metadata and resources (text, images, etc.) into one file for distribution .因此JAR文件格式是用于将许多 Java 类文件和关联的元数据和资源(文本、图像等)聚合到一个文件中进行分发的主要标准。
What I'm trying to do is building a common custom package for using it in several projects, but I want to have it linked instead of copied to each project so I modify the code only once... is this possible in Eclipse?
我想要做的是构建一个通用的自定义包以在多个项目中使用它,但我想将它链接而不是复制到每个项目,所以我只修改一次代码......这在 Eclipse 中可能吗?
If you want to properly do that you have to structure your projects as follow:如果你想正确地做到这一点,你必须按如下方式构建你的项目:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.