无法找到或加载主类com.johnathanmarksmith.gradle.HelloWorld

时间:2015-10-22 20:30:18

标签: java gradle

我使用以下命令编译并运行helloworld jar文件,但出现错误

  

无法找到或加载主类   com.johnathanmarksmith.gradle.HelloWorld

有谁能告诉我如何解决它?

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

我的构建结果是成功的

: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

1 个答案:

答案 0 :(得分:3)

问题是您在项目的根目录下创建了不在src/main/java下的包文件夹。

应该是:

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

现在它应该运作良好。