How do I import jars into my java program?

前端 未结 13 1419
自闭症患者
自闭症患者 2021-01-04 12:52

I\'ve downloaded two jars. I want to import some of their contained classes. How do I do this?

It\'s for my algorithms class. I\'ve tried following the instructions

13条回答
  •  隐瞒了意图╮
    2021-01-04 13:46

    If you're using Terminal to compile and launch your program, then in the Terminal window, begin by setting the CLASSPATH:

    $ export CLASSPATH=$CLASSPATH:/Users/Michael/path/to/jar1.jar:/Users/Michael/path/to/jar2.jar
    

    Then you can type echo $CLASSPATH and see that the jars are referenced.

    Now, in the same Terminal window, use javac to compile your class. Setting the CLASSPATH as above only applies to the current Terminal window and any processes launched from it.

    Alternately you can pass the CLASSPATH to javac:

    $ javac -cp $CLASSPATH:/Users/Michael/path/to/jar1.jar:/Users/Michael/path/to/jar2.jar MyClass.java
    

    To persist this CLASSPATH for future Terminal sessions, add the export line above to the file .profile in your home directory.

提交回复
热议问题