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

Java (Android) equivalent for PHP $$x
Why #Cacheable annotation leads to java.lang.IllegalArgumentException?
Upload video to Twitter using Twitter API
Scan a Java sentence and output each word
Vertx, Connexion to data base blocking by JDBC Client
How to add a JDBC driver to an application at run time?
Android- Wait to change latitude and longitude
Unexpected plane in java FX 3D view
Get my location in OsmDroid not work
Java Applications Crash when Browse Control Used
Why use the name of a constructor as a type inside the parameters of a method [duplicate]
RunnerClass not locating steps?
Jira/Java - injecting manager not working (setter)
How to redirect google sign in button
Akka parent is not getting messages from its child
returning the string associated with the year and rank

Categories

HOME
ms-access
pdf
atom-editor
azure-data-factory
push-notification
json.net
appx
office365api
iot
objectgears
bpmn
cplex
android-4.4-kitkat
xmpp
kalman-filter
rsync
programming-languages
infragistics
twitter-bootstrap-4
angular-ui-bootstrap
azure-storage-tables
constraint-programming
paradox
webrequest
add
nano-server
kentor-authservices
google-cloud-spanner
hex-editors
fortumo
pc
jsdoc
zapier
functional-testing
dcevm
zurb-foundation-6
wpfdatagrid
bootstrap-material-design
fopen
opentype
facebook-apps
subdomains
javascriptcore
functor
contact-form
webtest
xmlreader
amazon-kinesis-kpl
picasso
main
hockeyapp
libraries
event-driven
document.write
leading-zero
temporary-files
ensembles
hp-ux
firebase-admin
jxcore
nbconvert
bind9
topbeat
nested-sets
httplistener
ado.net-entity-data-model
phishing
storekit
ipconfig
tform
pyke
energy
hsv
google-places
jmeter-maven-plugin
clicktag
websocket4net
code-access-security
flask-cors
article
starcluster
aapt
phpthumb
reactfx
concurrent-collections
oracle-warehouse-builder
prettify
oam
xsockets.net
sabredav
cassini-dev
angularjs-controller
anonymous-methods
coderush
first-responder
armcc
ocx
netbeans-6.9
buildr
file-comparison
calling-convention
celltable
pyinotify
android-sdk-2.1
mongomapper
nhibernate.search
meego
webkit.net
brewmp
audio-capture
libs
adrotator
pascal-fc
paul-graham

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile