In Eclipse IDE, I add a JAR library using
Project > Build Path > Configure Build Path...
What is the equivalent in VisualStudioCode?
I had a look into launch.json
. There is a classpath defined. Adding JARs to this classpath (array) variable seems to have no effect.
Essentially, this is a duplicate question of
Visual Studio Java Language Support add jar.
But that question is unanswered.
This is such an extremely basic question, that I really don't understand not to find a solution for it in Microsoft's documentation or via Google search.
You can launch the classpath configuration from “JAVA PROJECTS” explorer or by clicking Ctrl+Shift+P to open command palette and then typing “configure classpath” on the palette. This feature is released as part of Java Extension Pack.
Start Visual Studio Code. In the Open Folder dialog, create a ClassLibraryProjects folder and click Select Folder (Open on macOS). Open the Terminal in Visual Studio Code by selecting View > Terminal from the main menu. The Terminal opens with the command prompt in the ClassLibraryProjects folder.
Methods: JAR file can be added in a classpath in two different ways Step 5: Select the jar file from the folder where you have saved your jar file Step 6: Click on Apply and Ok.
Add a JAR. JAR file(s) can be added by clicking the + sign next to Referenced Libraries node in project view. ... When you edit Java source code in Visual Studio Code, the Java language server is building your workspace to provide you with the necessary language features. You can see the detailed build task status and watch what is happening ...
If you have already installed VS Code and want to add Java support to it, we recommend to use Java Extension Pack, a collection of extensions suggested by Microsoft: Alternatively, you can add Java language support to VS Code by installing the popular Java extensions by yourself.
This option is viable when we are passing – classpath option while running our java program like java – classpath $ ( CLASSPATH) Main. In this case, CLASSPATH shell variable contains the list of Jar file which is required by the application.
Pre-requisite: In VS Code you need to open the folder and not just file to include any jar file for compilation, running, debugging and testing
VSCode works fine with maven or gradle, and if we are using any of those, we should use maven or gradle for dependency management.
If you are not using any dependency management, then there are two options to add jar file
Open .vscode/settings.json file (if not present, you can create one) and add required jar file there as shown below. In this case all the jar files present in lib directory will be included + I have added common-logging-1.1.1.jar which is located outside the project directory
{
"java.project.referencedLibraries": [
"lib/**/*.jar",
"c:\\path\\to\\jarfile\\commons-logging-1.1.1.jar"
]
}
If Java Extension is enabled then on left panel there will be Java Dependencies.
Inside there you can click on +
symbol to add customer jar files. With this method too, the settings.json
file will be updated
VSCode works fine with maven or gradle. But without those, as far as I know they have not provided any direct way to add jar file in classpath.
The workaround is to add the entry in .classpath
file manually.
For e.g. in below file I have added common-logging-1.2.jar
located in lib
directory manually by adding a line <classpathentry exported="true" kind="lib" path="lib/commons-logging-1.2.jar"/>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
<classpathentry exported="true" kind="lib" path="lib/commons-logging-1.2.jar"/>
</classpath>
Update
Finally VSCode team is working towards a better solution. This is still work in progress, but you can refer to this link for better aproach https://github.com/microsoft/vscode-java-pack/issues/94#issuecomment-573487982
Update April 2020
VSCode Extension update now supports this feature out of the box. Now VS Code Java lets you work with JAR files directly without any build tools. Go to JAVA DEPENDENCIES view, find the Referenced Libraries node and click the + icon.
Reference https://github.com/microsoft/vscode-java-pack/blob/master/release-notes/v0.9.0.md#work-with-jar-files-directly
I know this is pretty old, but it is still high up on Google results and many may find it. I just want to recommend the following guide to VSCode wich includes a small but sufficient part to including libraries:
https://blog.usejournal.com/visual-studio-code-for-java-the-ultimate-guide-2019-8de7d2b59902
the answer is pretty simple:
<classpathentry kind="lib" path="lib/javaxt-cor.jar"/>
to your .classpath
.You must have the Java Dependency Viewer extension installed which helps import the jar files and add them to the project's classpath OR add the libraries to the ".classpath" file manually by using <classpathentry kind="lib" path="manual\path\entry\jarfile.jar"/>
tags within <classpath></classpath>
tags for each jar file.
First what you want to do is know how to create a proper Java "project" in VS Code.
To do that:
Java: Create New Project
Then : Expand the Java Dependencies section in your Explorer Panel then Expand your "project name".
That's it!!!!
By the way you can see all of this in detail at https://code.visualstudio.com/docs/java/java-project#_working-with-jar-files , it has a clear visual representation of everything related.
Happy Coding!!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With