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

Time:09-22

I checked this enter image description here enter image description here

CodePudding user response:

Basically speaking, java extension looks for jars from Referenced Libraries.

Here're my answers to your questions:

  1. Right click the jar and choose copy relative path then add it to settings, click the refresh button then the added jar should be displayed under the option Referenced Libraries:

  2. The CLASSPATH variable is one way to tell applications, including the JDK tools, where to look for user classes. When it comes to add jars, settings java.project.referencedLibraries wins.

  3. No way to select jar folders but you can use keyboard shortcuts to select all jars then add them.

  4. There's a setting called "java.configuration.checkProjectSettingsExclusions", and it's true by default, so .project and .classpath won't be shown in VS Code.

  5. The command Java: Configure Classpath works on my machine, which can customize current project.
    enter image description here

  6. My guess is when you open a new project, vscode popped up a window and ask you if trust it, and you chose Not, then project is read-only. Trusting the workspace then try the command Java: Configure Classpath again, it should be writeable.

CodePudding user response:

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.

  • Related