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

Linked List remove
Update JTextArea to show progress
Repeating an effect on blocks in a world
What does the leak period means in sonarQube
Spring-Security with two authentication managers
Generate unique int whenever a new object is created
How to parse the Google search result in a specific date range?
How to list both Numbers and Contact in a single row
Spring Boot MVC/Rest controller and enum deserialization converter
Redraw/refresh layout after changing Activity's theme/style
Webview android application crashes with error “java.lang.NullPointerException”
Find unique element in an arraylist error
Mouse Motion Listener
Refresh a jTable from another window but another window is opning [closed]
ERROR: Cannot find symbol — How do I get my class to compile when it depends on a class in a subfolder?
Changing content of system file using Process output stream [Android]

Categories

HOME
ms-access
deployment
openmp
layout
mean-stack
ebean
yum
wamp
convolution
indesign
contact
dacpac
export-to-csv
quickbooks
google-apps-marketplace
event-handling
quickfix
code-review
textfield
lldb
clickonce
java-7
django-storage
status
pingfederate
uninstall
rst2pdf
publish
ejabberd-module
microsoft-chart-controls
react-chartjs
functor
airconsole
fog
copying
fusionpbx
wixsharp
appfabric
scaffold
web-mining
hot-module-replacement
azure-ml
greenrobot-eventbus
node-gyp
unixodbc
total-commander
zip4j
python-c-api
nstextview
elgg
libusb-win32
windows-iot-core-10
azure-sdk
httplistener
react-native-listview
pagedlist
hover-over
separator
teamcity-8.0
angular-leaflet-directive
fluid-dynamics
generic-programming
cisco-ios
javax.mail
typekit
android-nested-fragment
client-side-templating
neolane
article
doskey
mysqltuner
geos
ms-project-server-2010
tidy
soundtouch
pushbackinputstream
lcs
smtp-auth
clipper
distutils
angularjs-controller
word-processor
dsn
ticoredatasync
buildr
removeclass
punbb
cufon
custom-backend
eclipse-templates
heartbeat
f#-powerpack
xfbml
zend-translate
icanhaz.js
castle-monorail
fixed-width
premature-optimization
genealogy
3gp

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