Visual Studio Code, Java Extension, How to Add a Jar to Classpath

VSCode: How to import a jar file into your java project?

Your question is libraries you've added into the folder Referenced Libraries aren't displayed under the folder src and you want to find its solution. If there's any misunderstanding about your question, please let me know.

The official tutorial: Add a JAR and Library Configuration explain how to add a jar to your current project. Here's a simple demo:

Sample Image

Once you've add the library into the referenced libraries, you can use it in your code. VS Code is a lightweight but powerful source code editor and depends on extension to develop projects, and this is how Java Extension Pack in VS Code works. There's no need to be as the same as Eclipse.

Edit:

When it comes to get the source code of a JAR package, the command works:

jar xf jar-file [archived-file(s)]

Sample Image

About how to decompile jar File, you can refer to this.

How to add referenced library JAR files using relative path for eclipse Java project in vscode

The answer by @Molly Want-MSFT helped me a lot. Following is what I did to resolve the problem for good. I applied the steps below many time to verify they will work every time.

Steps to open a Java Project in both Eclipse and vscode:

  1. Download the JDK needed for vscode, Eclipse and your Java Project.

  2. Import the project in Eclipse and setup the JDK for the workspace and the project. Also, make sure to setup the compile level to match the JDK.

  3. Set up the Project JDK to match the default of the workspace.

  4. Build the project (Project-Cleanup) and Export the JAR to make sure all is OK.

  5. Now open the project in vscode.

  6. Setup java.home in Settings, in user and workspace sections. This must be JDK-11 or higher to allow vscode to function properly for Java Projects.

  7. Add references to the installed JDKs in User's settings.json under "java.configuration.runtimes" section.

  8. Restart vscode and take the option Configure Java Runtime from JAVA PROJECTS view. Make sure that the JDK of the Java Project is detected and working correctly.

  9. You may have to open one of the Java Source Code Files. Wait a bit until it will settle down. Check the Java Build Status progress by clicking the spinning icon in the bottom right. This icon is for Language Server and it will turn into an icon that looks like thumbs-up when build/compile is done.

  10. Close and open Configure Java Runtime to verify that the JDK was detected by vscode.

  11. Ensure that both JRE System Libraries and Referenced Libraries under JAVA PROJECTS view are visible without any errors. Check the Problems view and try to resolve all errors.

  12. The Referenced Libraries should be Read Only because this project was setup in Eclipse. You can delete the .classpath and .project files and open the project again in vscode, and try to fix the problems by adding libraries using the + button. When such files are deleted, the project will become Unmanaged. Later, you can restore such files.

When the project is unmanaged, you use Configure Java Runtime from JAVA PROJECTS view and you can check the project type. You can change the JDK to one of the installed ones as per the section "java.configuration.runtimes" in the user's setting.json.


  1. If the the Referenced Libraries is read-only, it has no effect even if you can add libraries into settings.json, but the .classpath file will win.

  2. From JAVA PROJECTS view, you can use the option Build Workspace and Clean Workspace to troubleshoot and try to resolve errors.

  3. Finally, you can Export JAR from the option on JAVA PROJECTS view. This option is not clear and it looks like and arrow pointing to the right |-->.

I hope this helps, and if you have any question, please post a comment and I will try to answer back when possible.

Use a class from a jar file in VSCode

Here's the solution:

1.Use the command javac Ler.java to compile Ler.java;

2.Type the command jar --create --file Ler.jar Ler.class to generate Ler.jar;

Attention: If Ler.java is under the subfolder, please enter it then compile and generate .jar.

Sample Image

3.Add Ler.jar to Referenced Libraries;

4.Code like Ler.function().

Sample Image



Related Topics



Leave a reply



Submit