Search code examples
javavisual-studio-codeimportjarlibraries

Load Java libraries via VSC?


I'm very beginner in Java and for that reason I'm having issues to understand how the imports work... The problem is to import the Ini library to a project in VSC. I've download the jar from ini4j and place it in the folder Referenced Libraries of the project and a .vscode and lib folders were created. In the class file I try to import it: import org.ini4j.*; and use it. But when I compile the code it seems like it doesn't find it as you can see here. Before asking the question I did some research and tried to follow tutorials that said to introduce in .vscode\settings.json this code:

"java.project.referencedLibraries": [
    "library/**/*.jar"
    "ini4j-0.5.4/ini4j-0.5.4.jar",
    "ini4j-0.5.4/ini4j-0.5.4-jdk14.jar"
]

Do you have a solution for my problem please? Is my import poorly done or how I use the library in the call?

It seems like VSC recognises the library but not Windows...


Solution

  • The problem is the way you are compiling the program. If you want to compile it from the terminal and then run it, you need to specify what libraries you are using. I would recommend you to add Java extension to you VS code, but if you insist on compiling it from cmd you need to do:

    For Windows machine

    javac -cp ".;first.jar;second.jar;third.jar" *.java
    

    And run it as

    java -cp ".;first.jar;second.jar;third.jar" Main.java