How do I do in Eclipse to build my custom java pac

2019-09-20 17:52发布

问题:

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?

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?

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

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?

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.

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.


Update 20180904:

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

  • I plan to link some of those classes under a package as Eclipse environment allows, say ".../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

  • 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! 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.


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

回答1:

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

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.


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 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.

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.


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?

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

  • 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.