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:
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)]
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:
Download the JDK needed for vscode, Eclipse and your Java Project.
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.
Set up the Project JDK to match the default of the workspace.
Build the project (Project-Cleanup) and Export the JAR to make sure all is OK.
Now open the project in vscode.
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.Add references to the installed JDKs in User's
settings.json
under"java.configuration.runtimes"
section.Restart vscode and take the option
Configure Java Runtime
fromJAVA PROJECTS
view. Make sure that the JDK of the Java Project is detected and working correctly.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.Close and open
Configure Java Runtime
to verify that the JDK was detected by vscode.Ensure that both
JRE System Libraries
andReferenced Libraries
underJAVA PROJECTS
view are visible without any errors. Check the Problems view and try to resolve all errors.The
Referenced Libraries
should beRead 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 becomeUnmanaged
. 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
.
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.From
JAVA PROJECTS
view, you can use the optionBuild Workspace
andClean Workspace
to troubleshoot and try to resolve errors.Finally, you can
Export JAR
from the option onJAVA 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.
3.Add Ler.jar
to Referenced Libraries;
4.Code like Ler.function
().
Related Topics
How to Match "Any Character" in Regular Expression
How Does Java Store Strings and How Does Substring Work Internally
Converting String to "Character" Array in Java
Why Doesn't String's Hashcode() Cache 0
Port of Random Generator from C to Java
How to Add Shapes on Javafx Linechart
What Does It Mean When They Say Http Is Stateless
Why Is "Final" Not Allowed in Java 8 Interface Methods
How to Specify the Root Context in Your <Web-App> Tags in Web.Xml
Replacing If Else Statement with Pattern
When Is It Ok to Catch Nullpointerexception
How to Calculate the Difference Between Two Arraylists
How to Execute Windows Commands Using Java - Change Network Settings
How to Convert Pojo to JSON and Vice Versa
Should I Use Java.Util.Date or Switch to Java.Time.Localdate