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

Cannot add local jar files in maven project
Import SWT as a Gradle dependency
How to get all rows from Oracle NoSQL table?
Android NullPointerException on a Button [duplicate]
Activity instance fields null after returning from child activity
Don't destroy a bound Service on Activity destroy
Use a non static function in a static one [duplicate]
Type inference and target type
behavior of ZonedDateTime when parsing string with conflicting zone id and offset
Let users input create object
Exception in thread “main” java.land.NullPointerException
Add icons and marker on Google Map Android
Google App Engine Task Queue REST API for the Java Development Server (devserver)
How to correctly use Dagger 2's field injection with LibGDX?
H2 CSVREAD not seperating field correctly when double quotes in values
Xamarin Java.Lang.Object creation is inefficient

Categories

HOME
asp.net-core
hp-exstream
mean-stack
jira
yahoo-oauth
elm
webpack-2
onelogin
networkx
medical
gnupg
user-input
row
zend-framework3
serverless-framework
oxyplot
jprofiler
bootstrap-tour
tibco-mdm
buildbot
virtualdub
pingfederate
fifo
dbclient
objectanimator
gsmcomm
srcset
flink-streaming
.net-4.0
google-qpx-express-api
fog
noraui
typed.js
hybridauth
windows-dev-center
overriding
suricata
web-mining
espeak
twitch
ssjs
android-fingerprint-api
eigenvalue
long-polling
user-accounts
carrot
csound
diagnostics
boost-preprocessor
spring-mongodb
spring-cache
account-kit
pintos
watchconnectivity
parallel-data-warehouse
jquery-filter
visual-c++-2008
rotativa
radtreelist
fody
superstack
apachebench
tcpserver
jms2
cartesian-product
iis-arr
jqgrid-formatter
kcachegrind
twirl
arcanist
ami
unity5.2.3
code-access-security
wp-query
reactfx
fmod
spring-io
blending
ril
plasma
android-screen-support
enterprisedb
free-variable
referrer
tinn-r
typoscript2
mhtml
ticoredatasync
osql
netdna-api
adk
pydot
vdsp
self-extracting
telerik-scheduler
gamma
routedevent
zend-tool
thread-local-storage
w3wp
geneva-server
ntvdm.exe





Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm