Error: JavaFX runtime components are missing. When building a javafx application - java

This is my first javafx and gradle application. First I created a gradle project in IntelliJ and then I added JavaFX dependencies referring this tutorial. My gradle file looks like this:
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform
plugins {
id 'application'
}
group 'com.skb'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
def javaFXPlatform = getJavaFXPlatform()
def javaFXVersion = "15.0.1"
dependencies {
// you need a dependency for each of the JavaFX modules you're going to use
implementation "org.openjfx:javafx-base:${javaFXVersion}:${javaFXPlatform}"
implementation "org.openjfx:javafx-controls:${javaFXVersion}:${javaFXPlatform}"
implementation "org.openjfx:javafx-graphics:${javaFXVersion}:${javaFXPlatform}"
}
application {
//Java Module System module name
mainModule.set('com.skb')
//Your JavaFX application class
mainClass.set('com.skb.EditorApp')
}
java {
// this enables Java Modularity in Gradle (version 6.7 and above)
modularity.inferModulePath.set(true)
}
// Based on this StackOverflow answer: https://stackoverflow.com/a/65209664/653519
private static String getJavaFXPlatform() {
def currentOS = DefaultNativePlatform.currentOperatingSystem
if (currentOS.isWindows()) {
return 'win'
} else if (currentOS.isLinux()) {
return 'linux'
} else if (currentOS.isMacOsX()) {
return 'mac'
}
return null
}
My module_info.java lloks like this:
module com.skb {
requires javafx.fxml;
requires javafx.controls;
requires javafx.graphics;
requires java.base;
//requires org.fxmisc.richtext;
//requires org.json;
opens com.skb;
}
EditorApp class contains just some biolerplate code:
package com.skb;
import javafx.application.Application;
import javafx.stage.Stage;
public class EditorApp extends Application {
#Override
public void start(Stage primaryStage) {
System.out.println("Running...");
}
public static void main(String[] args) {
launch(args);
}
}
Project structure is like this:
I am using IntelliJ 2021.1 Community Edition.
Please help me out. I have already searched a lot on this error, and got nothing that solved the error.

Related

How can i compile java record with scala code?

I'm currently working on project with both Scala and Java. And we want to rewrite some Scala code on Java. So when I've been starting rewrite my code I've faced with this issue.
CountryResponse.java:5: illegal start of type declaration
It seem like Scala couldn't compile Java's records introduced in JDK 16. I've made some research and found only this discussion on GitHub. Could someone suggest any workaround to compile Java's records?
I did try increase Scala version from 2.12.2 to 2.13.6 but problem wasn't solved.
We use Java 16, Scala 2.12.2 and Gradle 7.0.1 and also scala and java plugins for Gradle.
Also here my gradle's settings for compile both sources.
compileScala {
sourceCompatibility("16")
targetCompatibility("16")
}
sourceSets {
main {
java {
srcDirs = []
}
scala {
srcDirs = ['src/main/scala', 'src/main/java']
}
}
}
edit: update link to discussion
I tested with following project containing both Scala and Java 16 sources. The Scala and Java source depends on each other.
If your both Java and Scala sources depend on each other, then you will need to put those sources under common folder. Lets say main/jvm.
You can place all your Java records under main/java. This will work as long as your records don't have dependency on Scala code. Otherwise you will have to break this into multiple modules and micromanage according to the dependency graph.
Gradle version is 7.1, and this project builds and then runs successfully (both ScalaMain and JavaMain).
build.gradle
plugins {
id 'java'
id 'scala'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
compileScala {
sourceCompatibility("16")
targetCompatibility("16")
}
sourceSets {
main {
java {
srcDirs = ['src/main/java']
}
scala {
srcDirs = ['src/main/jvm']
}
}
}
dependencies {
implementation 'org.scala-lang:scala-library:2.13.6'
}
main/java/TestJavaRecord.java
public record TestJavaRecord(int i) {}
main/jvm/TestScalaClass.scala
class TestScalaClass(val i: Int, val testJavaRecord: TestJavaRecord) {}
main/jvm/TestJavaClass.java
public class TestJavaClass {
public int i;
public TestScalaClass testScalaClass;
public TestJavaClass(int i, TestScalaClass testScalaClass) {
this.i = i;
this.testScalaClass = testScalaClass;
}
}
main/jvm/ScalaMain.scala
object ScalaMain extends App {
val testJavaRecord = new TestJavaRecord(5)
val testScalaClass = new TestScalaClass(5, testJavaRecord)
val testJavaClass = new TestJavaClass(5, testScalaClass)
println(testJavaRecord.i)
println(testScalaClass.testJavaRecord.i)
println(testJavaClass.testScalaClass.testJavaRecord.i)
}
main/jvm/JavaMain.java
public class JavaMain {
public static void main(String[] args) {
TestJavaRecord testJavaRecord = new TestJavaRecord(5);
TestScalaClass testScalaClass = new TestScalaClass(5, testJavaRecord);
TestJavaClass testJavaClass = new TestJavaClass(5, testScalaClass);
System.out.println(testJavaRecord.i());
System.out.println(testScalaClass.testJavaRecord().i());
System.out.println(testJavaClass.testScalaClass.testJavaRecord().i());
}
}

java 9 modules with gradle not working: error: module not found: <module name>

I made a minimalist java gradle project to test java 9 modules.
I made only 1 module and tried testing depending on a third party module. But I keep getting the error when I try to build or run the project:
module not found: commons.validator
here are my files:
Demo.java"
public class Demo {
public static void main(String[] args) {
boolean result = EmailValidator.getInstance().isValid("abcd");
System.out.println(result);
}
}
module-info.java:
module auth.server.main {
requires commons.validator;
}
build.gradle:
plugins {
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
// for email validation
implementation 'commons-validator:commons-validator:1.7'
}
/*this is to let gradle infer the locations of the modules*/
java {
modularity.inferModulePath.set(true)
}
Whats wrong and how to solve it?
This might easy way.
plugins {
id 'java'
id "org.javamodularity.moduleplugin" version "1.1.1"
}
repositories {
mavenCentral()
}
dependencies {
implementation 'commons-validator:commons-validator:1.7'
}
Check it out : https://github.com/java9-modularity/gradle-modules-plugin

JavaFX runtime components are missing error in Java Gradle Project

I started a Gradle project with java and I wanted to add JavaFx Library but when I run my application I'd got "JavaFX runtime components are missing, and are required to run this application".
This is my Gradle file:
plugins {
id 'java'
id 'org.openjfx.javafxplugin' version '0.0.9'
}
group 'org.personal'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
javafx {
version = "15.0.1"
modules = [ 'javafx.controls', 'javafx.fxml' ]
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
}
test {
useJUnitPlatform()
}
And this is the main class:
package client;
import javafx.application.Application;
import javafx.stage.Stage;
public class ClientApp extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
}
}
And this is my "Edit Configuration": Edit Configuration here
you need to add the JavaFX JDK libs to the path. Otherwise, you need to use JDK 8 (JDK1.8) which integrates the JavaFX SDK

api configuration of java-library plugin is not recognized

I am new to Gradle and i am using Gradle 6.1.
I am writing small application to understand the concepts of multi project Application and Java-Library plugin of Gradle.
My Question is :
How App.java is running perfectly fine without importing DefaultRandomGenerator class from SubProject-2
Why am i getting the error message "No candidates found for method call api" in build.grade file of Parent project (MultiProjectApp).
Below are my application code :
Parent Project (MultiProjectApp) files
settings.gradle
rootProject.name = 'MultiProjectApp'
include 'SubProject-1'
include 'SubProject-2'
build.gradle
allprojects {
apply plugin: 'java'
group 'org.example'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
}
project(':SubProject-1') {
dependencies {
implementation project(':SubProject-2')
}
}
project(':SubProject-2') {
apply plugin: 'java-library'
dependencies {
api 'org.apache.commons:commons-math3:3.2'
implementation 'org.apache.logging.log4j:log4j-core:2.5'
testImplementation "junit:junit:4.12"
}
}
SubProject-2 files
build.gradle
Empty file
RandomGenerator.java
package org.examples;
public interface RandomGenerator {
String name();
int generate();
}
DefaultRandomGenerator.java
package org.examples;
import org.apache.commons.math3.random.RandomDataGenerator;
public class DefaultRandomGenerator implements RandomGenerator {
public String name() {
return "Main Random Number Generator";
}
public int generate() {
final RandomDataGenerator randomDataGenerator = new RandomDataGenerator();
return randomDataGenerator.nextInt(5, 10);
}
}
SubProject-1 files
build.gradle
Empty file
App.java
package org.examples;
import org.apache.commons.math3.random.RandomDataGenerator;
public class App {
public static void main(String[] args) {
RandomGenerator aRandomGenerator = new DefaultRandomGenerator();
System.out.println("The 1st random number is :" + aRandomGenerator.generate());
System.out.println("The 2nd random number is :" + generateMy());
}
public static int generateMy() {
final RandomDataGenerator aRandomDataGenerator = new RandomDataGenerator();
return aRandomDataGenerator.nextInt(5, 10);
}
}
How App.java is running perfectly fine without importing
DefaultRandomGenerator class from SubProject-2
It works because they are both in the same package (org.examples).
Note that this will not work if using the new module system introduced in Java 9. Because the two projects are considered "split", and you will need various hacks to make it work.
Why am I getting the error message "No candidates found for method
call api" in build.grade file of Parent project (MultiProjectApp).
This is an IDE problem, not a gradle problem. If you run, it should still work.
Example of runnig it in Vscode

