How do I import jars into my java program?

前端 未结 13 1421
自闭症患者
自闭症患者 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:43

    Open your Sublime

    Choose the Tools->Build System->New Build System

    Add below code to the new file

    This can tell the sublime to run the commands

    {
        "cmd": ["javac -cp /Users/yourusername/algs4/stdlib.jar:/Users/yourusername/algs4/algs4.jar:. \"$file\" && java -cp java -cp /Users/yourusername/algs4/stdlib.jar:/Users/yourusername/algs4/algs4.jar:. \"$file_base_name\""],
        "shell":true,
        "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
        "selector": "source.java"
    }
    

    Hope this can help those who are following the Algorithm Course from Princeton University

    0 讨论(0)
  • 2021-01-04 13:43

    I had this same problem. Renfei Wang's solution worked for me (I don't have enough points yet to comment directly on his response).

    In Sublime, navigate to Preferences: Browse Packages>JavaC.sublime_build.

    Here's what mine looks like:

    {
        "cmd": ["javac", "-cp", "/Users/jason/Documents/lib/*:./","$file"],
        "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
        "selector": "source.java"
    }
    

    /Users/jason/Documents/lib/*:/ lets Sublime know the location of the directory that holds my packages, so that now when I build, it loads those packages first.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-04 13:49

    My solution was to add 2 new build systems to my Sublime text editor: one to compile and the other to execute. Use Tools->Build_system->New_build_system... from main menu with these two code snippets:

    for compilation (I've named the file "algs-compile.sublime_build"):

    {
      "cmd": ["javac", "-cp", "/Users/admin/algs4/stdlib.jar:/Users/admin/algs4/algs4.jar:.", "$file"],
      "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
      "selector": "source.java"
    }
    

    for running ("algs-run.sublime_build")

    {
      "cmd": ["java", "-cp", "/Users/admin/algs4/stdlib.jar:/Users/admin/algs4/algs4.jar:.", "$file_base_name"]
    }
    

    Don't forget to replace paths to jar-files here with the correct ones from your system. I understand that this question is rather old but I hope this will help somebody.

    0 讨论(0)
  • 2021-01-04 13:52

    just name the package to default then it will work fine . also after that you dont need to import anyting just run some code provided in

    Fundamentsls

    chapter like

    average

    .

    0 讨论(0)
  • 2021-01-04 13:55

    Is your Percolation program contained in its own package? If so try putting it into the default package by commenting out any package statements from your files and recompiling it. Also, nothing in algs4 is in the java package, it's all it's own separate thing.

    0 讨论(0)
提交回复
热议问题