OpenCVライブラリが読み込めない
module-info.javaでOpenCVライブラリが読み込めないのでエラーになりました。下のようなエラー内容です。
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project pi4j-example-javafx: Compilation failure: Compilation failure:
[ERROR] /home/pi/projects/pi4j-example-javafx/src/main/java/com/pi4j/example/HelloCV.java:[8,18] package org.opencv.core is not visible
[ERROR] (package org.opencv.core is declared in the unnamed module, but module org.opencv.core does not read it)
[ERROR] /home/pi/projects/pi4j-example-javafx/src/main/java/com/pi4j/example/HelloCV.java:[11,26] package javafx.embed.swing does not exist
[ERROR] /home/pi/projects/pi4j-example-javafx/src/main/java/com/pi4j/example/HelloCV.java:[28,19] package javafx.fxml does not exist
[ERROR] /home/pi/projects/pi4j-example-javafx/src/main/java/com/pi4j/example/HelloCV.java:[29,18] package org.opencv.core is not visible
[ERROR] (package org.opencv.core is declared in the unnamed module, but module org.opencv.core does not read it)
[ERROR] /home/pi/projects/pi4j-example-javafx/src/main/java/com/pi4j/example/HelloCV.java:[30,18] package org.opencv.core is not visible
[ERROR] (package org.opencv.core is declared in the unnamed module, but module org.opencv.core does not read it)
[ERROR] /home/pi/projects/pi4j-example-javafx/src/main/java/com/pi4j/example/HelloCV.java:[45,9] cannot find symbol
[ERROR] symbol: class FXMLLoader
[ERROR] location: class com.pi4j.example.HelloCV
[ERROR] /home/pi/projects/pi4j-example-javafx/src/main/java/com/pi4j/example/HelloCV.java:[45,33] cannot find symbol
[ERROR] symbol: class FXMLLoader
[ERROR] location: class com.pi4j.example.HelloCV
[ERROR] /home/pi/projects/pi4j-example-javafx/src/main/java/com/pi4j/example/HelloCV.java:[51,15] cannot find symbol
[ERROR] symbol: class OpenCvController
[ERROR] location: class com.pi4j.example.HelloCV
[ERROR] -> [Help 1]
Mavenで下のコマンドを実行したときに出たエラーです。
mvn package
コンパイルするときにJARファイルはインストールできているのだが、読み込めない。。。
他にも下のようなエラーが出ました。
was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced -> [Help 1]
解決
以下の原因があったようです。
- キャッシュが更新されていない
- module-infoに読み込みを書いてない
- javafx-fxml, javafx-swingのバージョン指定が不適切
キャッシュをクリアする
下のコマンドで再度実行
mvn -U package
これで読込は動いた
module com.pi4j.example {
// Pi4J MODULES
requires com.pi4j;
requires com.pi4j.plugin.pigpio;
// SLF4J MODULES
requires org.slf4j;
requires org.slf4j.simple;
// JavaFX
requires transitive javafx.graphics;
requires transitive javafx.controls;
requires transitive javafx.fxml;
requires transitive javafx.swing;
requires transitive javafx.base;
// Add Modules
requires java.desktop;
// 3rd-party
requires org.bytedeco.opencv.platform;
requires org.bytedeco.javacv.platform;
uses com.pi4j.extension.Extension;
uses com.pi4j.provider.Provider;
// allow access to classes in the following namespaces for Pi4J annotation processing
opens com.pi4j.example to com.pi4j;
exports com.pi4j.example to javafx.graphics;
}
バージョン指定
下の部分がバージョン指定のところです。「${javafx.version}」はプロパティ要素で定義している定数です。
<version>${javafx.version}</version>
javafx-fxmlは「11」が使用できないらしい。。。
POMファイル
下記のようにPOMファイルに定義したらコンパイルできた。
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-fxml -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>${javafx.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-swing -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-swing</artifactId>
<version>17-ea+8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-base -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-base</artifactId>
<version>${javafx.version}</version>
</dependency>
module-info.java
/*-
* #%L
* **********************************************************************
* ORGANIZATION : Pi4J
* PROJECT : Pi4J :: EXAMPLE :: Sample Code
* FILENAME : module-info.java
*
* This file is part of the Pi4J project. More information about
* this project can be found here: https://pi4j.com/
* **********************************************************************
* %%
* Copyright (C) 2012 - 2020 Pi4J
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
module com.pi4j.example {
// Pi4J MODULES
requires com.pi4j;
requires com.pi4j.plugin.pigpio;
// SLF4J MODULES
requires org.slf4j;
requires org.slf4j.simple;
// JavaFX
requires transitive javafx.graphics;
requires transitive javafx.controls;
requires transitive javafx.fxml;
requires transitive javafx.swing;
requires transitive javafx.base;
// Add Modules
requires java.desktop;
// 3rd-party
requires org.bytedeco.opencv.platform;
requires org.bytedeco.javacv.platform;
uses com.pi4j.extension.Extension;
uses com.pi4j.provider.Provider;
// allow access to classes in the following namespaces for Pi4J annotation processing
opens com.pi4j.example to com.pi4j;
/* opens com.pi4j.example to javafx.graphics;
opens com.pi4j.example to javafx.controls;
opens com.pi4j.example to javafx.fxml;
opens com.pi4j.example to javafx.swing;
opens com.pi4j.example to javafx.base; */
}