Custom Gradle Java plugin crashes in v5.2.1, but worked in v4.7?

My plugin worked in Gradle v4.7, but is now crashing v5.2.1. I know that's a HUGE version jump.
I wrote a custom Gradle java plugin, and this the plugin implementation class. I put a breakpoint in the task.setProject(project) call, but it never gets there.
package com.zift.utilities;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
public class ZiftVersionPlugin implements Plugin<Project> {
public void apply(Project project) {
project.getTasks().create("manageVersion", ZiftVersion.class, (task) -> {
// Added breakpoint here, but it's never reached!
task.setProject(project);
});
}
}
Edit: (added simplified plugin code below)
Here's the simplified class that implements the manageVersion task. All the import statements will probably be unnecessary with this code.
package com.zift.utilities;
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.options.Option;
import org.gradle.api.Project;
public class ZiftVersion extends DefaultTask {
#Input private Project project;
private String projectDir=null;
private String propFileFullPath=null;
private String sourceBranch=null;
private String destinationBranch="staging";
String getPropFile() { return propFileFullPath; }
void setPropFile(String fn) { this.propFileFullPath = fn; }
public Project getProject() { return project; }
public void setProject(Project project) {
this.project = project;
this.projectDir = this.project.getRootDir().toString();
}
// The void set*() functions have to immediately follow their corresponding
// #Option() so they can take arguments in the command line, e.g.,
// $ ./gradlew manageVersion \
// --srcBranch=bugfix/devops-507-semver --dstBranch=master
#Option(option = "srcBranch", description = "Source branch of the pull request")
public void setSrcBranch(String s) { this.sourceBranch = s; }
public String getSrcBranch() { return sourceBranch; }
#Option(option = "dstBranch", description = "Destination branch of the pull request")
public void setDstBranch(String d) { this.destinationBranch = d; }
public String getDstBranch() { return destinationBranch; }
#TaskAction
void manageProjectVersion() {
System.out.println("Hello world!");
}
}
Here's my project's build.gradle file to build the plugin
plugins {
id 'idea'
id 'java'
id 'maven'
id 'maven-publish'
id 'java-gradle-plugin'
}
group=project.groupId
version = '666.666.666'
dependencies {
compile gradleApi()
}
jar {
manifest {
attributes 'artifactId': 'zift-version-plugin',
'groupId': 'com.zift.utilities',
'version': project.version
}
baseName artifactId
doLast {
println "artifactId: $project.artifactId\ngroupId: $project.groupId\nversion: $version"
}
}
gradlePlugin {
plugins {
simplePlugin {
id = 'com.zift.utilities.zift-version-plugin'
implementationClass = 'com.zift.utilities.ZiftVersionPlugin'
}
}
}
Here's how I use it, from another Gradle project that uses the plugin
$ ./gradlew manageVersion --srcBranch=feature/devops-507-semver-support --dstBranch=master
> Task :manageVersion
Hello world!
Here's the exception, which looks like it occurred during the create() function call.
Caused by org.gradle.api.internal.tasks.DefaultTaskContainer$TaskCreationException: Could not create task ':manageVersion'
Caused by java.lang.NullPointerException: (No message provided)
at org.gradle.api.internal.tasks.TaskPropertyUtils.visitProperties(TaskPropertyUtils.java:38)
at org.gradle.api.internal.project.taskfactory.PropertyAssociationTaskFactory.create(PropertyAssociationTaskFactory.java:49)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory.create(AnnotationProcessingTaskFactory.java:46)
...
at org.gradle.api.internal.tasks.DefaultTaskContainer.create(DefaultTaskContainer.java:359)
at com.zift.utilities.ZiftVersionPlugin.apply(ZiftVersionPlugin.java:8)
Any clues?
Idiocy is the cause, my idiocy particularly.
I compiled my plugin with Gradle wrapper from v4.x, so when a project built with v5.x or above uses it, the plugin crashes.
Solution is to upgrade my plugin's wrapper to Gradle v6.0, build and publish a new version, and have projects use the new version. No more crashes.

Categories

Resources