Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 264
elect Avatar asked Oct 22 '15 07:10

elect


People also ask

Could not find or load main class main class?

Reasons to Occur Error The error generates because the JVM fails to load the main class or package name. There are some other reasons that generate the same error, as follows: The class has been declared in the wrong package. Dependencies missing from the CLASSPATH.

Could not find or load main class maven Error?

Wrong Class Name Remember that class names must be unique in Java. Furthermore, they are case sensitive... This will result in the "Could not find or load main class" error because class names are case sensitive.


2 Answers

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.

like image 171
Stanislav Avatar answered Sep 26 '22 08:09

Stanislav


Normally you set the main-class in your build file as follows:

mainClassName = 'package.MainClassName'

Do you pass it as Parameter? -> hasProperty Then you had to run your build something like: gradle -PmainClass=MyClass build

like image 36
Beat Jost Avatar answered Sep 22 '22 08:09

Beat Jost