In Eclipse, 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
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
.It's so easy, anyway:
.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!!!
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
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.
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.
Java: Create Java Project
Select the location
<classpathentry kind="lib" path="lib/commons-codec-1.14/commons-codec-1.14.jar"/>
<classpathentry kind="lib" path="C:/Users/tim/Desktop/JavaExample/JavaExample/lib/commons-codec-1.14/commons-codec-1.14.jar"/>
package app;
line import 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=