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 can I write a JUnit-Test for custom Jackson Serializer?
TimerTask + multiThreading + java, not working for second execution
Error Apache Tomcat after deploy
File handling in Java - User Rights
error: cannot find symbol?
Android Wear Gestures (Version 1.4) in Apps, not just for Notifications?
Jackson: Deserialize JSON-Array in different attributes of an object
Getting returned wrong values, but reading in fine in system? Java
How can i add itemlistener to jcheckbox i added runtime?
java doubling characters/tripling characters
Accessing static variable from an object in memory
JAXB schemagen returns 1 as error code when processing class with Jackson attribute
Solving error for iterative program
Why cant i add items to a jComboBox through another jFrame?
JDBC Driver not found while try to create a connection
JxBrowser 6.1 JavaScript Java Bridge API is not working

Categories

HOME
ngrx
push-notification
google-play
cmd
yarn
sharepoint-designer
jgroups
android-youtube-api
cloudkit
communication
spring-tool-suite
n-gram
libtiff
transformation
angular-ui
pugjs
viewport
jtextfield
tapestry
jndi
cas
selectedindexchanged
url-scheme
tooltipster
elasticsearch-ruby
xacml
facebook-apps
atlassian-crucible
media-queries
ecto
twilio-api
create-table
cloud-code
restlet
serverside-rendering
leading-zero
preconditions
core-plot
password-encryption
outlook-api
angular2-meteor
wso2carbon
angular-resource
react-scripts
setter
cudafy.net
arrow-keys
mcafee
titanium-android
quartz-composer
nested-sets
wdf
file-writing
etsy
storekit
packagist
php-internals
oauth2client
rtbkit
lemon
t4mvc
0xdbe
unity3d-gui
pretty-print
gwidgets
typekit
qcodo
document-classification
flask-cors
hyprlinkr
monomac
html5-notifications
terminal-services
dir
blending
dataadapter
eventual-consistency
xamlparseexception
drools-planner
multipage
getmessage
ohm
psi
f#-powerpack
yetanotherforum
gamequery
routedevent
nsviewanimation

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile