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

How can I send parameters to methods using Maven/testng? [duplicate]
Setting environment variables and path in MacOS 10.11.6
Replace a pattern in a long String. Used replace method in a loop, but every time it will replace the first occurrence
java initialize object array
Add an element to an array of object
Are all instance variables declared outside functions?
Trying to add dependency to Atlassian pom.xml using IntelliJ
Dependency not found in Spring Boot project
RadioButton returns false after first run [closed]
EJB #PostConstruct not called
Interactive Brokers orders synched in both Gateway and TWS?
Whole JSONObject sent as JSON key using HttpURLConnection in Android
How to bypass the hosts file when doing an nslookup in Java using InetAddress
What are possible causes of ErrorMessage: Dynamic Backend Host Not Specified?
Grid does not follow “Platform” requirements during test run
What is the scope of a static object in a web application

Categories

HOME
debugging
nullpointerexception
raspberry-pi
syntax
rsyslog
framework7
analysis
yarn
rdf
icloud
medical
primary-key
vifm
here-api
modelica
size
visual-studio-2005
iolanguage
serverless-framework
clearcase-ucm
lombok
django-simple-history
after-effects
predix
swiftlint
wijmo
windows-error-reporting
gitignore
file-rename
column-family
accelerate-framework
normal-distribution
mapdb
picasso
revolution-slider
dosbox
hot-module-replacement
gesture
ssjs
squib
hp-ux
fedex
sql-server-agent
ibpy
checkboxlist
babel-core
komodoedit
dds
hendrix
integrity
aurelia-validation
darcs
sqlclient
media-player
r-forge
qtwebview
spring-mongodb
gridpane
elements
phishing
underscore.js-templating
asp.net-4.5
websitepanel
hover-over
fadeout
bluemix-app-scan
pagerank
pyke
rtbkit
xna-4.0
qtableview
sdhc
marching-cubes
collapse
twirl
event-bubbling
key-management
bridge.net
npapi
tt-news
device-manager
starcluster
yorick
dealloc
resty-gwt
inbox
dataservice
google-email-migration
first-responder
getmessage
osql
enter
ohm
punbb
netdna-api
fireworks
spyware
f#-powerpack
locationlistener
disclosure
site.master
google-friend-connect
paster
gallio
perfect-hash
windows-live-messenger
misv

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