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

Overriding Spring bean with using the parent properties
How to use ExecutorService shutdown in Android application
Make simple UI (with fragments/colums, smaller resolution screen and soft edges)
WAMP Server connection refuesed from Android Emulator
JSP dynamically change include page
Java: How to check if printer status
Javafx updating tableview on object change
Trouble deploying web service on Tomcat 8
Java EE 7 Webapp - Problems with REST-Services(json) after upgrading tika-parsers to 1.11
Eclipse: Java heap space, How to fix it?
How to return error from OFBiz SECA service
my code is not displaying a string stored in a variable [duplicate]
Is it possible to configure a Java web app to run with Apache Wink Server and JAX-RS 2.0?
java, A window with 2 buttons
Add Radio Button in List
iText Output Missing Unicode Characters

Categories

HOME
twitter
testing
image
oracle11g
azure-data-factory
plot
hp-exstream
fft
grep
filtering
cmd
graphql
fsm
installshield
cross-browser
echarts
flyway4
awesome-wm
tomcat6
here-api
fancybox-3
reveal.js
izpack
php-7.1
fortumo
circuit
iron-router
clearcase-ucm
ef-migrations
jsdoc
crystal-reports-2008
conemu
kryo
telerik-reporting
microsoft-sync-framework
javacv
fopen
fifo
log4js-node
phonegap
titanium-mobile
caret
protovis
textmate
form-data
language-concepts
espeak
angularjs-factory
lumberjack
awt
sfdc
dynamic-reports
http-live-streaming
checkboxlist
filepicker
ionicons
idisposable
recursive-datastructures
directory-structure
google-perftools
np-complete
ifs
imanage
dwscript
django-scheduler
minimization
crosswalk-runtime
topbeat
setuptools
url-pattern
angular-cache
mathematica-frontend
contact-list
pickadate
ptrace
two-factor-authentication
photobucket
wso2cloud
reactive-banana
cyclomatic-complexity
mutation-observers
retina
computer-algebra-systems
browser-bugs
dundas
c++03
google-style-guide
asp.net-dynamic-data
cctv
zend-route
eol
xceed-datagrid
jboss-weld
stxxl
mcts
gnu-smalltalk
flash-builder4.5
gridcontrol
comaddin
lcs
distutils
frameset
enter
cufon
qt-jambi
heartbeat
subgurim-maps
javax.script
revisions
nhibernate.search
aquaticprime

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