如何创建我自己的 Java 库(API)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3612567/
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
How to create my own java library(API)?
提问by Q8Y
I created a program in Java and I designed it so that methods that I want them to appear (getter methods) in the main, I can call them easily after initiate the class that holds these methods.
我用 Java 创建了一个程序,并设计了它,以便我希望它们出现在 main 中的方法(getter 方法),我可以在启动包含这些方法的类后轻松调用它们。
The question is that, I need to make this application (that holds the getter methods) to be like an API so that I can give my application for developers to use my functions (the getter methods) if they need them, and only what they need is to add this file (I think the API after is done shown as .jar file).
问题是,我需要让这个应用程序(包含 getter 方法)像一个 API,这样我就可以让我的应用程序让开发人员在需要时使用我的函数(getter 方法),并且只有他们需要的东西需要的是添加这个文件(我认为完成后的 API 显示为 .jar 文件)。
How can I make it so that I can make my code reusable with other application? It's similar to the .dll, I think.
我如何才能使我的代码可在其他应用程序中重用?我认为它类似于 .dll。
Thanks a lot ;)
非常感谢 ;)
采纳答案by Zack Marrapese
Create a JAR. Then include the JAR. Any classes in that JAR will be available. Just make sure you protect your code if you are giving out an API. Don't expose any methods / properties to the end user that shouldn't be used.
创建一个 JAR。然后包含 JAR。该 JAR 中的任何类都将可用。如果您提供 API,请确保保护您的代码。不要向最终用户公开任何不应使用的方法/属性。
Edit: In response to your comment, make sure you don't include the source when you package the JAR. Only include the class files. That's the best you can really do.
编辑:为了回应您的评论,请确保在打包 JAR 时不包含源代码。只包含类文件。这是你能做的最好的事情。
回答by CoolBeans
回答by Noel M
Make sure you write and publish javadocs for all your public and protected classes and methods.
确保为所有公共和受保护的类和方法编写和发布 javadoc。
回答by Michael Borgwardt
To be useable as an API, your classes should:
要用作 API,您的类应该:
- Use a unique package (ideally following the convention, i.e. the reverse of a domain you own as prefix). This prevents naming conflicts
- Have only those classes and methods
public
orprotected
that are intended to be used by others. This makes it easier to use. - Have extensive Javadoc comments.
- Be available as a JAR file - ideally separate JARs for binary distribution, source code and javadoc files.
- 使用唯一的包(最好遵循约定,即您拥有的域的反向作为前缀)。这可以防止命名冲突
- 只有那些类和方法
public
或protected
打算供其他人使用的类和方法。这使得它更容易使用。 - 有大量的 Javadoc 注释。
- 可作为 JAR 文件使用 - 最好将二进制分发、源代码和 javadoc 文件的 JAR 分开。
回答by Tai Squared
There are several ways you can expose your code. Creating a jar and distributing that may be the easiest as other developers will just have to include your jar. However, if you are talking about "anyone" accessing your code, a web service may make more sense as you can provide access to the data without providing all of the necessary code. You mention providing access to your getters - if you just create a class that has getters, the other developers can use them, but how are they going to be populated? If your application is self contained in that it gets the necessary data and provides the getters, that should work, but if you are talking about providing access to data from your running application, a web service makes more sense as your application can retrieve the data and provide access via publicly accessible methods.
您可以通过多种方式公开代码。创建一个 jar 并分发它可能是最简单的,因为其他开发人员只需要包含您的 jar。但是,如果您谈论的是“任何人”访问您的代码,那么 Web 服务可能更有意义,因为您可以在不提供所有必要代码的情况下提供对数据的访问。您提到提供对 getter 的访问 - 如果您只创建一个具有 getter 的类,其他开发人员可以使用它们,但是如何填充它们?如果您的应用程序是自包含的,因为它获取必要的数据并提供 getter,这应该可以工作,但是如果您正在谈论提供对正在运行的应用程序的数据的访问,那么 Web 服务更有意义,因为您的应用程序可以检索数据并通过可公开访问的方法提供访问权限。
You most likely want to create interfaces as well so developers can code against the interface and you can change the internal workings without impacting them. Any API that will be used by others should be extensively documented as well.
您很可能还想创建接口,以便开发人员可以针对接口进行编码,并且您可以在不影响它们的情况下更改内部工作方式。其他人将使用的任何 API 也应该被广泛记录。
回答by jmoreira
To create the jar:
创建罐子:
jar cf <jar_name> <sources>
回答by gigy
Well, depends on your IDE. I use Netbeans, so I just hit build project, and viola! A jar file is created in my directory specified. Now, that's just for compiling. All anyone has to do is download your .jar file, and if in Netbeans, right click libraries, add jar/folder, and select the downloaded file.
好吧,这取决于您的 IDE。我使用 Netbeans,所以我只需点击构建项目,然后中提琴!在我指定的目录中创建了一个 jar 文件。现在,这只是为了编译。任何人所要做的就是下载您的 .jar 文件,如果在 Netbeans 中,请右键单击库,添加 jar/文件夹,然后选择下载的文件。
回答by elyor
You can also consider:
您还可以考虑:
- Include some samples that demonstrate how to use your library
- Build your jar using Apache Maven
- Put your jar in a public maven repository
- Publish a new version of your library as you find/fix bugs
- If you want to hide your implementation, you can pack your jar with obfuscation, so that if someone decompiles your classes, the code will be difficult to read
- 包括一些演示如何使用您的库的示例
- 使用 Apache Maven 构建您的 jar
- 将您的 jar 放在公共 Maven 存储库中
- 在发现/修复错误时发布库的新版本
- 如果你想隐藏你的实现,你可以用混淆打包你的jar,这样如果有人反编译你的类,代码将难以阅读