java
“Could not find or load main class” error for Gradle-generated Scala JAR
After much research, I can't seem to get to the root of a problem I am having in generating a runnable Scala jar file using Gradle. I'm overriding the 'jar' Gradle task to create a jar file (dependencies included) that executes starting from my main class file. However, whenever I run it, regardless of what I use for a Main-Class attribute, the console throws a "Could not find or load main class" error. Here's what I have so far: build.gradle buildscript { repositories { mavenCentral() } } apply plugin: 'java' apply plugin: 'scala' apply plugin: 'application' repositories { mavenCentral() // some other repos } version = '1.0' sourceCompatibility = 1.8 targetCompatibility = 1.8 mainClassName = "com.test.Main" dependencies { // my dependencies } jar { zip64 = true manifest { attributes "Main-Class": "$mainClassName" } from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } } src/main/scala/com/test/Main.scala package com.test object Main { def main(args: Array[String]): Unit = { print("Hello world"); } } In fact, when I run "java tf test.jar", it shows "com/test/Main.class" in the root of the jar! Am I missing some important class path info or something? I'm running Java 1.8 on macOS Sierra using Gradle 3.5. Any help is appreciated. Thanks!
You can use the Shadow Jar Plugin instead of your own jar definition. Benefits of Shadow Shadowing a project output has 2 major use cases: Creating an executable JAR distribution Bundling and relocating common dependencies in libraries to avoid classpath conflicts Basic setup: shadowJar { baseName = 'your-app' classifier = 'all' version = version configurations = [project.configurations.compile] } jar { manifest { attributes 'Main-Class': 'com.test.Main' } } You can use the new syntax of Gradle Plugins: plugins { id 'java' id 'scala' id 'application' }
Related Links
JBoss 6.3.3 EAP closing datasource before application using datasource is finished unloading
How to make sure system properties are set before a SpringJUnit4ClassRunner test is run [duplicate]
Vaadin connect DB as grid backend
Use Dynamic key for hashMap in Thymeleaf, along with attribute
Google Drive Android API Authentication Problems
How does a null character (\u0000) end up in a string?
Why print function is not printing when invoked with java -jar? [duplicate]
Windows reserved name “con” workaround / hack
Retrofit - Android - Expected BEGIN_ARRAY but was BEGIN_OBJECT [duplicate]
Share the same data with different processes [duplicate]
Know when a different class is exited
Spring Boot Filter.init() methods are being invoked on first request
Switch statement returning wrong [duplicate]
Creating a project with just POJO beans
Access computer hardware using applet in web browser
Ant build does not include Android support library jar