Java笔记之Maven,import找不到jar包(Cannot resolve symbol ‘fastjson‘)

Spring Boot默认是没有阿里巴巴的jar包的 (如果这里有fastjsonjar包,应该多一行Maven:com.alibaba:fastjsonjar:1.2.38) 

此时在类文件中使用下面的语句该import语句会变红。

那么如何导入不存在的Jar包呢?

Step One

pom.xml文件中添加com.alibaba包的denpendycy

    <dependencies> 
    ...
    ...
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.38</version>
        </dependency>

    </dependencies>

Step Two

 执行

mvn clean install -Dmaven.test.skip=true

或者点击IDE右侧Maven的clean,再点击compile/package/verify/install中任意一个

执行之后,fastjson 将变成灰色,说明导入jar包成功了

Step Three

有时,我们按照上面的步骤执行后在External Libraries下并没有出现我们希望看到的Jar包,那么我们可以按照下面的步骤操作:

右键点击pom.xml->Maven->Reload project,此时就导入成功了

下载成功的jar包一般存储在个人目录的.m2目录下,如下图所示

Step five

如果要删除项目中的jar包,去除掉pom.xml中的dependency即可

此时,当前项目的jar包会清除

 但是.m2中的jar不会被清除

Mavne源的位置

通常百度出来的结构告诉你maven源的配置文件在maven安装目录的conf目录的settings.xml文件里,如果去配置有可能会不成功。其实有个更简单的方法,如果用的是intellij IDEA只需要如下操作找到配置文件

右键项目的pom.xml,找到Maven,找到Open 'setting.xml'即可,找到里面的<mirror>标签就是maven源的配置

常见错误

 Unable to find a single main class from the following candidates

问题描述:在执行上面的命令的时候,有的项目(暂不清楚为什么有的项目会出错,有的项目不会出错)会报错:Unable to find a single main class from the following candidates ,这是因为没有指定main方法所在的类,且项目中存在多个类的main方法

解决方案:解决该问题的方法有有以下几种

*仅保留一个main方法的类

*在pom.xml的属性中指定起始类

<properties>
    <!-- The main class to start by executing java -jar -->
    <start-class>com.ght.services.ServicesApplication</start-class>
</properties>

*在pom.cml的构建-插件中指定起始类

<build>
  <plugins>
      <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>             
          <configuration>    
              <mainClass>com.ght.services.ServicesApplication</mainClass>
          </configuration>
      </plugin>
  </plugins>
</build>

others-如何在运行弹簧引导应用程序时解决“无法从以下候选项中找到单个主类”? (bswen.com)

 The goal you specified requires a project to execute but there is no POM in this directory 

问题描述:再执行mvn clean install -Dmaven.test.skip=true报这个错误

解决方案:一般是因为执行该命令的目录不是pom.xml的目录,切换到pom.xml所在目录执行即可

Failed to clean project: Failed to delete 

问题描述:再执行mvn clean install -Dmaven.test.skip=true 的时候,有时候会报错Failed to clean project: Failed to delete

原因:一般原因是因为之前编译的工程还在运行,无法clean,导致maven生命周期无法继续进行;还有可能是编译的文件被其他程序(进程)占用,导致资源无法回收。

我碰到的原因是pom.xml里还有未加载的jar包,此时执行该命令就会提示该错误

 解决方案:删除pom.xml里面的红色的dependency,再执行mvn clean install -Dmaven.test.skip=true

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值