I've seen many people with "Could not find or load main class" errors when making jar files with Gradle. I have gone through many of the forums and have not found a solution that works for me yet. Following an online tutorial, I was able to set up a Gradle project in VSCode and add dependencies for a basic hello world program. Link to the tutorial I was following here: https://www.youtube.com/watch?v=BwdkyrnJQsg
This worked for me no problem but when I finished my much larger program, it doesn't find my Main.class file. Here's my build.gradle file:
plugins {
id 'java'
id 'java-library'
id 'application'
}
application
{
mainClass = 'Main'
}
java
{
sourceCompatibility = JavaVersion.VERSION_12
targetCompatibility = JavaVersion.VERSION_12
}
project.configurations.implementation.setCanBeResolved(true);
version = '1.0.0'
mainClassName = 'Main'
sourceSets
{
main
{
java
{
srcDirs 'src/main/java'
}
}
}
repositories
{
mavenCentral()
maven
{
url = 'https://licensespring-maven.s3.eu-central-1.amazonaws.com/'
}
}
dependencies
{
implementation 'com.licensespring:licensespring-license-client:2.1.0'
// https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java
implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '4.0.0-alpha-6'
// https://mvnrepository.com/artifact/net.harawata/appdirs
implementation group: 'net.harawata', name: 'appdirs', version: '1.2.0'
// https://mvnrepository.com/artifact/net.java.dev.jna/jna-platform
implementation group: 'net.java.dev.jna', name: 'jna-platform', version: '5.6.0'
// https://mvnrepository.com/artifact/net.java.dev.jna/jna
implementation group: 'net.java.dev.jna', name: 'jna', version: '5.6.0'
// https://mvnrepository.com/artifact/org.slf4j/slf4j-api
implementation group: 'org.slf4j', name: 'slf4j-api', version: '2.0.0-alpha1'
// https://mvnrepository.com/artifact/commons-io/commons-io
implementation group: 'commons-io', name: 'commons-io', version: '2.7'
}
jar
{
manifest
{
attributes(
"Main-Class": "$mainClassName",
"Class-Path": configurations.implementation.collect {it.name}.join(' ')
)
}
dependsOn('dependencies')
from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
}
In src/main/java/ I have my Main.java file with the main method along with all my other .java files. When I run gradlew clean build, my .class files show up under build/classes/java/main.
Just to clarify, when I run java -jar './build/libs/myApplication.jar' I get the Could not find or load main class Main error. Let me know if you need any more specifics.
UPDATE: Switched over to Intellij again and I am getting errors involving the Main Manifest Attribute. I checked the outputted jar and the Manifest file is indeed packaged and it does have a Main Manifest Attribute. PLEASE HELP ME SOMEONE!
Can you try giving source set and full package name for main?
It should look something like this:
sourceSets {
main {
java {
srcDirs 'src/main/java'
}
}
}
project.configurations.implementation.setCanBeResolved(true)
jar{
manifest {
attributes(
"Main-Class": "com.xyz.Main",
"Class-Path": configurations.implementation.collect { it.name }.join(' ')
)
}
dependsOn ('dependencies')
}
I had similar issue to create runnable jar. I could solve it with following example
Solution 1 : Create a fat jar with all dependencies.
task fatJar(type: Jar) {
manifest {attributes 'Main-Class': 'com.example.gradle.App'}
from {
configurations.compile.collect
{ it.isDirectory() ? it : zipTree(it) }
}with jar
}
Solution 2 : Create a runnable jar with dependencies copied to a directory and setting the class path
def dependsDir = "${buildDir}/libs/dependencies/"
task copyDependencies(type: Copy) {
from configurations.compile
into "${dependsDir}"
}
task createJar(dependsOn: copyDependencies, type: Jar) {
manifest {
attributes(
'Main-Class': 'com.example.gradle.App',
'Class-Path': configurations.compile.collect
{ 'dependencies/' + it.getName() }.join(' ')
)}
with jar
}
Read more on this with an example here
Related
I am pretty new to using Gradle. I am using Gradle-7.5.1. I just want a jar file created using Gradle that contains just the manifest file and the source files I wrote but why does the created jar contain a million other things in it?
My gradle.build file is as follows:
apply plugin: 'java'
apply plugin: 'application'
version = '1.0'
sourceSets {
main {
java {
srcDirs = ['src']
}
}
}
task fatJar(type: Jar) {
manifest {
attributes 'Manifest-Version': version,
'Class-Path': configurations.runtimeClasspath.files.collect { it.getName() }.join(' ')
}
baseName = 'MyProj'
from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
repositories {
mavenCentral()
}
dependencies {
implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25' < etc >
}
What I want in the MyProj-1.0 is:
META-INF (holding the Manifest.MF)
com (holding my src files)
What I get in the jar is:
assets
style
corejava
javax
..and a lot more such things
Thanks in advance for any pointers. As I have banged my head to the point of snapping it!
I am looking for javafx 10 or newer. I currently have javafx-sdk-11 and trying to make my programme a single runnable jar file, but apparently since javafx 11, that option isn't available anymore.
So I have to go to the terminal and type the following line to run it :
java --module-path /path/to/javafx/javafx-sdk-11.0.2 or another/lib --add-modules javafx.controls,javafx.fxml,javafx.graphics,javafx.web -jar /path/to/GUI_Music_Gen.jar
Since I can't find older versions of javafx available for download, I ask for your help. If anybody can help me, let me know. Thanks in advance.
Btw, I don't know if this will be an issue for compatibility, but I run macOS X.
I would recommend using dependency management like grade or maven to run JavaFX and Build a working Jar.
I can offer you this build.gradle for a working JavaFX project:
buildscript {
dependencies {
classpath group: 'de.dynamicfiles.projects.gradle.plugins', name: 'javafx-gradle-plugin', version: '8.7.0'
classpath 'org.openjfx:javafx:11'
}
repositories {
mavenLocal()
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
}
plugins {
id 'java'
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.8'
}
sourceCompatibility = 11
targetCompatibility = 11
repositories {
jcenter()
mavenCentral()
}
// configure here
mainClassName = "your.app.main"
publishing {
publications {
mavenAar(MavenPublication) {
from components.java
afterEvaluate {
artifact javadocJar
artifact sourcesJar
}
}
}
}
javafx {
version = "11"
modules = ['javafx.controls', 'javafx.fxml', 'javafx.graphics']
}
sourceSets {
main.java.srcDir "src/main/java"
main.resources.srcDir "src/main/resources"
}
dependencies {
api 'org.apache.commons:commons-math3:3.6.1'
implementation 'com.google.guava:guava:27.0.1-jre'
testImplementation 'junit:junit:4.12'
// use java fx just like a regular dependency :)
implementation 'org.openjfx:javafx:11'
compile group: 'org.openjfx', name: 'javafx', version: '11.0.2'
}
// important Configure your project
jar {
manifest {
attributes(
'Main-Class': 'your.app.main'
)
}
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}
compileJava {
doFirst {
options.compilerArgs = [
'--module-path', classpath.asPath,
'--add-modules', 'javafx.controls,javafx.fxml,javafx.graphics'
]
println options.compilerArgs
}
}
Just replace "your.project.main" with your actual main class and everything should run fine.
Also it is really important that your Main class does not extend from Application.
It should only Launch the Application.
After building my gradle jar within intellij, my project will run perfectly fine. If I try to then move that jar to my desktop and run it using the command "java -jar ProjectName.jar" it throws and error "Cannot find or load main class main"
I'm painfully aware that this question has been asked multiple times on this website as I've spent hours trying to fix it. Ever since I reinstalled Intellij I have been having this issue. I've tried making a fat jar and excluding META-INF, I tried creating a whole new project and creating a jar within that project. I also tried to use a recent iteration of this project which did run as a standalone and had no luck.
build.gradle
plugins {
id 'java'
}
group 'PantryDatabase'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile(
[group: 'com.google.cloud', name: 'google-cloud-bigquery', version: '1.74.0'],
[group: 'com.jfoenix', name: 'jfoenix', version: '9.0.8'],
[group: 'me.xdrop', name: 'fuzzywuzzy', version: '1.2.0']
)
}
jar {
manifest {
attributes 'Main-Class': "Main"
}
from configurations.runtime.collect { zipTree(it) }
}
task customFatJar(type: Jar) {
baseName = 'Pantry_Database'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
with jar
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
manifest {
attributes 'Main-Class': 'Main'
}
}
Class Files: https://imgur.com/gallery/HbGJPgr
Error Message: https://imgur.com/gallery/KkHNfas
I perform the first use of Dagger 2 in my life and I think I encounter some issues that are considered insignificant by expirienced users and thus omitted in their guides.
So I created a simple absolutely empty class that does nothing, marked its constructor with #Inject, then created an interface with #Component annotation with method return type of which is my empty class. Then I invoked gradlew build and Dagger actually generated files that it must have generated. But these files are in build/generated/source/apt/main/com/warden/bot/ directory of my IDEA project whilst source code is in src/main/java/com/warden/bot/ directory of the project, so I can't use Dagger generated code in my program. Should there be a special setup for Dagger before use and what do I do now?
UPD
My build.gradle file is:
plugins {
id "net.ltgt.apt" version "0.12"
}
version '2.0'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.dagger:dagger:2.11'
apt 'com.google.dagger:dagger-compiler:2.11'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
jar {
manifest {
attributes(
'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
'Main-Class': 'com.warden.bot.Main'
)
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
I have searching to solve this problem for two days and I did not find solution. Firstly I wanted to build fat-jar, then I gave up on that it looked more complicated so I started building normal executable jar. I am building some program inside NetBeans 8.2 with Gradle plugin(http://plugins.netbeans.org/plugin/44510/gradle-support)
My project structure is like this:
Project Structure
I am relatively new to Gradle I am using it less then one month. My build.gradle looks like this:
repositories {
mavenCentral()
}
dependencies {
// TODO: Add dependencies here ..
// // http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:how_to_declare_your_dependencies
// testCompile group: 'junit', name: 'junit', version: '4.10'
// https://mvnrepository.com/artifact/org.apache.poi/poi
compile group: 'org.apache.poi', name: 'poi', version: '3.16'
// https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml
compile group: 'org.apache.poi', name: 'poi-ooxml', version: '3.16'
// https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox
compile group: 'org.apache.pdfbox', name: 'pdfbox', version: '2.0.6'
}
apply plugin: 'java'
mainClassName = "paket.Glavna"
jar {
from {
(configurations.runtime).collect {
it.isDirectory() ? it : zipTree(it)
}
}
manifest {
attributes("Main-Class": "Glavna" )
}
}
Now, I cant get it working to make executable jar. I think i have traced problem that there isnt path to main class in jar but I dont know why it isnt adding it. Error i get is:
Error: Could not find or load main class DnevniIzvestajG-1.0.jar
jar runs with (inside Glavna class is main):
java -cp projectname.jar paket.Glavna
Also I have tried running
manifest-addition
but taht also didnt add link to main class.
Try adding a package to the class with main() method.
jar {
archiveName = 'Glavna.jar'
manifest {attributes 'Main-Class': 'paket.Glavna'}
from(configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }) {}
}
Building a fat jar like this is not preferable, try the Application plugin
I use below way to add all dependencies in .jar of gradle project.
build.gradle :-
jar {
manifest {
attributes(
'Main-Class': 'com.main.MainClass'
)
}
duplicatesStrategy = DuplicatesStrategy.INCLUDE
from { configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
}