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

JaCoCo coverage report not displayed in Sonarqube 5.3
How to change user in hdfs using sparkSubmit in java
What is the difference between Files.walk.filter and Files.find?
Java: Filtering Panels like a JTable
Auto Extracting XPATH of all html elements using JAVA
How to convert em dash in java
Is Java's import keyword for source files or binary files?
PostgreSQL function to insert into tables.. i need it to return specific values on errors and on specific if conditions
Have to click twice to get number, Java program not working as intended
Eclipse Java Compiler Errors
JSPExpression Language reading value from bean
Switch case not returning string
Get selected cell object in datatable using different lists for each column [duplicate]
Trouble getting a Java Applet program to open in my browser
NoNodeAvailableException[None of the configured nodes are available] in Elasticsearch 5.1.2
SPRING #RestController: Passing Cookie to a Service from Every Method

Categories

HOME
ms-access
magnific-popup
isabelle
gremlin
plone
stock
hp-exstream
relayjs
read-eval-print-loop
infragistics
packages
jpeg
ezpublish
constraint-programming
android-youtube-api
volttron
modx-revolution
event-handling
pugjs
apache-metamodel
evopdf
ab-testing
django-cms
visual-composer
facebook-instant-articles
sylius
strncpy
saas
google-cloud-nl
yadcf
galsim
scorm2004
google-qpx-express-api
windows-dev-center
mpmediaquery
restlet
automake
libraries
leading-zero
trim
ws-security
spring-security-kerberos
import-from-excel
scorm
scrollable
chain-builder
pubmed
celery-task
tropo
statsd
nstextview
carrot
pg-dump
windows-iot-core-10
qtwebview
sonarlint-vs
vtigercrm
blacklist
parallel-data-warehouse
elements
azure-virtual-network
thrust
mongocsharpdriver
plottable.js
skype4py
ready-api
master-slave
hover-over
revolution-r
two-factor-authentication
phpcas
uid
kendonumerictextbox
t4mvc
python-3.2
ruby-2.2
pundit
sframe
responsive-images
appfabric-cache
remobjects
umbraco6
clicktag
asp.net-web-api-odata
zend-route
heisenbug
p4java
wp-query
braille
dealloc
ccss
easy-install
rabl
orchardcms-1.7
angularjs-controller
browser-detection
sublist
errai
enter
removeclass
transactionscope
subgurim-maps
rfc1123
sudzc
gamequery
brewmp
caching-application-block
ntvdm.exe

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