如何解决 Netbeans 22 中 Nashorn 删除和 JS 替换问题?

问题描述 投票:0回答:1

尽管完全按照指定的步骤在 Netbeans 22 中安装 Nashorn 的替代方案,但在尝试构建 FXML 项目时,我仍然收到以下错误:

Java 15 has removed Nashorn, you must provide an engine for running JavaScript yourself.

GraalVM JavaScript 是我正在尝试集成的 js。在 Oracle graalvm 页面之后,我下载并提取了以下内容:

https://download.oracle.com/graalvm/22/latest/graalvm-jdk-22_windows-x64_bin.zip

将下载解压到目录

C:Program Files/Java/graalvm-jdk-22.0.1+8.1
并将文件夹重命名为
graalvm-22

在管理 cmd shell 中,使用以下命令设置 JAVA_HOME 环境变量:

setx /M JAVA_HOME "C:\Progra~1\Java\graalvm-22"

并设置路径:

setx /M PATH "C:\Progra~1\Java\<graalvm>\bin;%PATH%"

用 echo 检查这些变量,结果显示:

echo %JAVA_HOME%
C:\Progra~1\Java\graalvm-22

echo %PATH%
C:\Progra~1\Java\graalvm-22\bin;
C:\Program Files\CommonFiles\Oracle\Java\javapath;
C:\WINDOWS\system32;
C:\WINDOWS;
C:\WINDOWS\System32\Wbem;
C:\WINDOWS\System32\WindowsPowerShell\v1.0\;
C:\WINDOWS\System32\OpenSSH\;
C:\Program Files\HP\HP One Agent;C:\Users\qabpa\AppData\Local\Microsoft\WindowsApps;

我已经被这个问题困扰了几天,非常感谢您的帮助。谢谢。

javascript java netbeans fxml
1个回答
0
投票

删除或卸载全部

  • 删除或卸载 JDK、JavaFX、Netbeans...
  • 删除环境变量JAVA_HOME

安装 zulu JDK 包括 JavaFX

https://www.azul.com/downloads/?version=java-21-lts&os=windows&architecture=x86-64-bit&package=jdk-fx#zulu

下载 ->

.msi

JDK_000.png

zulu21.34.19-ca-fx-jdk21.0.3-win_x64.msi

点击

zulu21.34.19-ca-fx-jdk21.0.3-win_x64.msi

下拉

Set JAVA_HOME variable
,选择
Entire feature will be installed on local hard drive

JDK_001.png

点击

Next

JDK_002.png

请注意,您不需要自己设置

JAVA_HOME
PATH
环境变量。

因为安装程序已经自动配置好了。

检查JDK安装

打开CMD,输入命令

javac -version

javac 21.0.3

输入另一个命令:

java -version

openjdk version "21.0.3" 2024-04-16 LTS
OpenJDK Runtime Environment Zulu21.34+19-CA (build 21.0.3+9-LTS)
OpenJDK 64-Bit Server VM Zulu21.34+19-CA (build 21.0.3+9-LTS, mixed mode, sharing)

安装 Netbeans 22

注意,请使用exe安装而不是zip安装。

点击

Apache-NetBeans-22-bin-windows-x64.exe

Netbeans_001.png

NetBeans 将检测到 Zulu-21

点击

Next

Netbeans_002.png

点击

Install

配置JavaFX

菜单

Tools
->
Options

Netbeans_JavaFX_001.png

点击

Next

Netbeans_JavaFX_002.png

选择

accept the terms in all of the license agreements.

点击

Install

Netbeans_JavaFX_003.png

点击

Finish

Netbeans_JavaFX_004.png

点击

OK

创建 JavaFX 示例

File
->
New Project...

Netbeans_Example_001.png

  • 类别 ->
    Java with Maven
  • 项目 ->
    FXML JavaFX Maven Archetype

点击

Next

Netbeans_Example_002.png

  • 项目名称:
    hello1fx
  • 群组 ID:
    com.exmaple

点击

Finish

Netbeans_Example_003.png

单击图标(运行)

然后出现javafx窗口。

创建脚本引擎

项目目录树

hello1fx
├── nbactions.xml
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   ├── com
    │   │   │   └── example
    │   │   │       └── hello1fx
    │   │   │           ├── App.java
    │   │   │           └── PrimaryController.java
    │   │   └── module-info.java
    │   └── resources
    │       └── com
    │           └── example
    │               └── hello1fx
    │                   └── primary.fxml
    └── test
        └── java

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>hello1fx</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>21</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>21</version>
        </dependency>
        <dependency>
            <groupId>org.openjdk.nashorn</groupId>
            <artifactId>nashorn-core</artifactId>
            <version>15.4</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.4</version>
                <configuration>
                    <mainClass>com.example.hello1fx.App</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <!-- Default configuration for running -->
                        <!-- Usage: mvn clean javafx:run -->
                        <id>default-cli</id>
                    </execution>
                    <execution>
                        <!-- Configuration for manual attach debugging -->
                        <!-- Usage: mvn clean javafx:run@debug -->
                        <id>debug</id>
                        <configuration>
                            <options>
                                <option>-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=localhost:8000</option>
                            </options>
                        </configuration>
                    </execution>
                    <execution>
                        <!-- Configuration for automatic IDE debugging -->
                        <id>ide-debug</id>
                        <configuration>
                            <options>
                                <option>-agentlib:jdwp=transport=dt_socket,server=n,address=${jpda.address}</option>
                            </options>
                        </configuration>
                    </execution>
                    <execution>
                        <!-- Configuration for automatic IDE profiling -->
                        <id>ide-profile</id>
                        <configuration>
                            <options>
                <option>${profiler.jvmargs.arg1}</option>
                <option>${profiler.jvmargs.arg2}</option>
                <option>${profiler.jvmargs.arg3}</option>
                <option>${profiler.jvmargs.arg4}</option>
                <option>${profiler.jvmargs.arg5}</option>
                            </options>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
  • 为脚本引擎添加nashorn-core
  • 将 javafx-xxx 版本从
    13
    更改为
    21
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>21</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>21</version>
        </dependency>
        <dependency>
            <groupId>org.openjdk.nashorn</groupId>
            <artifactId>nashorn-core</artifactId>
            <version>15.4</version>
        </dependency>

primary.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.Button?>
<?import javafx.geometry.Insets?>

<VBox alignment="CENTER" spacing="20.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.hello1fx.PrimaryController">
   <children>
      <TextArea fx:id="inputArea" text="" promptText="Enter your script here..." />
      <Button fx:id="runButton" text="run" onAction="#execRun"/>
      <Button fx:id="cleanButton" text="clean" onAction="#execClean"/>
      <TextArea fx:id="outputArea" text="" editable="false" />
   </children>
</VBox>

模块信息.java

  • 添加
    requires java.scripting;
  • 添加
    requires jdk.dynalink;
module com.example.hello1fx {
    requires javafx.controls;
    requires javafx.fxml;
    
    requires java.scripting;
    requires jdk.dynalink;
    
    opens com.example.hello1fx to javafx.fxml;
    exports com.example.hello1fx;
}

应用程序.java

package com.example.hello1fx;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

/**
 * JavaFX App
 */
public class App extends Application {

    private static Scene scene;

    @Override
    public void start(Stage stage) throws IOException {
        scene = new Scene(loadFXML("primary"), 640, 480);
        stage.setScene(scene);
        stage.setTitle("Nashorn JavaScript Engine");
        stage.show();
    }

    static void setRoot(String fxml) throws IOException {
        scene.setRoot(loadFXML(fxml));
    }

    private static Parent loadFXML(String fxml) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));
        return fxmlLoader.load();
    }

    public static void main(String[] args) {
        launch();
    }

}

PrimaryController.java

package com.example.hello1fx;

import java.io.IOException;
import javafx.fxml.FXML;
import javafx.scene.control.TextArea;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.PrintWriter;
import java.io.StringWriter;

public class PrimaryController {
    ScriptEngineManager manager ;
    ScriptEngine engine;
    @FXML
    private TextArea inputArea;
    
    @FXML
    private TextArea outputArea;
    
    @FXML
    private void execRun() throws IOException {
            manager = new ScriptEngineManager();
            engine = manager.getEngineByName("nashorn");
            StringWriter writer  = new StringWriter();
            engine.getContext().setWriter(writer);

            try {
                String inputText = inputArea.getText();
                System.out.println(">>> inputText = "+inputText);
                engine.eval(inputText );
                System.out.println(">>> OK");
                outputArea.setText( "Processed: \n"+writer.toString());
                System.out.println(">>> set output OK");
            } catch (ScriptException e1) {
                e1.printStackTrace(new PrintWriter(writer));
            }       
    }
    
    @FXML
    private void execClean() throws IOException {
        inputArea.clear();
        outputArea.clear();
    }
}

最终结果

单击图标再次运行

JavaFX_Script_001.png

在(1)输入区域放置一些java脚本代码。

print('Hello World!')
print(100 + 200)

单击按钮

run
(3)

然后将结果显示在(2)中

您可以点击按钮

clean
(4)来清洁(1)和(2)

© www.soinside.com 2019 - 2024. All rights reserved.