简体   繁体   中英

Adding build system in Sublime-Text, to Compile and Run JavaFX codes

I want to run java codes containing JavaFX pieces, in the sublime-text itself.

I am using JDK 13, and so JavaFX is not bundled with JDK itself. I downloaded the JavaFX files and stored them at E:\\javafx-sdk-15.0.1 , I also created a system environment variable PATH_TO_JAVAFX to store "E:\\javafx-sdk-15.0.1\\lib" , the directory where JavaFX modules are present.

I tried to add the following build inside Sublime-Text 3, but it is not working.

    {
        "shell_cmd": "javac --module-path %PATH_TO_JAVAFX% --add-modules javafx.base,javafx.controls,javafx.fxml,javafx.graphics,javafx.media,javafx.swing,javafx.web \"${file}\"",
        "file_regex": "^(..[^:]):([0-9]+):?([0-9]+)?:? (.)$",
        "working_dir": "${file_path}",
        "selector": "source.java",
    
        "variants":
        [
            {
                "name": "Run",
                "shell_cmd": "javac --module-path %PATH_TO_JAVAFX% --add-modules javafx.base,javafx.controls,javafx.fxml,javafx.graphics,javafx.media,javafx.swing,javafx.web \"${file}\" && java --module-path %PATH_TO_JAVAFX% --add-modules javafx.base,javafx.controls,javafx.fxml,javafx.graphics,javafx.media,javafx.swing,javafx.web \"${file_path}/${file_base_name}\""
            }
        ]
    }

Running the same command in cmd itself with actual values of file , file_path and file_base_name , works smoothly. On inspection, I found %PATH_TO_JavaFX% is not doing what I expected it to, it is not expanding to the actual path I set it to.

Trying the same build with the actual value of PATH_TO_JAVAFX works for the normal compile, but the run version still doesn't work. I got the following error:

Error: Could not find or load main class D:\\Codes\\JAVA\\HelloWorld_JavaFX.HelloWorld_JavaFX Caused by: java.lang.ClassNotFoundException: D:\\Codes\\JAVA\\HelloWorld_JavaFX.HelloWorld_JavaFX

This is the code I am trying to run( taken from ),

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    
    public class HelloWorld_JavaFX extends Application {
    
        @Override
        public void start(Stage stage) {
            String javaVersion = System.getProperty("java.version");
            String javafxVersion = System.getProperty("javafx.version");
            Label l = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
            Scene scene = new Scene(new StackPane(l), 640, 480);
            stage.setScene(scene);
            stage.show();
        }
    
        public static void main(String[] args) {
            launch();
        }
    
    }

Where exactly is the issue, and how to solve it? Also is there a way, I can use the PATH_TO_JAVAFX , instead of writing the complete path?

NOTE: I am using Windows.

I suggest using a build tool like Gradle and use the javafx Gradle plugin in your build.gradle file. Have SublimeText run the gradle build. There are Gradle plugins for Sublime Text as well. You could also use Maven, though I find it much more cumbersome.

What I assumed to be an issue with Sublime-Text turns out to be Java issue only, which could have been resolved quickly with few Google searches if I had thought about that.

So, thanks to some enlightenment from this Stackoverflow thread and mostly hit and trial, I was able to build successfully inside Sublime Text only without any added plugin or modification to Sublime Text.

This is how my build file looks now


    {
        "shell_cmd": "javac --module-path %PATH_TO_JAVAFX% --add-modules javafx.base,javafx.controls,javafx.fxml,javafx.graphics,javafx.media,javafx.swing,javafx.web \"${file}\" && java -cp \"${file_path}\" --module-path %PATH_TO_JAVAFX% --add-modules javafx.base,javafx.controls,javafx.fxml,javafx.graphics,javafx.media,javafx.swing,javafx.web \"${file_base_name}\"",
        "file_regex": "^(..[^:]):([0-9]+):?([0-9]+)?:? (.)$",
        "working_dir": "${file_path}",
        "selector": "source.java",
    
        "variants":
        [
            {
                "name": "Compile",
                "shell_cmd": "javac --module-path %PATH_TO_JAVAFX% --add-modules javafx.base,javafx.controls,javafx.fxml,javafx.graphics,javafx.media,javafx.swing,javafx.web \"${file}\""
            },
            {
                "name": "Run",
                "shell_cmd": "java -cp \"${file_path}\" --module-path %PATH_TO_JAVAFX% --add-modules javafx.base,javafx.controls,javafx.fxml,javafx.graphics,javafx.media,javafx.swing,javafx.web \"${file_base_name}\""
            }
        ]
    }

Still can't find an easy way to get Environment Variable working, so have to add complete for path in place of %PATH_TO_JAVAFX% .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM