Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix classpath in gradle generated startScripts

Tags:

gradle

I am building a simple application which starts well using gradle wrapper.

Now I would like to start it using a shell script. I am trying to use gradle startScripts task provided by Application plugin to generate such a script.

Here are the commands I tried:

n@laptop - ./gradlew clean build
BUILD SUCCESSFUL in 1s
9 actionable tasks: 9 executed

n@laptop - ./build/scripts/simple
Error: Could not find or load main class Main
Caused by: java.lang.ClassNotFoundException: Main

Here is my build.gradle

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'application'

repositories {
    mavenCentral()
}

sourceCompatibility = 1.11
targetCompatibility = 1.11

application {
    mainClassName = 'Main'
}

dependencies {
    compile("org.apache.commons:commons-io:1.3.2")
    compile("org.apache.commons:commons-io:1.3.2")
    compile("javax.validation:validation-api:2.0.0.Final")
    compile("com.fasterxml.jackson.core:jackson-annotations:2.2.1")
    compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.9.8")
    compile("org.hibernate:hibernate-validator:6.0.16.Final")
    compile("javax.el:javax.el-api:3.0.0")
    compile("org.glassfish:javax.el:3.0.0")
    compile("com.beust:jcommander:1.72")

    testCompile ("junit:junit:4.12")

    compileOnly "org.projectlombok:lombok:1.18.8"

    annotationProcessor 'org.projectlombok:lombok:1.18.8'
}

like image 440
user3173237 Avatar asked Sep 13 '25 12:09

user3173237


1 Answers

You cannot run the generated script from Gradle’s build directory directly. The script is only generated there but meant to be used as part of one of the built distribution archives under build/distributions/. For example (assuming that your project is called simple and we’re on a Unix system):

# create a directory where we will install the built ZIP distribution
mkdir installdir
# unzip the ZIP distribution to the new install directory
unzip build/distributions/simple.zip -d installdir
# run the application using the generated script from the distribution
installdir/simple/bin/simple

For reference, here’s what the install directory will look like for your build (with the generated start scripts in the bin directory):

installdir/simple/
├── bin
│   ├── simple
│   └── simple.bat
└── lib
    ├── classmate-1.3.4.jar
    ├── commons-io-1.3.2.jar
    ├── simple.jar
    ├── hibernate-validator-6.0.16.Final.jar
    ├── jackson-annotations-2.9.0.jar
    ├── jackson-core-2.9.8.jar
    ├── jackson-databind-2.9.8.jar
    ├── jackson-datatype-jsr310-2.9.8.jar
    ├── javax.el-3.0.0.jar
    ├── javax.el-api-3.0.0.jar
    ├── jboss-logging-3.3.2.Final.jar
    ├── jcommander-1.72.jar
    └── validation-api-2.0.1.Final.jar

Alternatively, you can run your program directly from Gradle using the run task of the application plugin:

./gradlew run
like image 131
Chriki Avatar answered Sep 16 '25 01:09

Chriki