creating our own jar and using it in another project

心不动则不痛 提交于 2020-01-17 07:56:50

问题


I want to create our own jar which has some simple methods like a print method.

I created the jar of that java project and imported it into an other project I am building path also.

The problem is that I am able to create the object of the classes which reside in the jar but I am unable to call the methods of that class. i am using eclipse 3.4 (Eclipse Ganymede (3.4))version


回答1:


Sounds like if you are successfully building the JAR that you are not including it in the classpath when you compile / run your application. You can do that if you are compiling/running from the command line with the -cp or -classpath option. Both perform the same function.

To compile:

javac -cp .:PathToMyJar/MyJar.jar MyMainClass.java

To Run:

java -cp .:PathToMyJar/MyJar.jar MyMainClass

The above commands will look in the current directory ('.') and in the MyJar.jar file specified. On Windows you will separate the items in the classpath with a semicolon and on Linux/Unix/OS X you will use a colon.

If you are using an IDE you will need to somehow add the JAR file to the classpath for your project. How to do that is IDE specific.



来源:https://stackoverflow.com/questions/6315716/creating-our-own-jar-and-using-it-in-another-project

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!