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

How to optimize groupBy() operation on Spark RDD
java regular expression comma separated numbers
Continuously animate Text from left to right
Interval continue working after onErrorResumeNext
How can i assign a string array item to edittext in the other class so when when in user mode user changes edittext , item changes also?
For Loop only printing/storing last iteration
Mocking and verifying SLF4J with JMockit
Execute bash script in eclipse
How to pass file as an argument to Python process invoked by Java
Selenium - Java - How can I pass website address from one test to another
Hadoop - Finding Unique Values in Text Files
how to repeatedly ask the user to input a sentence and it should stop if the user types END also should count the no. of words,vowels and non vowels
Android change the map fragment when entering a Geofence
ArrayIndexOutOfBoundsException after creating Arrays of Objects [duplicate]
using Stimulsoft in intellij idea and java language
Cassandra java driver connection error

Categories

HOME
cakephp
asp.net-core
vbscript
keycloak
angular-material
stock
hp-exstream
elm
google-project-tango
azure-media-services
paradox
node-notifier
facebook-messenger-bot
modx-revolution
remote-access
decimal
designer
openrefine
facebook-page
excel-vba-mac
xlsxwriter
bootstrap-tour
crosstab
microsoft-sync-framework
tdd
qhull
facet
dynamic-featured-image
kannel
elasticsearch-net
saas
large-file-upload
gitignore
socialengine
bcd
http-status-code-503
noraui
winrt-xaml-toolkit
accelerate-framework
android-nestedscrollview
plsql-psp
serverside-rendering
unspecified
lumberjack
broadcastreceiver
arena-simulation
no-www
bootstrap-dialog
thin
websphere-mq-fte
scrollable
react-scripts
jquery-nestable
rdfs
drupal-6
orthogonal
smart-table
angular-strap
angular-cache
ctest
connect-by
separator
pundit
angular-leaflet-directive
varargs
appfabric-cache
graph-drawing
mono-embedding
pretty-print
notify
geonetwork
formatjs
clicktag
cctv
bitcoinj
aapt
reactfx
htmlcleaner
comexception
monomac
flexmojos
xsockets.net
listings
cos
batterylevel
qt-faststart
limejs
eventlistener
gil
ocx
code-cleanup
eclipse-templates
horizontal-accordion
locationlistener
javax.script
recent-documents
file-comparison
calling-convention
disclosure
sudzc
wise
routedevent
nsviewanimation
firefox-5
user-friendly
iweb
simpletest
temporal-database
lzh

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile