Why I can not compile JavaFX program using grade? - java

yesterday I created a JavaFX project and I don't know why but when I want to run the app with commands java says:
Gradle task : build
Command : java -jar "name_of_the_file.jar"
Error: Could not find or load main class com.example.HelloApplication
Caused by: java.lang.NoClassDefFoundError:javafx/application/Application
but I can run it using gradlew run
module-info.java file
module com.example{
requires javafx.controls;
requires javafx.fxml;
opens com.example ;
exports com.example;
}
Manifest.MF file
Manifest-Version: 1.0
Main-Class: com.example.HelloApplication
build.gradle file
plugins {
id 'java'
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.13'
id 'org.beryx.jlink' version '2.25.0'
}
group 'com.example'
version '1.0-SNAPSHOT'
jar{
manifest{
attributes ('Main-Class' : 'com.example.HelloApplication' )
}
}
repositories {
mavenCentral()
}
ext {
junitVersion = '5.8.2'
}
sourceCompatibility = '17'
targetCompatibility = '17'
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
application {
mainModule = 'com.example'
mainClass = 'com.example.jfx.HelloApplication'
}
javafx {
version = '17.0.2'
modules = ['javafx.controls', 'javafx.fxml']
}
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")
}
test {
useJUnitPlatform()
}
jlink {
imageZip = project.file("${buildDir}/distributions/app-${javafx.platform.classifier}.zip")
options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
launcher {
name = 'app'
}
}
jlinkZip {
group = 'distribution'
}
and project structure
project structure
I am using inteliij idea community and my operating system is windows
I really don't know what is wrong it seems all correct to me. Strange

Related

Invalid image reference registry when using jib to dockerize gradle project

I had a problem when dockerize the Gradle project.
running gradle jib
What went wrong:
Execution failed for task ':jib'.
Invalid image reference registry.hub.docker.com/myusername/${rootProject.name}, perhaps you should check that the reference is formatted correctly according to https://docs.docker.com/engine/reference/commandline/tag/#extended-description
For example, slash-separated name components cannot have uppercase letters
Root Module
setting.gradle
rootProject.name = 'myproject'
include 'discovery-service'
include 'client-service'
include 'notification-service'
include 'api-gateway'
build.gradle
jib {
var tag = 'latest'
from {
image = 'eclipse-temurin:17.0.4.1_1-jre'
}
to {
image = 'registry.hub.docker.com/myUsername/${rootProject.name}'
}
}
One of the service
plugins {
id 'java'
id 'org.springframework.boot' version '2.7.5'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}
group = 'com.leekify'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
jar {
baseName = 'client-service'
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
....
}
tasks.named('test') {
useJUnitPlatform()
}
setting.gradle
rootProject.name = 'client-service'
Use double-quotes instead of single quotes for the jib.to.image string so that Gradle evaluates the variable.

Gradle "Couldn't replace placeholder" on build

