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

Best way to update and notify by service a JSON get request
HttpUrlConnection issue from Eclipse and windows 10
How to POST a document with manual id in MongoDB database from Spring hypermedia-based RESTful front end?
android opencv camera app hanging
JDK installers not working on my Yosemite
Java Continuous adding of two small numbers loses accuracy [duplicate]
Which should be preferred of Excel/csv for data driven test in Selenium?
Collision detection with KeyListener and scrolling background
java.lang.NullPointerException from org.apache.commons.beanutils.PropertyUtilsBean after migrating to TC7
Binary search code in java won't run [closed]
can we implement Spring Autowiring in Java Standalone Application (Swing)
Netbeans run particular unit test with resource
Exposing properties for binding
Stuck threads in weblogic classloader
java.lang.OutOfMemoryError: GC overhead limit exceeded [duplicate]
NoClassDefFoundError eventhough class in in same folder

Categories

HOME
java
ionic-framework
coq
zeromq
activiti
gremlin
heroku
tinymce
at-command
frameworks
ckeditor
django-imagekit
directx
export-to-csv
spring-tool-suite
phaser
orchardcms
messages
dtrace
ups
jsprit
clearcase-ucm
lombok
footer
aurigma
text-rendering
excel-vba-mac
tokenize
graphicsmagick
excel-2007
extjs5
language-agnostic
hammerspoon
status
bootstrap-material-design
maxmind
libssl
subdomains
primitive
gsoap
directx-10
noraui
calibre
webtest
repo
html5-fullscreen
main
gesture
occlusion
sqlite2
arena-simulation
eclipse-gef
apple-news
auto-update
celery-task
svmlight
drupal-6
csound
worker-thread
blacklist
quartz-composer
nested-sets
jquery-filter
visual-c++-2008
libpng
url-masking
impresspages
bstr
contact-list
sigabrt
testng-dataprovider
sdf
ptrace
browser-link
unity-networking
xna-4.0
teamcity-8.0
django-unittest
angular-leaflet-directive
varargs
web-controls
proj4js
bridge.net
oxwall
websocket4net
neolane
google-reader
sgen
jubula
android-2.2-froyo
didselectrowatindexpath
app42
centos5
gnu-smalltalk
inbox
web2py-modules
seed
distutils
e4x
jquery-knob
appconkit
nsmanagedobject
qt-jambi
zpt
genshi
javax.script
appender
mongomapper
digest-authentication
preference
mirah
data-driven
swing-app-framework
aquaticprime
private-members
paul-graham

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