How should i use a jar file?

≯℡__Kan透↙ 提交于 2019-12-24 21:48:44

问题


I have a jar file which contains some classes those which i want to use in my project. I am working in command line and not in eclipse.

Please tell me how i should use those classes in the jar file for my project.


回答1:


Use the -classpath command line option:

javac -classpath library.jar MyProgram.java

And then to run it, specify the classpath again - including where your actual code is:

java -classpath library.jar;. MyProgram

(That's assuming you're using Windows. On Unix use : instead of ; as that's the path separator.)




回答2:


Jar file is a way to package java classes. To use the classes in the jar file, you need to include the jar in your classpath.

You need to then import the required class(es) in your java code and access them.

Of course you need to know how to use these classes i.e. what public methods etc. they expose.

If these jars correspond to some 3rd party library, you need to check the documentation (maybe on the web) to see how to use the classes.




回答3:


You just include it to your classpath like this:

java -cp otherclasspath;thejar.jar yourlass


来源:https://stackoverflow.com/questions/1605145/how-should-i-use-a-jar-file

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