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

Android: Fragment is not attached to Activity error
Running job in hadoop- ERROR security.UserGroupInformation
Obtain specific part of URL
Spring - Embdedded H2 Test Db
How would I display an Image, and overlay a button in Java? [duplicate]
How to change textview text from another activity on button click in android studio?
Three-level nesting in elasticsearch
InvalidSelectorException - Selenium Webdriver support to recognize accent letters
Form validation with Spring Security login page
Python - IPython Notebook fails to find the Java executable
JBehave reuse steps from dependent projects
Spring boot + JPA - SharedEntityManagerCreator opens sessions at startup
Java Two-Dim Array
JAXB XML Required Field Validation
How to handle Activemq's max frame size exception with failover transport
Build fails after updating to gradle 3.3

Categories

HOME
google-chrome-extension
deployment
netbeans
heroku
google-api-php-client
plot
react-redux
binary-tree
fingerprint
constraint-programming
android-youtube-api
medical
u-sql
zend-framework3
ibm-odm
clojurescript
foselasticabundle
facebook-page
pugjs
connection-string
code-review
google-pagespeed
cx-freeze
autoconf
language-agnostic
mmap
restful-authentication
jndi
h2db
pingfederate
uninstall
social-media
maxmind
uisplitview
winrt-xaml-toolkit
hybridauth
accelerate-framework
wallpaper
mmenu
catalog
jmonkeyengine
neuroscience
event-driven
az-application-insights
reportbuilder
unoconv
arena-simulation
heightmap
wptoolkit
executenonquery
smartcontracts
hibernate-tools
hls.js
return-value
csound
knockout-components
spring-cache
dwscript
minimization
setuptools
url-pattern
grails-tomcat-plugin
sts-springsourcetoolsuite
py2app
xml-attribute
firebaseui
ubuntu-10.04
make-install
dlna
itextpdf
cannon.js
wordml
ibaction
mono-embedding
streambase
c++03
system.reflection
typekit
android-imagebutton
java-metro-framework
preferences
project-planning
has-many-through
sgen
uitouch
phpthumb
hippomocks
java.nio.file
stxxl
viadeo
cdc
eclipse-memory-analyzer
comaddin
orchardcms-1.7
eventlistener
lcs
clipper
dbproviderfactories
bubble-chart
dotnethighcharts
hgsubversion
whoosh
libc++
gnustep
xmemcached
appendto
ecl
android-sdk-2.1
telerik-scheduler
web-application-design
getresponsestream
filtered-index
rescale
zend-tool
memory-size
libs
photoshop-cs4
swing-app-framework
temporal-database
text-coloring
post-build

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