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

BDD Cucumber reporting issue in Jenkins
In debug mode I can see extra array list elements and are initialized to zero
JavaFx ComboBox valueProperty().addListener(new ChangeListener<String>() progressively repeated
Skip list remove and print method in java
How to start new line after press keyboard key is “ENTER” in SWT table cell?
ECIES encryption/decryption Java
For JSONArray parsing: TRY inside LOOP or LOOP inside TRY?
InstanceAlreadyExistsException coming from kafka consumer
Pong Game in java
Unable to Configure log4j2(v2.5) using dynamically changing configuration read from properties file
PostgreSQL function taking too long to complete execution
Need help positioning in java
Creating an ArrayList that outputs into 2 types [closed]
Android studio - transformClassesWithDexForDebug error
Ading Double type values [duplicate]
Wicket - getlist() in List printing multiple times

Categories

HOME
openlayers
testng
google-api-php-client
syntax
xmpp
q
at-command
frameworks
datastax-java-driver
convolution
indesign
qore
portia
adfs
amazon-cloudformation
rascal
gz
task
percona
uitypeeditor
workload-scheduler
systemc
django-admin
ef-migrations
viewport
immutable.js
lcd
java-7
pingfederate
dbclient
jaxb2
nouislider
frame
mpmediaquery
file-format
adobe-premiere
gesture
hue
dism
gzipstream
wptoolkit
fault
azure-application-gateway
jlink
powercli
tizen-native-app
aurelia-validation
jxcore
diagnostics
linode
sonarlint-vs
eventkit
grails-tomcat-plugin
sts-springsourcetoolsuite
azure-virtual-network
transmitfile
jwplayer7
lttng
holder.js
packagist
qtableview
t4mvc
thredds
marching-cubes
django-unittest
kcachegrind
muse
sframe
myo
fluid-dynamics
census
sonarqube5.1.2
inmobi
java.util.concurrent
qcodo
code-access-security
openexr
concurrent-collections
cloud-connect
java.nio.file
mbr
monomac
openlaszlo
spring-io
tws
dataadapter
hungarian-algorithm
jelly
libstdc++
coderush
armcc
boost-filesystem
gnustep
xmemcached
table-footer
telerik-scheduler
paster
wise
compiler-specific
microsoft-virtualization
commodore
rtml

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