Gavin Niu
Gavin Niu

Reputation: 1335

Could not find or load main class com.johnathanmarksmith.gradle.HelloWorld

I use following commands to compile and run a helloworld jar file but got an error

Could not find or load main class com.johnathanmarksmith.gradle.HelloWorld

Can anyone tell me how to fix it?

mkdir runnablejar
cd runnablejar
mkdir -p src/main/java
mkdir -p src/main/resources
mkdir -p src/test/java
mkdir -p src/test/resources
mkdir -p com/johnathanmarksmith/gradle
vi com/johnathanmarksmith/gradle/HelloWorld.java
(insert)
package com.johnathanmarksmith.gradle;
public class HelloWorld
{
    public static void main(String[] args) 
    { 
        System.out.println("Hello World!"); 
    } 
}
vi build.gradle
apply plugin: 'java' 

 jar { 
        baseName = 'smith' 
        version = '1.0' 
        manifest { 
                     attributes 'Main-Class': 'com.johnathanmarksmith.gradle.HelloWorld' } 
     }
gradle build
java -jar ./build/libs/smith-1.0.jar

My build result is success

:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar
:assemble
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build

BUILD SUCCESSFUL

Upvotes: 0

Views: 1155

Answers (1)

Opal
Opal

Reputation: 84864

The problem is that you created the package folders under root of the project not under src/main/java.

It should be:

mkdir -p src/main/java/com/johnathanmarksmith/gradle
vi src/main/java.com/johnathanmarksmith/gradle/HelloWorld.java

Now it should work well.

Upvotes: 3

Related Questions