[英]How do I run a main method in a Java Gradle project from command line?
I have a Gradle project (say, MyProject
) that has a main method that I only want to use to run a small Java app.我有一个 Gradle 项目(比如
MyProject
),它有一个 main 方法,我只想用它来运行一个小型 Java 应用程序。 Say, the script is in a class and package as follows:说,脚本在一个类和包中,如下所示:
MyScript.Java
脚本语言
package com.car.ant;
import com.3rdparty.library.LibraryException;
public class MyScript {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
I build the project with $ ./gradlew clean build
, no problem.我用
$ ./gradlew clean build
项目,没问题。 I am trying to run this via command line, but when I do, I get an Exception when trying to run:我正在尝试通过命令行运行它,但是当我这样做时,尝试运行时出现异常:
$ pwd
~/workspace/MyProject/build/classes/java/main
$ java com.car.ant.MyScript
Exception in thread "main" java.lang.NoClassDefFoundError: com/3rdparty/library/exception/LibraryException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
... 7 more
This script is not the main part of the app, but I just want it in there to run every once in a while, and I'd like to figure out how to run it by itself command line.这个脚本不是应用程序的主要部分,但我只是希望它在那里每隔一段时间运行一次,我想弄清楚如何通过自己的命令行运行它。 Is there a way to do this without importing anymore libraries?
有没有办法在不导入更多库的情况下做到这一点? I've seen this done with one-jar, but I thought I could do this without one-jar.
我见过用一罐完成的,但我认为不用一罐也能做到。
UPDATE:
更新:
Tried to add a classpath after reading this , but now I'm getting a different error:阅读本文后尝试添加类路径,但现在我遇到了不同的错误:
$ java -cp com/3rdparty/library/exception/LibraryException com.car.ant.MyScript
Error: Could not find or load main class com.car.ant.MyScript
You can read about how to do this at the Gradle Docs .您可以在Gradle Docs阅读有关如何执行此操作的信息。
apply plugin: 'application'
mainClassName = "com.mycompany.myapplication.MyScript"
applicationDefaultJvmArgs = [
"-Djava.util.logging.config.file=src/main/resources/logging.properties"
]
Then simply use gradlew run
to run your application.然后只需使用
gradlew run
来运行您的应用程序。
As an added benefit, this plugin helps you to package your app for distribution.作为一个额外的好处,这个插件可以帮助你打包你的应用程序以进行分发。 Use the
gradle distZip
command for that. gradle distZip
使用gradle distZip
命令。
Try this.尝试这个。
plugins {
id 'java-library'
id 'application'
}
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
/*
sourceCompatibility is "Java version compatibility to use when compiling Java source."
targetCompatibility is "Java version to generate classes for."
*/
repositories {
jcenter()
}
dependencies {
}
//To run the class name from command line using gradle run
mainClassName = 'com.sam.home.HelloWorld'
//To create a runnable jar in GradleJavaProject\build\libs then java -jar GradleJavaProject.jar
jar {
manifest {
attributes 'Main-Class': 'com.sam.home.HelloWorld'
}
}
//Beyond this line evertyhing is optional
//Alternatively you can run it as a task too gradle -q runWithJavaExec
task runWithJavaExec(type: JavaExec) {
group = "Execution"
description = "Run the main class with JavaExecTask"
classpath = sourceSets.main.runtimeClasspath
main = 'com.sam.home.HelloWorld'
}
wrapper {
gradleVersion = '5.5'
distributionType = Wrapper.DistributionType.BIN
}
In command line type either gradle run or gradle -q runWithJavaExec在命令行中输入 gradle run 或 gradle -q runWithJavaExec
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.