I must apologize if it is a duplicate question, but I googled first without getting a useful answer.
Description
Today I'm trying to migrate from IDEA to VS Code, but when running a non-spring Maven project in VS Code, it won't run with dependencies in pom.xml, I can only get Exception in thread "main" java.lang.NoClassDefFoundError
in console.
Here I'll provided a brief example:
The project shown below use fastjson to parse a Map to JSON string, the fastjson is a maven repository defined in pom.xml
.
Project Structure
App.java
package com.example;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSON;
public class App {
public static void main(String[] args) {
Map<String, String> item = new HashMap<>();
item.put("a", "1");
item.put("b", "2");
System.out.println(JSON.toJSONString(item));
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1</version>
<name>demo</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
</dependencies>
</project>
Maven Dependencies Shown in VS Code
Try to run the project with Run and Debug
in VS Code
Result
Look! The VS Code did not run this Maven project correctly, it did not run with dependencies in pom.xml!
Trivia
- I have installed
Extension Pack for Java
for VS Code - I even installed
Spring Boot Extension Pack
for VS Code - I have installed Maven and configured corresponding Environment Variable,
mvn
command is able to run in console - The project above is running correctly in IDEA
- Spring project is able to run normally in VS Code, but the problem project is maven without Spring
Help
Did I miss some configurations for VS Code to run Maven project? What should I do to make it running correctly in VS Code?
CodePudding user response:
Your code throws no errors in my project:
I think there's a cache messed it, you may
- Turn to local dependency installation folder, delete
\User folder\.m2\repository\com\alibaba\fastjson\1.2.78
- Delete the dependency
fastjson
inpom.xml
- Open Command Palette and choose Java: Clean Java Language Server Workspace
- re-add the
fastjson
dependency and follow the notification to synchronize project configuration.
CodePudding user response:
Okay, finally I resolved my problem with VS Code myself. I put my solution here in case someone or I met the same problem in the future.
To solve the issue:
1. I moved my maven dependencies path to else where: I created a folder G:\maven-repo
, and modified global settings.xml
of maven like below:
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>G:/maven-repo</localRepository>
</settings>
2. In settings.json
of VS Code, I added the line below, which refers to where the settings.xml
in step 1 is:
"java.configuration.maven.globalSettings": "G:/ideaIU/plugins/maven/lib/maven3/conf/settings.xml"