Gradle build for javafx application: Virtual Keybo

2019-08-31 02:53发布

问题:

i am using a javafx application and we developed a gradle build system for this application. The jar file can be created with the following gradle task:

    task fatJar(type: Jar) {
manifest {
    attributes 'Main-Class': 'myProject'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA' 
with jar

}

This works so far - the only problem is that we want to use the virtual keyboard (javafx) and therefore we need to set the following system properties:

     systemProperty 'com.sun.javafx.isEmbedded', 'true' 
     systemProperty 'com.sun.javafx.touch', 'true'       
     systemProperty 'com.sun.javafx.virtualKeyboard', 'javafx' 

Can i set this properties in the gradle build or is it necessary to start the application with

java -Dcom.sun.javafx.isEmbedded=true -Dcom.sun.javafx.touch=true -Dcom.sun.javafx.virtualKeyboard=javafx -jar myProject.jar

best regards

__________________________________-

The solution (big thanks to Frederic Henri :-)) is to write a wrapper class like

public class AppWrapper 
{   
    public static void main(String[] args) throws Exception 
    {  
        Class<?> app = Class.forName("myProject");         
        Method main = app.getDeclaredMethod("main", String[].class);     
        System.setProperty("com.sun.javafx.isEmbedded", "true"); 
        System.setProperty("com.sun.javafx.touch", "true");          
        System.setProperty("com.sun.javafx.virtualKeyboard", "javafx");     
        Object[] arguments = new Object[]{args};
        main.invoke(null, arguments);
    }
}

回答1:

I dont think this job can be done from the build phase (I'd be happy to be wrong and see another answer though) -

It seems there is another answer here Java: Modify System properties via runtime where it says to write a wrapper around your final main method and so you can pass the properties you need, not sure how good it is though.