简介
本文介绍Idea中常见的一些问题,有:找不到或无法加载主类;cannot resolve symbol “xxxx”;Command line is too long;Reading pom.xml;运行时在单元测试失败(找不到符号)等。
错误: 找不到或无法加载主类 xxx
复现
修改包的名字:右键包=> Refactor=> Rename,重新运行出错
原因
Refactor功能没有修改Idea的运行配置。
在以下两处可以发现,都是原来包的位置。
Run=> Edit Configurations=> Application=>
Configurations=> Main class
Code Coverage=> Packages and classes to include in coverage data
解决
把上边两处改为相应的包即可。
cannot resolve symbol “xxxx”
情况1
现象:类显示为红色,compile也失败
原因:包不在classpath中
解决:鼠标放到红色处=> alt + enter=> Add library 'xxxxxx' to classpath
情况2
现象:IDEA 无法识别同一个 package 里的其他类,将其显示为红色
原因:JDK丢失了
解决: File=> Project Structure=> Project Settings=> Project SDK=> 选择正确的SDK
情况3
现象: IDEA 无法识别同一个 package 里的其他类,将其显示为红色,但是 compile 没有问题。鼠标放上去后显示 “Cannot resolve symbol XXX”,重启 IDEA ,重新 sync gradle,Clean build 都没有用。
原因: 多半是因为 IDEA 之前发生了错误,某些 setting 出了问题。
解决:File=> Invalidate Caches / Restart=> Invalidate and Restart
Command line is too long
错误日志
Error running 'ServiceStarter': Command line is too long. Shorten command line for ServiceStarter or also for Application default configuration.
解决方法1
Run=> Edit Configuration=> SpringBoot=> Configuration=> Environment=> Shorten command line=>
将none项改为"Jar manifest"
解决方法2
修改项目下 .idea\workspace.xml,找到标签 , 在标签里加一行
<property name="dynamic.classpath" value="true" />
解释
该选项控制如何将classpath传递给JVM:通过命令行或通过文件。大多数操作系统都有最大的命令行限制,当它超过时,IDEA将无法运行您的应用程序。 当命令行长于32768个字符时,IDEA建议您切换到动态类路径,长类路径被写入文件,然后由应用程序启动器读取并通过系统类加载器加载。
Reading pom.xml
更新了Idea版本到2020.2.3之后,再次打开项目就停在这里:Reading xxx pom.xml。
解决方法
关闭Idea=> 删掉项目路径下的.idea文件夹=> 以pom.xml打开项目
运行时在单元测试失败(找不到符号)
第一步:删除.idea文件夹、项目名.iml
第二步:重新导入项目(通过目录打开,而不是以前记住的项目)
同样的程序,其中一个失败
复现
有两个工程,test_maven和test_ES,里边的程序及配置一模一样,但test_maven成功,test_ES报异常:Caused by: java.lang.ClassNotFoundException: org.elasticsearch.index.query.QueryBuilder
排查过程
1. 使用BeyondCompare比较,发现部署的xml文件不同:
test_maven的test_maven_war_exploded.xml:
test_ES的test_ES_war_exploded.xml:
可见差别:ES的没有相关库。
2.重新部署
File=> Project Structure=> Project Settings=> Artifacts=> 中间“+”号=> 选择默认的模块=> Apply
经试验,还是报错
3.重新配置Tomcat
Run=> Edit Configurations=> 删除原有Tomcat=> 中间“+”号=> Tomcat Server=> Local=> ...
详见:Tomcat系列--综述_feiying0canglang的博客-CSDN博客
Gradle项目缓存问题
错误日志
2020-08-18 09:28:27.154 INFO [-,,,] 4940 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
2020-08-18 09:28:29.244 INFO [-,,,] 4940 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Connect Timeout Exception on Url - http://localhost:8888. Will be trying the next url if available
...
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.application.name' in value "${spring.application.name}"
解决方法
启动突然报错
配置的bootstrap如下:
spring:
application:
name: product
# 配置中心
cloud:
config:
fail-fast: true
name: ${spring.application.name}
# profile: ${spring.profiles.active}
discovery:
enabled: true
service-id: config
# profiles:
# active: dev
# 注册中心配置
eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://192.168.1.123:8765/eureka/
按理应该会到http://192.168.123:xxx(配置中心微服务)去获取配置,但启动时却发现:
Fetching config from server at : http://localhost:8888
目前未确定问题,尝试删除.idea/workspace.xml。
打包:already exists in vfs
解决方法:需要删除原来的META-INF,原先的META-INF所在目录在Directory for META-INF/MANIFEST.MF下