【发布时间】:2016-11-26 08:47:38
【问题描述】:
我在 IntelliJ IDEA 2016.1.3 中有 java 项目,我正在构建 jar 文件(构建 -> 构建工件... -> 构建),在我想使用外部库(org.apache. commons:commons-lang3:3.4),没什么大不了的,它在 IDE 中工作。但是提取的 jar 需要该依赖项。 该怎么做?
我已经尝试过项目结构 -> 项目设置 -> 工件 -> 并添加为库。
想要的依赖 jar 位于 jar 工件的根文件夹中,但尝试使用该代码时仍然出现此错误。
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
....
....
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 37 more
我错过了什么?
【问题讨论】:
-
看起来你想要一个“胖罐子”,罐子里面有类路径罐子。这通常需要额外的处理;我使用 Spring Boot,Spring Boot Maven 插件就是这样做的。还有一些其他非 Spring 特定的重新包装器。
-
谢谢@chrylis,我希望intelliJ 为我做这件事:( 最后,我很着急,我只需要一个方法countMatches
-
private int countMatches(@NotNull String source, @NotNull String pattern) { if (!source.isEmpty() && !pattern.isEmpty()) { int count = 0; for (int idx = 0; (idx = source.indexOf(pattern, idx)) != -1; idx += pattern.length()) { count++; } return count; } else { return 0; } }
标签: java maven intellij-idea jar artifacts