简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM