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

Gobblin MR Job throws Invalid path ' ' Exception
Maven build failure : could not transfer metadata
How to proccess/read java objects from xmlrpc received by a server
Trying to input values into a 2d array and print it, code not running
Popup DatePicker using DialogFragment and saving selection to TextView or EditText
Write out all Spring Boot metrics to Stdout or aggregator?
Passing string from load operation to Pig UDF
Android programmatically update application when a new version is available in api 24 and api 25
Calendar Event via Intent does not set Start Date and End Date
Combining two accounts [closed]
Casting list of objects to list of maps in java [duplicate]
JComboBox to display multiple lines of text
RotatedRect to Java Ellipse2D or similar - OpenCV
Need to restart Java method up to three times for username & password log-in system
How can I change default elements color android layout?
Guice Request-scoped injection without a servlet (in an RPC server)

Categories

HOME
client
bluetooth
clips
reserved
mfc
google-play
json.net
vmware
elm
electronics
jgroups
callback
cross-browser
echarts
azure-media-services
ios-charts
directx
slurm
session-variables
user-input
zebra-printers
clojurescript
undefined
textfield
custom-wordpress-pages
paging
dcevm
crosstab
h2db
vlsi
react-css-modules
tasklet
nouislider
microsoft-chart-controls
preg-match
uisplitview
create-table
ios5
dynamics-crm-2013
service-discovery
user-controls
awt
elmah
powershell-dsc
nomethoderror
galleria
cubic-spline
skeleton-css-boilerplate
scrollable
upstart
revapi
jedis
react-scripts
paxos
hendrix
fancybox-2
elgg
capacity
mako
cudafy.net
lync-client-sdk
color-profile
smart-table
gnome-shell-extensions
background-service
ableton-live
static-ip-address
home-directory
packagist
livequery
nessus
associative-array
pyke
browser-link
atk4
arcanist
appfabric-cache
vstest.console.exe
ami
sonarqube5.1.2
lustre
kineticjs
tween
gwidgets
c3
gadt
coldbox
javafx-webengine
flask-cors
fluentautomation
soundtouch
odata4j
excel-2003
orchardcms-1.7
free-variable
distutils
manchester-syntax
propertyeditor
jspinclude
online-compilation
postgresql-performance
gcj
genshi
infobox
tomcat-valve
yetanotherforum
propagation
mysql-error-1005
cxxtest
yslow
xsdobjectgen
castle-monorail
firefox4
mtj
project-hosting
msdev
document-conversion

Resources

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
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App