9

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?

Community
  • 1
  • 1
elect
  • 6,765
  • 10
  • 53
  • 119
  • could you check, whether your jar contains valid manifest file with the MainClass attribute and your Main class is provided in this jar? – Stanislav Oct 22 '15 at 07:44
  • Sure, `manifest` seems correct and the `Main.java` is there – elect Oct 22 '15 at 07:54
  • 1
    Then check, whether your Main.java file has a package declaration in it, like `package test;`. Without it, the class will not be found – Stanislav Oct 22 '15 at 08:11
  • Yep, it has, anyway the ide would have triggered that before. – elect Oct 22 '15 at 08:31
  • 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 Oct 22 '15 at 08:41
  • just added previews comment as an answer – Stanislav Oct 22 '15 at 08:50
  • 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? – tddmonkey Oct 22 '15 at 09:10
  • 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? – elect Oct 22 '15 at 09:13

2 Answers2

3

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.

Stanislav
  • 27,441
  • 9
  • 87
  • 82
  • Should I just give `-cp folderContainingDependencyJars`, shouldn't I? Because it doesn't work yet – elect Oct 22 '15 at 08:55
  • @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 – Stanislav Oct 22 '15 at 09:00
  • Uhm, ok, so what shall I do? Copy all my dependencies in the same folder of my jar? – elect Oct 22 '15 at 09:05
  • 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 Oct 22 '15 at 09:14
  • @elect could you please provide the command you are trying to run? – Stanislav Oct 22 '15 at 10:25
  • Sure, `PS C:\Users\GBarbieri\Documents\NetBeansProjects\jgli\build\libs> java -cp .\jgli.jar; C:\Users\GBarbieri\Downloads\joga mp-all-platforms\jogamp-all-platforms\jar\* test.Main` – elect Oct 22 '15 at 10:36
  • @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 – Stanislav Oct 22 '15 at 10:41
  • 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\build\libs\jgli.jar!\textures\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? – elect Oct 22 '15 at 11:51
  • 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 – Stanislav Oct 22 '15 at 12:02
  • 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 ` – elect Oct 23 '15 at 16:16
  • @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? – Stanislav Oct 23 '15 at 16:24
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

Beat Jost
  • 51
  • 4
  • `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. – Stanislav Oct 22 '15 at 08:18