如何重定向JavaFX的DLL库缓存路径?配置后仍报错
如何将JavaFX缓存目录重定向到指定路径?
我无法将JavaFX的缓存库放在"C:\Users\anpaumal\"目录下,希望将其重定向到C:\Program Files\OpenJdk路径。
我的pom文件
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.openjfx</groupId> <artifactId>javafx-maven-plugin</artifactId> <version>16</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.release>11</maven.compiler.release> <javafx.version>16</javafx.version> <javafx.maven.plugin.version>0.0.8</javafx.maven.plugin.version> </properties> <dependencies> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-controls</artifactId> <version>${javafx.version}</version> </dependency> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-fxml</artifactId> <version>${javafx.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <release>${maven.compiler.release}</release> </configuration> </plugin> <plugin> <groupId>org.openjfx</groupId> <artifactId>javafx-maven-plugin</artifactId> <version>${javafx.maven.plugin.version}</version> <configuration> <mainClass>org.openjfx.hellofx.App</mainClass> </configuration> </plugin> </plugins> </build> </project>
遇到的错误
java.lang.UnsatisfiedLinkError: C:\Users\anpaumal.openjfx\cache\16\prism_d3d.dll
该DLL被安全策略阻止(无法从该目录执行DLL),且我无权修改此策略。
已尝试的操作
- 在运行命令中配置参数:
-Djavafx.cachedir="C:\Program Files\OpenJDK" -Djavafx.verbose=true - 尝试不带引号的参数配置:
-Djavafx.cachedir=C:\Program Files\OpenJDK -Djavafx.verbose=true - 在Eclipse运行配置中设置环境变量
javafx.cachedir为C:\Program Files\OpenJDK
以上操作后仍出现相同错误,请问是否可以实现将缓存目录重定向的需求?
可行的解决方法
1. 在Maven插件中直接配置JVM参数
修改pom.xml中的javafx-maven-plugin配置,添加jvmArgs参数强制指定缓存目录,同时确保目标目录有读写权限:
<plugin> <groupId>org.openjfx</groupId> <artifactId>javafx-maven-plugin</artifactId> <version>${javafx.maven.plugin.version}</version> <configuration> <mainClass>org.openjfx.hellofx.App</mainClass> <jvmArgs> "-Djavafx.cachedir=C:\Program Files\OpenJDK\.openjfx\cache" -Djavafx.verbose=true </jvmArgs> </configuration> </plugin>
2. 配置系统级环境变量
设置系统环境变量JAVAFX_CACHEDIR为C:\Program Files\OpenJDK\.openjfx\cache,JavaFX会优先读取这个环境变量作为缓存目录。
3. 手动使用本地JavaFX SDK替代自动下载
若上述方法无效,可手动下载JavaFX 16 SDK并解压到C:\Program Files\OpenJDK,然后修改pom.xml指定本地SDK路径:
<plugin> <groupId>org.openjfx</groupId> <artifactId>javafx-maven-plugin</artifactId> <version>${javafx.maven.plugin.version}</version> <configuration> <mainClass>org.openjfx.hellofx.App</mainClass> <libraryPath>C:\Program Files\OpenJDK\javafx-sdk-16\lib</libraryPath> </configuration> </plugin>
同时将依赖改为系统依赖,避免自动下载缓存:
<dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-controls</artifactId> <version>${javafx.version}</version> <scope>system</scope> <systemPath>C:\Program Files\OpenJDK\javafx-sdk-16\lib\javafx-controls.jar</systemPath> </dependency> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-fxml</artifactId> <version>${javafx.version}</version> <scope>system</scope> <systemPath>C:\Program Files\OpenJDK\javafx-sdk-16\lib\javafx-fxml.jar</systemPath> </dependency>
内容的提问来源于stack exchange,提问作者Paul
相关产品推荐
相关产品推荐