I'm having a JavaFX project configured with Gradle in IntelliJ.
Whenever I'm trying to run gradlew.bat clean build or simply the build task directly from IntelliJ, this is what I receive:
> Task :startScripts FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':startScripts'.
> Couldn't replace placeholder in E:\git\yuber\build\scripts\yuber
I'm also using the shadowJar plugin, which actually works when I'm trying to create a jar file and run it. And it works perfectly. Although, I'm not sure why it won't build. Looked everywhere for an answer, but seems that no one actually had this problem at this moment?
This is my build.gradle file:
plugins {
id 'java'
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.10'
id 'org.beryx.jlink' version '2.24.1'
id "com.github.johnrengelman.shadow" version "7.1.2"
}
group 'com.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
ext {
junitVersion = '5.8.2'
}
sourceCompatibility = '11'
targetCompatibility = '11'
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
application {
mainModule = 'com.example.yuber'
mainClass = 'com.example.yuber.Launcher'
}
javafx {
version = '11.0.2'
modules = ['javafx.controls', 'javafx.fxml', 'javafx.web']
}
dependencies {
implementation('org.controlsfx:controlsfx:11.1.0')
implementation('com.dlsc.formsfx:formsfx-core:11.3.2') {
exclude(group: 'org.openjfx')
}
implementation('net.synedra:validatorfx:0.1.13') {
exclude(group: 'org.openjfx')
}
implementation('org.kordamp.ikonli:ikonli-javafx:12.2.0')
implementation('org.kordamp.bootstrapfx:bootstrapfx-core:0.4.0')
implementation('eu.hansolo:tilesfx:11.48') {
exclude(group: 'org.openjfx')
}
implementation 'org.json:json:20220320'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.2.2'
implementation 'com.fasterxml.jackson.core:jackson-core:2.13.2'
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.13.2'
implementation 'junit:junit:4.13.1'
testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")
}
test {
useJUnitPlatform()
}
jlink {
imageZip = project.file("${buildDir}/distributions/app-${javafx.platform.classifier}.zip")
options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
launcher {
name = 'app'
}
}
jlinkZip {
group = 'distribution'
}
Updating the org.openjfx.javafxplugin (https://plugins.gradle.org/plugin/org.openjfx.javafxplugin) to the latest version inside build.gradle (from 0.0.10, as IntelliJ added it, to 0.0.13) seemed to have fixed the problem.

Unable to import compiled java avro files into Scala codebase

I am trying to reference the generated Avro files in my scala code basebase. I am using the commercehub avro gradle plugin to generate the avro java compiled files and they get correctly generated in build/generated-main-avro-java/
This is how my gradle file looks. The problem is that the java files get generated correctly but I cannot import them in my scala code. Any idea on what I maybe doing wrong here?
buildscript {
repositories {
gradlePluginPortal()
}
dependencies {
classpath "gradle.plugin.com.lightbend.akka.grpc:akka-grpc-gradle-plugin:0.7.2"
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.8'
}
}
plugins {
id 'scala'
id 'application'
id "com.google.protobuf" version "0.8.8"
id 'com.github.johnrengelman.shadow' version '5.2.0'
id "com.commercehub.gradle.plugin.avro" version "0.9.1"
}
apply plugin: 'java'
apply plugin: 'com.lightbend.akka.grpc.gradle'
apply plugin: 'com.google.protobuf'
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: "com.commercehub.gradle.plugin.avro"
akkaGrpc {
language = "Scala"
generateClient = true
generateServer = true
}
mainClassName = 'com.test.App'
repositories {
mavenLocal()
mavenCentral()
google()
}
tasks.withType(ScalaCompile) {
ScalaCompileOptions.metaClass.daemonServer = true
ScalaCompileOptions.metaClass.fork = true
ScalaCompileOptions.metaClass.useAnt = false
ScalaCompileOptions.metaClass.useCompileDaemon = false
}
def scalaVersion = "2.12"
ext.elasticAPMVersion = "1.14.0"
// Define a separate configuration for managing the dependency on Jetty ALPN agent.
configurations {
alpnagent
}
dependencies {
...
...
...
...
...
implementation "org.apache.avro:avro:1.9.2"
}
test {
jvmArgs "-javaagent:" + configurations.alpnagent.asPath
maxParallelForks = 1
}
run {
mainClassName = mainClassName
standardInput = System.in
}
task runServer(type: JavaExec) {
classpath = sourceSets.main.runtimeClasspath
main = mainClassName
jvmArgs "-javaagent:" + configurations.alpnagent.asPath
}
shadowJar {
classifier = 'shadow'
mainClassName = mainClassName
archiveName = "${title}-${version}.jar"
append 'reference.conf'
destinationDir new File("./docker/bin")
zip64 true
}
I would say, you need to add the generated sources folder to source sets and scala compiler source - something like this:
compileScala {
source = ['src/main/scala', 'src/main/java', 'build/generated-main-avro-java/']
}
sourceSets {
main {
avro {
scala.srcDir file('build/generated-main-avro-java/')
}
}
}

Getting error after setting sourceCompatility to 1.6 - Unsupported major.minor version 52.0

I need to run a spring boot app jar. My machine is on java 1.8 but the destination server is on java 1.7.
Tried several things to generate a jar that will run on java 1.7. But nothing has worked so far.
I have tried 1.7, 1.6 and also 1.5. But none have worked.
build.gradle is as follows:
buildscript {
ext {
springBootVersion = '2.2.6.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath(
"org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
plugins {
id 'org.springframework.boot' version '2.2.6.RELEASE'
id 'java'
}
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'org.springframework.boot'
/// ------------
compileJava {
options.incremental = true
options.fork = true
options.failOnError = false
}
assert hasProperty('javaHome'): "Set the property 'javaHome' in your your gradle.properties pointing to a Java 6 or 7 installation"
assert hasProperty('targetJavaVersion'): "Set the property 'targetJavaVersion' in your your gradle.properties to '1.6' or '1.7'"
java {
sourceCompatibility = JavaVersion.toVersion(targetJavaVersion)
}
def javaExecutablesPath = new File(javaHome, 'bin')
def javaExecutables = [:].withDefault { execName ->
def executable = new File(javaExecutablesPath, execName)
assert executable.exists(): "There is no ${execName} executable in ${javaExecutablesPath}"
executable
}
tasks.withType(AbstractCompile) {
options.with {
fork = true
forkOptions.javaHome = file(javaHome)
}
}
tasks.withType(JavaExec) {
executable = javaExecutables.java
}
tasks.withType(Javadoc) {
executable = javaExecutables.javadoc
}
tasks.withType(Test) {
executable = javaExecutables.java
}
///--------
group = 'com.abc.grt'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.6'
targetCompatibility = '1.6'
jar {
baseName = "someApp"
version = '0.0.1-SNAPSHOT'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
compile "org.springframework.integration:spring-integration-sftp:4.3.7.RELEASE"
// https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-context
compile group: 'org.springframework.cloud', name: 'spring-cloud-context', version: '2.2.2.RELEASE'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
bootJar {
baseName = "someApp"
version = '0.0.1-SNAPSHOT'
mainClassName = 'com.abc.grt.someApp.SomeApplication'
}
test {
useJUnitPlatform()
}
Destination machine throws the exception:
Exception in thread "main" java.lang.UnsupportedClassVersionError: org/springframework/boot/loader/JarLauncher : Unsupported major.minor version 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
...

Gradle changes JRE after "Gradle Refresh"

I'm currently using LibericaJDK-11 as my default JRE in eclipse, also in my project. I added Gradle to my Project and everytime I Refresh the Project via Gradle. It sets the JRE to JavaSE-11. And i have to manually put it back to Liberica.
Is there a way to set a default JRE for Gradle in gradle.build oder gradle.settings?
The following code is my gradle.build.
plugins {
id 'java-library'
id 'application'
id 'pmd'
}
version = '0.0.1'
group = 'zuulsLostPlace.MainApp'
application{
mainClassName = "zuulsLostPlace.MainApp"
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.4.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.4.2'
}
test {
useJUnitPlatform()
}
repositories {
mavenCentral()
}
task(runGameNoGUI, dependsOn: 'classes', type: JavaExec) {
group 'application'
description 'Starts the Text Adventure without GUI'
main = 'zuulsLostPlace.model.Game'
standardInput = System.in
ignoreExitValue true
classpath = sourceSets.main.runtimeClasspath
}
task(runGameGUI, dependsOn: 'classes', type: JavaExec) {
group 'application'
description 'Starts the Text Adventure with GUI'
main = 'zuulsLostPlace.MainApp'
standardInput = System.in
ignoreExitValue true
classpath = sourceSets.main.runtimeClasspath
}
task myJavadocs(type: Javadoc) {
source = sourceSets.main
tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
options.addStringOption('encoding', 'UTF-8')
options.addStringOption('charSet', 'UTF-8')
}
}

Categories