How should i use a jar file?

2019-08-27 17:29发布

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.

标签: java jar class
3条回答
走好不送
2楼-- · 2019-08-27 18:16

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楼-- · 2019-08-27 18:19

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.)

查看更多
▲ chillily
4楼-- · 2019-08-27 18:33

You just include it to your classpath like this:

java -cp otherclasspath;thejar.jar yourlass
查看更多
登录 后发表回答