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 to delete dynamic table elements in jsp? What should I write in my controller?
Java Gui layouts and positions
Send a file to REST API in java
How to double the size of a queue in java
Scala - Spark Streaming Keeps Throwing “StreamingContext Not Serializable” Error
org.apache.kafka.common.errors.UnknownTopicOrPartitionException: This server does not host this topic-partition
How Can I create a new Directory with the last element of the linkedList for each line of the file
about java KeyStore load and store, when keyStore.load() again, the format of certificates in keyStore will be changed
Get all nodes with the same name in different levels
ApplicationHttpRequest is doubling the parameters when it's parsing the request parameters
Recursive pathing method
Unsatisfied dependency running a Test in Spring Boot
Maven Invoker Skipping Test in Goals
Creating new .PSF file programmatically in java for particular Tag name
Mocking final static void method using powermock and return some values based on the arguments
Adding Text on each bar of jfreechart bar graph [closed]

Categories

HOME
multithreading
proxy
omnet++
single-sign-on
push-notification
alfresco
kalman-filter
infragistics
swagger-ui
qore
append
facebook-messenger-bot
volttron
qt-creator
circular-dependency
visual-studio-cordova
workload-scheduler
crystal-reports-2008
object-detection
scriptcs
opencover
physics-engine
zurb-foundation-6
format-specifiers
kannel
windows-error-reporting
ping
assistant
buck
objectanimator
tinymce-4
preg-match
semantic-versioning
oracle-fusion-middleware
scorm2004
avcapturesession
wallpaper
xcode-extension
hot-module-replacement
uft-api
document.write
temporary-files
azure-ml
user-controls
bootstrap-dialog
cubic-spline
angular-resource
long-polling
filepicker
revapi
typescript1.8
nodebb
vtigercrm
angular-strap
heidisql
removechild
libpng
ctest
mathematica-frontend
fuzzy-search
testng-dataprovider
phpcas
browser-link
cannon.js
jmeter-maven-plugin
wordml
spidermonkey
security-testing
ionic
php-ci
graph-drawing
apache-commons-fileupload
sniffer
ami
bridge.net
sorl-thumbnail
xcode-6.2
googlemock
reactfx
xceed-datagrid
jboss-weld
ccss
web2py-modules
random-seed
type-equivalence
quickdialog
rdoc
propertyeditor
multipage
nsdatecomponents
gnu-prolog
xdomainrequest
netbeans-7.1
locate
jquery-ui-droppable
nsviewanimation
bespin
database-management
yagni
anti-piracy
noscript

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App