简体   繁体   English

如何在Intellij IDEA中从类创建.jar库

[英]How to create .jar library from class in Intellij IDEA

I am trying to learn java and have implemented a single, and simple, class in Intellij IDEA 14.1.3. 我正在尝试学习java并在Intellij IDEA 14.1.3中实现了一个简单的类。 I want to be able to use this class in other projects without copy and pasting the class source into each project src directory. 我希望能够在其他项目中使用此类,而无需复制并将类源粘贴到每个项目的src目录中。 Essentially, I want to create a package, or module (I'm not sure which, which is part of the problem) that I can simply import into any projects's src that I am working on--kind of like my own, one-class, library. 基本上,我想创建一个包或模块(我不确定哪个,这是问题的一部分),我可以简单地导入到我正在处理的任何项目的src中 - 有点像我自己的,一个 - 班级,图书馆。 While trying to figure out how to do this, I came across the two following blog posts-- 在试图弄清楚如何做到这一点时,我发现了以下两篇博文 -

http://blog.jetbrains.com/idea/2011/10/new-in-11-create-library-from-project-view/ http://blog.jetbrains.com/idea/2010/08/quickly-create-jar-artifact/ http://blog.jetbrains.com/idea/2011/10/new-in-11-create-library-from-project-view/ http://blog.jetbrains.com/idea/2010/08/quickly-创建-JAR假象/

--but when I tried to do what they instructed (creating artifacts in Project structure, etc.) I wasn't able to because my class doesn't have a main() method, as its not meant to. - 但是当我尝试按照他们的指示行事时(在Project结构中创建工件等)我无法做到,因为我的类没有main()方法,因为它并不意味着。 So I was wondering if I was going about this right--is there a way to do this, or am I stuck with copying and pasting? 所以我想知道我是否正确的做法 - 有没有办法做到这一点,还是我坚持复制和粘贴?

I managed to get it--I had to go into Project Structure and set it to create a jar file containing my compiled .class file upon build. 我设法得到它 - 我必须进入Project Structure并将其设置为在构建时创建包含我编译的.class文件的jar文件。 Then I could add the jar file as an external library in my other projects. 然后我可以在我的其他项目中将jar文件添加为外部库。 Thanks guys for the replies. 谢谢各位回复。

You only need a main method if you want the package to be able to run as a standalone application. 如果希望程序包能够作为独立应用程序运行,则只需要一个main方法。

So, to answer your question: no. 所以,回答你的问题:不。

You don't need a main method to create a package, nor to import or use the package/library in a separate application. 您不需要main方法来创建包,也不需要在单独的应用程序中导入或使用包/库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM