本文介绍了如何在Eclipse中同时构建我的自定义Java包和项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

据说一个Java包可能包含类,接口,枚举等,但是我一直在网上寻找,但仍然找不到我想要的东西……当我看到包信息时它们还包含类的文件...我的想法是,程序包在同一文件中定义类,接口等,但是类的代码应该在单独的类文件中,不是吗?

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?

是否应该在为Eclipse创建的每个类中命名相同的程序包,以便将它们全部放在同一程序包下而不需要程序包信息文件?另一方面,Eclipse是否真的有必要为每个包创建一个treedir?我可以在一个包文件中定义这些类的位置吗?包裹信息文件的内容是什么?

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? On the other hand, is really necessary that Eclipse creates a treedir for each package? Could I just define at a package file where the classes are located? What would be the content to a package info file?

也有人说,当您要使用软件包时,它应该采用.jar格式...是真的吗?

It's also said that when you're going to use a package it should be in a .jar format... is it true?

我要做的是构建一个通用的自定义程序包,以便在多个项目中使用它,但是我希望将其链接而不是复制到每个项目中,所以我只修改一次代码...在这种情况下是可能的Eclipse?

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?

想法1:+一个包含所有类的定义和位置的包文件+一堆按主题分布在不同文件夹中的故事+ 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.

想法2:+没有包文件+每个班级都会通过包含包名来说明哪个包在其中+ 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.

更新20180904:

Update 20180904:

  • 我想将所有类存储在单独的文件夹中,例如".../classes/*.java"

  • I would like to store all my classes in a separate folder like ".../classes/*.java"

我计划在Eclipse环境允许的情况下链接其中的某些类,例如".../src/com.dataproc/class1.java ..."

I plan to link some of those classes under a package as Eclipse environment allows, say ".../src/com.dataproc/class1.java ..."

我想编译该软件包,并将其准备好用于正在开发的项目以及特殊目录中的其他项目,例如".../mylib/common/*.jar

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

当我说同时"时,是因为我想在同一个环境中为我的包和项目创建类.

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.

您可能会想,我仍然是使用Java和Eclipse的新手,但是,嘿,渴望学习!我不得不说我查看了所提到的有关软件包的Oracle信息但我仍然不清楚如何进行.这对我来说太基础了,所以我不明白是否必须在每个类java文件中声明包或在某些package-info.java文件中声明我想要的所有类,我知道我必须使用在我的项目中导入mypackagename.*"...

As you may think, I'm still a rookie with java and Eclipse, but hey, eager to learn! 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. 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...

我们非常感谢您的帮助或方向.

Any help or orientation is really appreciated.

更新20180926:此问题已解决

Update 20180926: This question has been solved from this related question

推荐答案

首先,我尝试阐明 java包 package-info.java 文件的含义和 JAR 文件.

First of all I try to clarify the meaning of java package, package-info.java file and JAR file.

Java软件包的概念独立于您用于开发的IDE.来自 Oracle文档-创建和使用软件包: Java程序包是一组相关类型的组合,提供访问保护和名称空间管理.请注意,类型是指类,接口,枚举和注释类型.

The concept of Java package is independent from the IDE that you use for development. From Oracle Documentation - Creating and Using Packages: a Java package is a grouping of related types providing access protection and name space management. 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.

package-info.java 文件的主要目的是为应用到程序包的注释创建一个空间,除了为程序包级javadoc提供一个宿主. [ Joseph D. Darcy的Oracle Weblog ]

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. [Joseph D. Darcy's Oracle Weblog]

JAR 代表 Java Archive .这是一种基于流行的ZIP文件格式的文件格式,用于将许多文件聚合到一个文件中.

JAR stands for Java ARchive. It's a file format based on the popular ZIP file format and is used for aggregating many files into one.

因此, JAR 文件格式是用于将许多Java类文件以及相关的元数据和资源(文本,图像等)聚合到一个文件中的主要标准,该文件用于分发 .

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.

如果要正确执行此操作,则必须按照以下步骤构造项目:

If you want to properly do that you have to structure your projects as follow:

  • 从现在开始在一个名为 A 的特定项目中组织可重用组件. A 可以包含所需数量的程序包.需要构建 A 并将其部署为可供其他客户端使用.有一些工具可以帮助您满足这些要求(最常用的自动化构建工具适用于Java的是 Maven 成绩).
  • Project A 编译的源代码以 JAR 文件格式提供.
  • 其他项目可以重用 A 提供的功能,例如 B 是一个依赖 A 的项目:为了重用项目 A jar文件的 A 组件必须在 B 的类路径中可用.
  • 在项目开发过程中,IDE可以轻松满足这些要求,但是当您必须分发最终产品时,前面提到的工具是最好的解决方案.
  • Organize the reusable components in a specific project, called A from now on. A can contain as many packages as required. A need to be build and deployed to be available to other clients. There are tools that can be used to help you with these requirements (the most used automation build tools for Java are Maven and Gradle).
  • Project A compiled source code is available in a JAR file format.
  • Other projects can reuse the functionalities provided by A, let's say B is a project that depends on A: in order to reuse the components of A the project A jar file must be available in the classpath of B.
  • These requirements can easily be satisfied by your IDE under the development of your projects but when you have to distribute your final products the tools previously mentioned are the best solutions.

这篇关于如何在Eclipse中同时构建我的自定义Java包和项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 21:56