'Visual Studio Code, Java Extension, how to add a JAR to classpath?
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.
Solution 1:[1]
Update for latest version
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
Option 1: Manually
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"
]
}
Option 2: Via User Interface
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
Old Answer kept for reference...
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.
Solution 2:[2]
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:
- add the jar to a folder you prefer, e.g. a new directory "lib" in the project folder.
- add a line like:
<classpathentry kind="lib" path="lib/javaxt-cor.jar"/>
to your.classpath
. - done. You can simply import it as you are used to.
Solution 3:[3]
import external jar file into java project,now in vscode 1.48.2 is work well,follow the reference below: https://code.visualstudio.com/docs/java/java-project#_working-with-jar-files
what I want to said is, if your external jar path has Chinese characters?even if put the jar files into lib directory under project root directory and the project path has chinese characters,when you debug or run,it will report an error message like this:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
I had not idea whether this problem appear in other language characters.be careful.
Solution 4:[4]
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:
- Have a folder opened in VSCode
- Press Ctrl+Shift+P to open the Command Palette
- Type in
Java: Create New Project
- Follow the steps according to your requirements and provide the project name
Then : Expand the Java Dependencies section in your Explorer Panel then Expand your "project name".
- When you hover over "Referenced Libraries" you will see the "+" sign. Click it.
- It will open the explorer. Use it to select all your jar files and click "Select Jar Files" button.
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!!!
Solution 5:[5]
A terrible solution, but for me it works. (Assuming maven is installed).
Locate your maven repository directory, for me it is:
/Users/username/.m2/repository
Then create the path required following the package name.
If you don't know the package name you can rename the .jar
to a .zip
and extract the content.
Also create a version number, if you don't have one then make one up.
Here is a example of a structure that I created for byte-buddy-agent
since that does not have a maven snippet.
Having done that you edit pom.xml
and add something among the lines of:
<dependency>
<groupId>net.bytebuddyagent</groupId>
<artifactId>byte-buddy-agent</artifactId>
<version>1.9.8</version>
</dependency>
Save the file, reload the changes, and you should be good to go. A bunch of files should be created in the directory.
Solution 6:[6]
For adding external Jar files whithout maven or gradle .classpath file must be changed for the library.. example
<?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="lib" path="lib/log4j-1.2.6.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
But one extension conflicts with vscode-java. https://marketplace.visualstudio.com/items?itemName=georgewfraser.vscode-javac .Disable it if you've installed to suppress error if you find after changing .classpath file. You can check this issue- https://github.com/redhat-developer/vscode-java/issues/956
Solution 7:[7]
It's so easy, anyway:
- Create project with java ctrl+shift+p
- Add the jar file to your
.classpath
- Add on the top of your project package [app]; the name of your package
- We are done, don't forget to refresh it.
Solution 8:[8]
I'm going to use the Apache Commons Codec http://commons.apache.org/proper/commons-codec/ as an example and start from scratch. I've installed the Visual Studio Code Java Pack Installer https://code.visualstudio.com/docs/languages/java and got a new VSCode window.
- Ctrl+Shift+P and type to select
Java: Create Java Project
- Choose a folder for your project and click
Select the location
- Give your project a name and press Enter
- Create folder in the project called lib and copy in the commons-codec-1.14 folder from the zip file.
- Open the .classpath file and add the following line inside the node
<classpathentry kind="lib" path="lib/commons-codec-1.14/commons-codec-1.14.jar"/>
NB You may need to use the absolute address of the lib file. In my first go the above code worked fine. My demo project for this guide only worked with a full path eg<classpathentry kind="lib" path="C:/Users/tim/Desktop/JavaExample/JavaExample/lib/commons-codec-1.14/commons-codec-1.14.jar"/>
- Open the src\App.java file and add the following line below the
package app;
lineimport org.apache.commons.codec.binary.Base64;
I was then able to run the following code in App.java
package app;
import org.apache.commons.codec.binary.Base64;
public class App {
public static void main(String[] args) throws Exception {
String message = "Hello World";
System.out.println(message);
String encodedMessage = Base64.encodeBase64String(message.getBytes());
System.out.println(encodedMessage);
}
}
To produce
Hello World
SGVsbG8gV29ybGQ=
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Dortmunder |
Solution 3 | |
Solution 4 | manissss |
Solution 5 | clankill3r |
Solution 6 | lazyTank |
Solution 7 | Yunnosch |
Solution 8 | TimSmith-Aardwolf |