我正在做一个应该运行在安卓和桌面(JavaSE)上的项目。为此,我将其分为3个java模块:
我使用Android (AS)的原因是显而易见的。除了JavaSE模块的“运行”按钮之外,我几乎所有东西都要工作。问题很简单:不完全的类路径。模块编译,但不运行。当我把它装进罐子里的时候,它运行得很好。JAR包括所有必需的依赖项。唯一的问题是在JavaSE模块上使用的该死的“运行”按钮。
顶级build.gradle:(基本上由AS生成)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
google()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}CommonCore build.gradle:
apply plugin: 'java-library'
targetCompatibility="1.8"
sourceCompatibility="1.8"
dependencies {
api 'org.nanohttpd:nanohttpd:2.3.1'
api 'com.google.code.gson:gson:2.8.1'
api 'org.apache.commons:commons-text:1.1'
api files('src/main/resources')
}DesktopApp build.gradle:
apply plugin: 'java'
targetCompatibility="1.8"
sourceCompatibility="1.8"
dependencies {
implementation project(':CommonCore')
implementation 'org.xerial:sqlite-jdbc:3.21.0.1'
}
jar {
manifest {
attributes 'Main-Class': 'myproject.Main'
}
from {
configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}AndroidApp build.gradle:(跳过实际的android设置以保持简短)
apply plugin: 'com.android.application'
dependencies {
implementation 'com.android.support:appcompat-v7:26.0.2'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation project(':CommonCore')
}
android { ... }当我运行它时,我得到:
Exception in thread "main" java.lang.NoClassDefFoundError: fi/iki/elonen/NanoHTTPD显然,JVM无法从CommonCore中找到依赖项‘org.nanhttpd:nanohttpd:2.3.1’。在检查了类路径之后,我看到:
/Applications/Android Studio.app/Contents/jre/jdk/<a ton of unused JARs>
/Users/bamboo/AndroidStudioProjects/MyProject/DesktopApp/build/classes/java/main
/Users/bamboo/AndroidStudioProjects/MyProject/CommonCore/build/classes/java/main缺少Gradle托管依赖项。
我做错了什么?必须看到他们,因为自动完成工作很好。
发布于 2017-12-19 06:08:55
将依赖项放置在runtime()中,例如:
dependencies {
runtime(
[group: 'org.nanohttpd', name: 'nanohttpd', version: '2.3.1'],
)
}发布于 2017-12-21 10:47:10
解决这个问题的一种方法是将NetBeans与Gradle插件一起用于JavaSE开发。
https://stackoverflow.com/questions/47878682
复制相似问题