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

Autowire a Spring service in JUnit test
JBoss AS 7 and MySQL - Can't start server
Java - JPanel vs Canvas
Java: How to remove duplicate string array from ArrayList?
Spring Boot test class wont inject beans
Using unique variable names in Xtend loop (Code Generation)
Using Scanner.in with java Applet
Sencha GXT themebuilder 3.1.0 Beta does not work with Sencha Gxt 3.1.0
AmazonClientException: Data read has a different length than the expected
SpringMVC: how to get the value of #RequestMapping in the function
convert java.io.inputstream to java.sql.blob
androidstudio constructor error
NaN Error w/ Quadratic Equation Calculator
Java - Use binarySearch to find # of occurrences of certain element in array, without If
Injecting EJB in spring
Setting request body on JAX-RS 2.0 Client API With RestEasy

Categories

HOME
sendgrid
vim
openlayers
cookies
zeromq
nullpointerexception
dot
amazon-ecs
youtube-api-v3
umd
ebean
fingerprint
kibana-4
fancybox-3
spring-xd
postgres-xl
commonmark
ups
circuit
django-simple-history
reactive-cocoa
emulator
mustache.php
php-openssl
hammerspoon
h2db
opennlp
su
centos6.5
fluentvalidation
xacml
wtx
kendo-datasource
librato
instant-messaging
sql-server-2012-express
react-chartjs
binary-data
caret
azure-sql-database
google-api-nodejs-client
devextreme
serve
az-application-insights
reportbuilder
impersonation
splice
slick-3.0
galleria
outlook-2013
dds
paxos
ionicons
gabor-filter
typed-lambda-calculus
knpmenubundle
webdriverjs
setuptools
static-ip-address
hill-climbing
independentsoft
phpcas
jms2
intel-fortran
collapse
comobject
citrus-pay
purge
quicklisp
sysinternals
vstest.console.exe
mmc
web-controls
clipperlib
jsapi
javax.mail
bigbluebutton
geonetwork
tween
javafx-webengine
asp.net-web-api-odata
gulp-less
starcluster
expected-exception
doskey
flexmojos
uv-mapping
c18
cos
chronoforms
delphi-6
chuck
pysimplesoap
cascalog
appconkit
nsmanagedobject
mercurial-server
infobox
f#-powerpack
xtype
table-footer
file-comparison
ecl
calling-convention
actionview
gallio
revisions
adrotator

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