javaeclipsevisual-studio-codejarreference-library

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


I checked this question and many other posts to help me resolve my issue, but nothing worked.

I trying to open an existing Java Project from Eclipse in vscode. The project was opened successfully in Eclipse 2021-06 and I fixed the referenced libraries by selecting the JAR libraries under lib/ folder and right-click - configure build batch - add select add to build path. Then the file .classpath was updated automatically. Then I had to download JDK 11 (needed for vscode) and JDK 1.8 needed for the Java project. I managed to work with eclipse to set the JDK correctly and all worked fine. I was able to compile the project, export the result JAR and deploy and all worked well.

I am having trouble to open this project in vscode using Open Java Project option. I am facing issues adding referenced libraries this Java project as it is a managed project (since it was created in Eclipse). When I try to add the libraries using vscode UI, I am getting mixed results and sometimes it is not working and the build fails. I was surprised as in some instances, things just work, and then they don't.

I tried to update .classpath file in vscode and still same issues. When I try to add the libraries from Referenced Libraries (Read Only) under JAVA PROJECTS tree in vscode by clicking +, it has no effect. This step will modify the file settings.json under .vscode folder by adding the relevant values to the property "java.project.referencedLibraries".

I am confused about how to configure the referenced libraries for Java projects in vscode. Following is a summary of questions:

  1. How to add a library using a relative path to a Java Project based on the project root folder?
  2. What is the difference between adding a library using the .classpath file and by modifying settings.json file? Which one will win?
  3. How to add a folder with all JAR files to be as the referenced library to the Java Project? This is to avoid adding one file at a time.
  4. Why the .classpath and .project files are not showing in vscode explorer view? It will only show when you open the file in vscode from the Windows File Explorer.
  5. I found command Configure Classpath but it is read-only, which I think it is the same as the .classpath file. Is there a way to change it from vscode UI?
  6. When I remove .classpath file or when I removed the classpathentry lines from the .classpath file, the node 'Referenced Libraries (Read Only)' under JAVA PROJECTS in vscode view was removed. Why?

See the snapshots below for more details.

I appreciate your help.

enter image description here enter image description here


Solution

  • 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.