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

Joining to ElementCollections to find at least one match
Retrieve Image From JLabel and store it in a variable in Java
how to create table in mysql with the contents of first row in excel in java dynamicallly
Communication with usbserialhost to Android
How to process audio(.3gp) files in android [closed]
Android: Setting a table row visible in the main java class
Using pairs of words from a dictionary with no letters in common, find a pair that maximizes the sum of the words' lengths
NoClassDefFoundError when using KyroNet
Inheriting different booleans from 2 classes
how to read math symbols
The type or namespace name 'DataSource' could not be found
2d Array with Rectangle() grid
Android- How to update data on another activity. Outside the one you're working on
Spring injection - custom annotation to initialise classes
Good free sqlite gui tool [closed]
Bean Validation in a Camel environment?

Categories

HOME
netsuite
kde
react-redux
amortized-analysis
jsrender
dxl
jpeg
fancybox-3
offline
modelica
google-cloud-spanner
php-7.1
remote-access
commonmark
percona
designer
saxon
tostring
fatal-error
nas
physics-engine
jndi
numerical-methods
uninstall
wtx
large-file-upload
atlassian-crucible
skia
preg-match
fabric8
framemaker
host
appfabric
espeak
event-driven
android-browser
vapor
dism
particles.js
hp-ux
galen
sfdc
avro4s
outlook-api
eclipse-gef
btrace
galleria
scorm
strptime
setter
knpmenubundle
iso8601
sonarlint-vs
word-vba-mac
time-and-attendance
abcpdf9
url-pattern
database-optimization
adxstudio-portals
pcf
flash-cs5
react-native-listview
superstack
rvest
yaws
pagerank
phpcas
separator
t4mvc
thredds
computer-algebra-systems
sortedlist
skos
system.reflection
gwidgets
typekit
preferences
tld
navigationservice
valueconverter
android-2.2-froyo
balanced-payments
dexterity
flash-builder4.5
random-seed
factory-method
broken-links
word-processor
multipage
armcc
tridion-worldserver
reddot
postgresql-performance
jquery-ui-layout
whoosh
window-management
sproutcore-2
exchange-server-2003
yagni
phonon
uiq3

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile