简体   繁体   English

在 Sublime-Text 中添加构建系统,以编译和运行 JavaFX 代码

[英]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.我想在 sublime-text 本身中运行包含 JavaFX 片段的 Java 代码。

I am using JDK 13, and so JavaFX is not bundled with JDK itself.我使用的是 JDK 13,因此 JavaFX 没有与 JDK 本身捆绑在一起。 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.我下载了 JavaFX 文件并将它们存储在E:\\javafx-sdk-15.0.1 ,我还创建了一个系统环境变量PATH_TO_JAVAFX来存储"E:\\javafx-sdk-15.0.1\\lib" ,JavaFX 模块所在的目录存在。

I tried to add the following build inside Sublime-Text 3, but it is not working.我尝试在 Sublime-Text 3 中添加以下构建,但它不起作用。

    {
        "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.在 cmd 本身中使用filefile_pathfile_base_name实际值运行相同的命令,可以顺利运行。 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.经过检查,我发现 %PATH_TO_JavaFX% 没有按照我的预期运行,它没有扩展到我设置的实际路径。

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.使用PATH_TO_JAVAFX的实际值尝试相同的构建适用于正常编译,但运行版本仍然不起作用。 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错误:无法找到或加载主类 D:\\Codes\\JAVA\\HelloWorld_JavaFX.HelloWorld_JavaFX 导致: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?还有一种方法,我可以使用PATH_TO_JAVAFX ,而不是写完整的路径?

NOTE: I am using Windows.注意:我使用的是 Windows。

I suggest using a build tool like Gradle and use the javafx Gradle plugin in your build.gradle file.我建议使用像 Gradle 这样的构建工具,并在 build.gradle 文件中使用 javafx Gradle 插件。 Have SublimeText run the gradle build.让 SublimeText 运行 gradle 构建。 There are Gradle plugins for Sublime Text as well. Sublime Text 也有 Gradle 插件。 You could also use Maven, though I find it much more cumbersome.您也可以使用 Maven,但我发现它更麻烦。

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.我认为是 Sublime-Text 的问题结果只是 Java 问题,如果我考虑过这一点,可以通过很少的谷歌搜索快速解决。

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.因此,多亏了这个Stackoverflow 线程的一些启发,并且主要是命中和试用,我能够在 Sublime Text 中成功构建,而无需添加任何插件或对 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% .仍然找不到让环境变量工作的简单方法,因此必须添加完整的路径来代替%PATH_TO_JAVAFX%

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Sublime Text 2构建系统,用于在新的终端/命令提示符窗口中编译和运行Java? - Sublime Text 2 build system to compile & run Java in a new Terminal/Command Prompt window? Sublime Text 2-编译并运行单个Java文件 - Sublime text 2 - compile and run single java file 如何在Ubuntu 14.04上使用Sublime Text 2编译和运行Java程序? - How to compile and run Java Program with Sublime Text 2 on Ubuntu 14.04? Java 在 Sublime Text 3 中编译 - Java compile in Sublime Text 3 如何设置Sublime text 3在linux上运行和编译java? - How to set up Sublime text 3 to run and compile java on linux? 如何编译和运行包含带有Sublime Text 2导入的Java程序? - How to Compile and Run Java programs that include a Import with Sublime Text 2? 如何通过 Ubuntu 上的终端使 Sublime Text 3 编译和运行 java - How to make Sublime Text 3 compile and run java through terminal on Ubuntu 如何在 sublime text 3 中为 java 构建系统,它也支持包? - how to build system in sublime text 3 for java which also supports packages? 是否可以使用Sublime Text 2使用库构建/运行Java - Is it possible to build/run java with libraries using Sublime Text 2 设置Sublime text 3以在Ubuntu 16.04 LTS上运行和编译Java时出错 - Error in setting up Sublime text 3 to run and compile java on Ubuntu 16.04 LTS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM