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

12,482

Solution 1

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.

Solution 2

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

Share:
12,482
elect
Author by

elect

https://github.com/elect86/ https://github.com/kotlin-graphics

Updated on July 26, 2022

Comments

  • elect
    elect almost 2 years

    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?

    • Stanislav
      Stanislav over 8 years
      could you check, whether your jar contains valid manifest file with the MainClass attribute and your Main class is provided in this jar?
    • elect
      elect over 8 years
      Sure, manifest seems correct and the Main.java is there
    • Stanislav
      Stanislav over 8 years
      Then check, whether your Main.java file has a package declaration in it, like package test;. Without it, the class will not be found
    • elect
      elect over 8 years
      Yep, it has, anyway the ide would have triggered that before.
    • Stanislav
      Stanislav over 8 years
      I've just found your git repo and sources of your Main class, seems that it implements some interface, which is provided in your dependencies. 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 with dependent jar
    • Stanislav
      Stanislav over 8 years
      just added previews comment as an answer
    • tddmonkey
      tddmonkey over 8 years
      I've grabbed your project, applied the application plugin and set mainClassName = 'test.Main' and it runs. It crashes (OSX), but that's what you want anyway right?
    • elect
      elect over 8 years
      Wait, for me it runs through NB only on windows. Therefore I am trying now by console. where did you run it from which os and how?
  • Stanislav
    Stanislav over 8 years
    mainClassName is used by application plugin, but not java. If you'll not apply this plugin, you'll get No such property exception, because this property will not be found.
  • elect
    elect over 8 years
    Should I just give -cp folderContainingDependencyJars, shouldn't I? Because it doesn't work yet
  • Stanislav
    Stanislav over 8 years
    @elect due to javadoc "When you use the -jar option, the specified JAR file is the source of all user classes, and other class path settings are ignored." that means, you can't run it with -jar option
  • elect
    elect over 8 years
    Uhm, ok, so what shall I do? Copy all my dependencies in the same folder of my jar?
  • Stanislav
    Stanislav over 8 years
    classpath could combine number of jars and directories, so it could be something like java -cp jgli.jar;/folder/to/deps/* test.Main and no need to provide MainClass attribute in your manifest
  • Stanislav
    Stanislav over 8 years
    @elect could you please provide the command you are trying to run?
  • elect
    elect over 8 years
    Sure, PS C:\Users\GBarbieri\Documents\NetBeansProjects\jgli\build\lib‌​s> java -cp .\jgli.jar; C:\Users\GBarbieri\Downloads\joga mp-all-platforms\jogamp-all-platforms\jar\* test.Main
  • Stanislav
    Stanislav over 8 years
    @elect seems, you have a whitespaces in your path, for exampl after ; or \joga mp-all-platforms` in that case you have to wtite it within "` like: java -cp ".\jgli.jar;C:\Users\GBarbieri\Downloads\joga mp-all-platforms\jogamp-all-platforms\jar\*" test.Main because in your case, the -cp value ends after the semicolon and path to lob folder is taken as a new parameter
  • elect
    elect over 8 years
    I had to space to get the tab suggestions. Anyway it is kind of strange, I remove the space but I still get the error, I have to go with the ". Now it runs, but I still get an exception, java.io.FileNotFoundException: file:\C:\Users\GBarbieri\Documents\NetBeansProjects\jgli\bui‌​ld\libs\jgli.jar!\te‌​xtures\ku eken7_rgb8_srgb.dds. Texture is located under jgli/src/main/resources/textures/ and I load it inside java with the path \textures\ku eken7_rgb8_srgb.dds, where am I wrong?
  • Stanislav
    Stanislav over 8 years
    here you have to take a look at the way you try read it. since it's inside an archive, you can't just use a File, but have to use a streams to read it
  • elect
    elect over 8 years
    Thanks, that worked on Windows. Do you know if on Linux I need to change anything in particular? Because this doesn't work elect@elect-desktop:~/NetBeansProjects/jgli/build/libs$ java -cp "jgli.jar;/home/elect/Downloads/jogamp-all-platforms/jar/*" test.Main Error: Could not find or load main class test.Main
  • Stanislav
    Stanislav over 8 years
    @elect I suppose, the reason is not that class test.Main was not found (commands are the same, may be just the privilegies differs), but was not loaded. As I can see, org.jogamp.jogl libs are depends on native libraries, that means, the may need some platform specific installation. May be you have to look for some platform specific questions about org.jogamp.jogl installation?