Theo
Theo

Reputation: 3149

Gradle - Could not find or load main class src.main.java.Test

I have the following folder structure and want to play with gradle testing.

Eclipse Image

Before starting any tests I want to see what tasks are available.

------------------------------------------------------------
 All tasks runnable from root project
------------------------------------------------------------

Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that   
depend on it.
buildNeeded - Assembles and tests this project and all projects it depends   
on.
classes - Assembles main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles test classes.

Build Setup tasks
-----------------
init - Initializes a new Gradle build. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]

Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root   
project 'GradleTesting'.
components - Displays the components produced by root project   
'GradleTesting'. [incubating]
dependencies - Displays all dependencies declared in root project   
'GradleTesting'.
dependencyInsight - Displays the insight into a specific dependency in root    
project 'GradleTesting'.
help - Displays a help message.
model - Displays the configuration model of root project 'GradleTesting'.    
[incubating]
projects - Displays the sub-projects of root project 'GradleTesting'.
properties - Displays the properties of root project 'GradleTesting'.
tasks - Displays the tasks runnable from root project 'GradleTesting'.

Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.

Other tasks
-----------
execute

So I know that there isn't any problect with gradle. Now I write this code in my build.gradle file.

apply plugin: 'java'

task execute(type: JavaExec) {
//This line throws me an exception.
main = "src.main.java.Test"
classpath = sourceSets.main.runtimeClasspath

}

And get this message.

$ gradle execute
:compileJava
:processResources UP-TO-DATE
:classes
:executeError: Could not find or load main class src.main.java.Test
 FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':execute'.
> Process 'command 'C:\Program Files\Java\jdk1.8.0_45\bin\java.exe''   
finished with non-zero exit value 1

Any ideas how to fix this error?

Thanks.

Upvotes: 1

Views: 1895

Answers (1)

Patrice M.
Patrice M.

Reputation: 4319

'src/main/java' is the file system path to the java files. The package names start here.

In your case there is no package name, the fully-qualified class name is "Test" so, change your build.gradle to:

apply plugin: 'java'

task execute(type: JavaExec) {
    main = 'Test'
    classpath = sourceSets.main.runtimeClasspath
}

Upvotes: 2

Related Questions