Gradle, error could not find or load main class 'test.Main'

elect picture elect · Oct 22, 2015 · Viewed 11.4k times · Source

I implemented Gradle on one of my projects. I use Netbeans 8.02 with the gradle plugin.

Structure is as it should be, sources are located under jgli/src/main/java/, resources under jgli/src/main/resources/

The Main class is jgli/src/main/java/test/Main.java

If I run it through the ide, it runs on windows, it crashes on linux. That's why I am trying to run it through the console right now:

java -jar jgli.jar

But I keep getting:

Error could not find or load main class 'test.Main'

This is my build.gradle

apply plugin: 'java'

sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

// NetBeans will automatically add "run" and "debug" tasks relying on the
// "mainClass" property. You may however define the property prior executing
// tasks by passing a "-PmainClass=<QUALIFIED_CLASS_NAME>" argument.
//
// Note however, that you may define your own "run" and "debug" task if you
// prefer. In this case NetBeans will not add these tasks but you may rely on
// your own implementation.
if (!hasProperty('mainClass')) {
    ext.mainClass = ''
}

repositories {
    mavenCentral()
    // You may define additional repositories, or even remove "mavenCentral()".
    // Read more about repositories here:
    //   http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:repositories
}

dependencies {
    // TODO: Add dependencies here ...
    // You can read more about how to add dependency 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'

    compile 'org.jogamp.jogl:jogl-all-main:2.3.2'
    compile 'org.jogamp.jogl:nativewindow-main:2.3.2'
    compile 'org.jogamp.jogl:newt-main:2.3.2'
    compile 'org.jogamp.gluegen:gluegen-rt-main:2.3.2'
}

jar {
    manifest {
        attributes 'Main-Class': 'test.Main'
    }
}

I just modified the dependecies section and added the manifest part.

I tried to add 'test.Main' to the ext.mainClass, but it changed nothing..

Here you can download the jar.

This is my MANIFEST.MF:

Manifest-Version: 1.0
Main-Class: test.Main

Main.class is properly located inside the jar. Main.java has the package declaration package test;

Tried also

apply plugin: 'application'

mainClassName = 'test.Main'

without success..

What am I missing?

Answer

Stanislav picture Stanislav · Oct 22, 2015

I've just found your git repository and sources of your Main class, seems that it implements some interface, which is provided in your dependencies:

import com.jogamp.newt.event.KeyListener;
import com.jogamp.opengl.util.GLEventListener;

public class Main implements GLEventListener, KeyListener {
  ...

Which are from some of your dependencies libs:

compile 'org.jogamp.jogl:jogl-all-main:2.3.2'
compile 'org.jogamp.jogl:nativewindow-main:2.3.2'
compile 'org.jogamp.jogl:newt-main:2.3.2'
compile 'org.jogamp.gluegen:gluegen-rt-main:2.3.2'

In that case, your dependencies must be in your class path, while you running your Main class. Try to provide -cp option to point the path, where your dependecies could be found.