0

I have created a very basic gradle java project that prints "Hello Word". The main class builds and runs fine. When i try to run java -jar out\artifacts\helloWorld_jar\helloWorld.jar on the command line it gives me this error

Error: Could not find or load main class com.exmple.helloWorld
Caused by: java.lang.ClassNotFoundException: com.exmple.helloWorld

here is how my directory looks

|-gradle
|-.idea
|-build
|-gradle
|-META-INF
|-out
 | -artifacts
 | -helloWorld_jar
 | -helloWorld.jar
|-src
 |-main
  |-java
   |- com.exmple
    |-helloWorld
 |-test
|build.gradle
etc...

My build.gradle file looks like this:

plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}
jar{
    manifest{
        attributes "Main-Class": "com.exmple.helloWorld"
    }

}
dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}

test {
    useJUnitPlatform()
}

I have tried both single quotes and double quotes and it still runs an error. Any ideas on how to fix this? I have been trying for a few days now.

PS. example is spelt wrong, but its spelt exmple throughout

Jane
  • 17
  • 5

2 Answers2

1

If the full classname is com.exmpl.helloWorld, then the source directory structure should be:

|-src
 |-main
  |-java
   |-com
    |-exmpl
     |-helloWorld.java

not

|-src
 |-main
  |-java
   |-com.exmple
    |-helloWorld

Please read What does "Could not find or load main class" mean? for more information. (It explains in depth the many things that can cause this problem.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

It is looking like you have a typo in the path that you are passing:

Error: Could not find or load main class com.exmple.helloWorld Caused by: java.lang.ClassNotFoundException: com.exmple.helloWorld"

An A is missing from example package

AdrianMirica
  • 45
  • 1
  • 6
  • it is spelt exmple for everything. I know it is spelt wrong (oops). I have checked the package and the path and everything is exmple – Jane Feb 09 '22 at 12:29