[英]VS Code can not run non-Spring Maven project correctly
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.今天,我正在尝试从想法迁移到VS代码,但是在运行非SPRING Z9115C10980645C4131313F244AC1A377F7Z在VS代码中项目时,它不会在POM.Z0F635DD0E0F3874FF58B581C132E6C77.6C7.6C7.ANFER中运行Exception in thread "main" java.lang.NoClassDefFoundError
控制台中的Exception in thread "main" java.lang.NoClassDefFoundError
。
The project shown below use fastjson to parse a Map to JSON string, the fastjson is a maven repository defined in pom.xml
. 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尝试在 VS Code 中使用Run and Debug
运行项目Look, The VS Code did not run this Maven project correctly.看,VS Code 没有正确运行这个 Maven 项目。 it did not run with dependencies in pom.xml!它没有与 pom.xml 中的依赖项一起运行!
Extension Pack for Java
for VS Code我已经为 VS Code 安装Extension Pack for Java
Spring Boot Extension Pack
for VS Code我什至为 VS Code 安装了Spring Boot Extension Pack
mvn
command is able to run in console我已经安装了Maven并配置了相应的环境变量, mvn
命令可以在控制台运行Did I miss some configurations for VS Code to run Maven project?我是否错过了 VS Code 运行 Maven 项目的一些配置? What should I do to make it running correctly in VS Code?我应该怎么做才能让它在 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
转到本地依赖安装文件夹,删除\User folder\.m2\repository\com\alibaba\fastjson\1.2.78
fastjson
in pom.xml
删除pom.xml
中的依赖fastjson
fastjson
dependency and follow the notification to synchronize project configuration.重新添加fastjson
依赖,按照通知同步项目配置。G:\maven-repo
, and modified global settings.xml
of maven like below: 1. 我将我的 maven 依赖项路径移动到其他位置:我创建了一个文件夹G:\maven-repo
,并修改了 Z402C5D9AF6B43711EA070BEE5170F74D 的全局settings.xml
。xml 如下:<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: 2.在VS Code的settings.json
中,我加了下面这行,指的是第1步的settings.xml
在哪里:"java.configuration.maven.globalSettings": "G:/ideaIU/plugins/maven/lib/maven3/conf/settings.xml"
Java: Clean Java Language Server Workspace
as @Molly Wang-MSFT said. 3.重启VS Code,执行Java: Clean Java Language Server Workspace
,正如@Molly Wang-MSFT所说。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.