I must apologize if it is a duplicate question, but I googled first without getting a useful answer.
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.
The project shown below use fastjson to parse a Map to JSON string, the fastjson is a maven repository defined in pom.xml
.
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));
}
}
<?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>
Run and Debug
in VS Code Look, The VS Code did not run this Maven project correctly. it did not run with dependencies in pom.xml!
Extension Pack for Java
for VS CodeSpring Boot Extension Pack
for VS Codemvn
command is able to run in consoleDid I miss some configurations for VS Code to run Maven project? What should I do to make it running correctly in VS Code?
Your code throws no errors in my project:
I think there's a cache messed it, you may
\User folder\.m2\repository\com\alibaba\fastjson\1.2.78
fastjson
in pom.xml
fastjson
dependency and follow the notification to synchronize project configuration.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>
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"
Java: Clean Java Language Server Workspace
as @Molly Wang-MSFT said.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.