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

JBoss 6.3.3 EAP closing datasource before application using datasource is finished unloading
How to make sure system properties are set before a SpringJUnit4ClassRunner test is run [duplicate]
Vaadin connect DB as grid backend
Use Dynamic key for hashMap in Thymeleaf, along with attribute
Google Drive Android API Authentication Problems
How does a null character (\u0000) end up in a string?
Why print function is not printing when invoked with java -jar? [duplicate]
Windows reserved name “c​on” workaround / hack
Retrofit - Android - Expected BEGIN_ARRAY but was BEGIN_OBJECT [duplicate]
Share the same data with different processes [duplicate]
Know when a different class is exited
Spring Boot Filter.init() methods are being invoked on first request
Switch statement returning wrong [duplicate]
Creating a project with just POJO beans
Access computer hardware using applet in web browser
Ant build does not include Android support library jar

Categories

HOME
listview
isabelle
kde
session
read-eval-print-loop
infragistics
survey
disassembler
azure-media-services
constraint-programming
podio
vifm
fancybox-3
u-sql
n-gram
ups
iron-router
pc
nas
amazonsellercentral
reverse-proxy
physics-engine
kvc
excel-2007
captiveportal
dxf
emgucv
angular2-aot
centos6.5
chromebook
fgetcsv
elasticsearch-ruby
buck
compatibility
siesta-swift
http-get
code-contracts
caret
azure-sql-database
rotational-matrices
google-api-nodejs-client
nssegmentedcontrol
appfabric
neuroscience
libvpx
code-search-engine
squib
nand2tetris
fakeiteasy
turbogears
flashair
xml-documentation
chain-builder
jedis
paxos
python-webbrowser
dotnetzip
composite-key
dtexec
capacity
crosswalk-runtime
specrun
wdf
removechild
multi-level
mikroc
gstreamer-0.10
prettytensor
testng-dataprovider
bgp
ios4
revolution-r
qtableview
execute
lib.web.mvc
eclipse-clp
retina
security-testing
muse
map-projections
microbenchmark
sniffer
dundas
angular-local-storage
dereference
skos
e10s
clicktag
camanjs
android-imagebutton
rdtsc
socketexception
issuu
google-reader
eol
resty-gwt
message-driven-bean
quantlib-swig
ruby-datamapper
dotnetnuke-5
ocunit
referrer
cascalog
zpt
perfect-hash
webkit.net
sef
exchange-server-2003
data-driven
uimenucontroller

Resources

Encrypt Message