没有域关联的 java 包名的约定是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12535798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:17:46  来源:igfitidea点击:

What's the convention for java package names without a domain association?

javaandroidnaming-conventionspackageconventions

提问by Mike

I can't find a Q/A on SO that answers my exactquestion, so I figure I'd post it and see what comes back.

我在 SO 上找不到可以回答我确切问题的 Q/A ,所以我想我会发布它,看看会返回什么。

As far as the naming convention goes for Java packages, I understand that it's supposed to be the reverse domain name: com.whatever.stuffand I get the rules about no mixed case, hyphens, keywords, etc.

就 Java 包的命名约定而言,我知道它应该是反向域名:com.whatever.stuff我得到了关于不能混合大小写、连字符、关键字等的规则。

I've also read section 7.7 (Unique-Package-Names) of the Java Language Specification. As far as I can tell, the rules from Java are to use the reverse domain to insure uniqueness... and if you don't have one, go get one:

我还阅读了 Java 语言规范的第 7.7 节(Unique-Package-Names)。据我所知,Java 的规则是使用反向域来确保唯一性……如果你没有,那就去获取一个:

You form a unique package name by first having (or belonging to an organization that has) an Internet domainname, such as sun.com.- Section 7.7

You form a unique package name by first having (or belonging to an organization that has) an Internet domainname, such as sun.com.- 第 7.7 节

So if I'm not interested in shelling out the money for a domain name, I don't belong to a company whose name I can piggyback off of, is there any part of the naming convention that I'm missing that should be followed? I'm thinking I can just go name it whatever unique name I feel like at this point, but I was just wondering if I was missing anything.

因此,如果我对为域名掏钱不感兴趣,那么我不属于我可以借用其名称的公司,是否有我遗漏的命名约定的任何部分应该遵循? 我想我现在可以给它命名任何我觉得独特的名字,但我只是想知道我是否遗漏了什么。

Note:I would like to release my package for other developers to use, so I didn't want to just do something that worked on my system, but something more standard if possible.

注意:我想发布我的包供其他开发人员使用,所以我不想只做一些在我的系统上工作的东西,但如果可能的话,做一些更标准的事情。

I added the android tag because the java packages I'm going to be writing are going to be used in an Android application, wasn't sure if there were different opinions from android developers.

我加了android标签是因为我要写的java包要在Android应用程序中使用,不知道android开发者是否有不同的意见。

采纳答案by arshajii

If you are going to be distributing a lot of stuff, I would really suggest getting a domain name. Another alternative however would be to use your e-mail: e.g. [email protected]would become com.gmail.bob. This is less common than using domain names but is still done by some and still ensures uniqueness.

如果您要分发很多东西,我真的建议您获取一个域名。然而,另一种选择是使用您的电子邮件:例如[email protected]将成为com.gmail.bob. 这不像使用域名那么常见,但仍然由一些人完成并且仍然确保唯一性。

回答by Grundlefleck

One convention is to use the domain name of the hosting provider, e.g.

一种约定是使用托管服务提供商的域名,例如

com.github.myrepositoryname
net.sf.sourceforge.myproject
com.googlecode.myproject

Benefits:

好处:

Drawbacks:

缺点:

  • if you decide to change providers, you either have package structures which are out-of-date, or you introduce backward-incompatible changes to keep the source in line with your new provider
  • 如果您决定更改提供者,您要么拥有过时的包结构,要么引入向后不兼容的更改以使源与新提供者保持一致

回答by GreyBeardedGeek

Domain names can be had for free. For example dyn.com offers free domain names of the form 'whatever.dyndns.org' at http://free.domain.name/

域名可以免费获得。例如,dyn.com 在http://free.domain.name/提供了“whatever.dyndns.org”形式的免费域名

回答by Jannis Froese

If you are the only coder, you can just use your name. My name is Jannis Froese, so I would use

如果您是唯一的编码员,则可以使用您的姓名。我的名字是 Jannis Froese,所以我会用

jannisfroese.projectname.stuff

jannisfroese.projectname.stuff

or if you want to stay with 'valid' domain names

或者如果您想使用“有效”域名

localhost.jannisfroese.projectname.stuff

localhost.jannisfroese.projectname.stuff

(localhost is a reserved top level domain)

(localhost 是保留的顶级域)

Of course this only works if your name is sufficiently unique, so that a collision is unlikely enough

当然,这仅在您的名称足够独特时才有效,因此不太可能发生冲突

回答by Makoto

In a professional environment, the convention is to use reverse domain. In an environment that's more associated with yourself, you can use org.projectname.packagename.*.

在专业环境中,约定是使用反向域。在与自己更相关的环境中,您可以使用org.projectname.packagename.*.

It's a convention, not a hard and fast rule. You're free to use whichever domain naming style you like.

这是一个约定,而不是硬性规定。您可以随意使用您喜欢的任何域名命名风格。