我收到“ gradle包javafx.geometry不存在”错误,使用gradle编译

时间:2019-07-16 22:26:34

标签: eclipse gradle javafx java-8

当我运行gradle“ run”任务时,我无法完成Java的编译,因为我得到了许多形式的错误:“错误:所有javaFX导入的包javafx.XXXXX不存在”。我的测试项目可以很好地导入,尽管它没有使用gradle进行编译。

我正在尝试在Ubuntu VM上运行客户端程序“ BilliardViewer”。它是通过许多程序员的手中完成的,其中一些程序员可能没有那么多的编码经验。我不确定这是gradle问题,eclipse问题,库问题还是编码问题。我知道JavaFX库已正确安装,因为我可以运行单独的HelloWorld jfx程序。两者之间构建路径的唯一区别是BilliardViewer程序具有一些外部依赖性,因为它使用C ++和Java。该程序目前在他的Mac上运行,我相信MacOS Mojave。

我的HelloWorld程序有效。

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
...

@Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }

BilliardViewer程序中的某些代码不起作用(Colors.java)

import java.util.Optional;

import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
...

public ColorPicker(final int x, final int y) {
        stage.setX(x);
        stage.setY(y);

        stage.focusedProperty().addListener((obs, wasFocused, isNowFocused) -> {
            if (!isNowFocused) {
                stage.hide();
            }
        });

        stage.setScene(new Scene(grid));
        stage.setTitle("SO COLORFUL");
        grid.setPadding(new Insets(10));

        for (int row = 0; row < ROWS; row++) {
            for (int col = 0; col < COLS; col++) {
                final Color currentColor = color[row][col];

                final Rectangle rect = new Rectangle(RECT_SIZE, RECT_SIZE, currentColor);

                rect.setOnMouseClicked(event -> {
                    selectedColor = Optional.of(currentColor);
                    stage.close();
                });

                Tooltip.install(rect, new Tooltip(Colors.colorMap.get(currentColor).get()));

                grid.add(rect, col, row);
            }
        }
    }

我希望该版本至少能够编译Java,因为完全相同的代码可以在客户端的Mac笔记本电脑上正常编译。

编辑:build.gradle

apply plugin: 'cpp'
apply plugin: TestingModelBasePlugin
//apply plugin: 'findbugs'

mainClassName = 'billiards.viewer.Main'

repositories {
    jcenter()
}

dependencies {
    // When getting the version number for a dependency, don't go to Maven Central and
    // simply search for the package you want. You will often find many versions of the
    // package, most of which are unofficial or out-of-date. Instead, go to the website
    // of the package, and they will usually give you the correct Maven information there.
    compile 'org.eclipse.collections:eclipse-collections-api:9.2.0'  // All the interfaces
    compile 'org.eclipse.collections:eclipse-collections:9.2.0'  // The actual classes
    compile 'com.google.guava:guava:25.1-jre'
    compile 'org.apache.commons:commons-math3:3.6.1'
    compile 'org.apache.commons:commons-lang3:3.7'  // can remove this now I think?
    compile 'org.apache.commons:commons-io:1.3.2'
    compile 'io.javaslang:javaslang:2.0.5'
    compile 'org.xerial:sqlite-jdbc:3.23.1'
    compile 'net.java.dev.jna:jna:4.5.1'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.2.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.2.0'
}

test {
    useJUnitPlatform()
}

compileJava {
    options.compilerArgs << '-Xlint' << '-Werror'
}

jar {
    baseName = 'billiard-viewer'
    manifest.attributes 'Main-Class': 'billiards.viewer.Main'
}

sourceSets {
    main {
        java {
            srcDir 'src/java'
        }

        //resources {
            //srcDir 'css'
        //}
    }
}

applicationDefaultJvmArgs = ['-server', '-Djna.library.path=./build/libs/backend/shared', '-Xss10000m']

sourceCompatibility = 1.8
targetCompatibility = 1.8

model {

    components {

        backend(NativeLibrarySpec) {

            binaries.all {

                cppCompiler.define "NDEBUG"
                cppCompiler.args '-O3', '-march=native', '-flto', '-ftrapv'
                linker.args '-lgmp', '-lmpfr', '-lmpfi', '-lsqlite3', '-ltbb'

                if (toolChain in Clang) {
                    cppCompiler.args '-Weverything', '-Werror',
                                     '-Wno-padded', '-Wno-comma',
                                     '-Wno-exit-time-destructors', '-Wno-global-constructors',
                                     '-Wno-c++98-compat', '-Wno-c++98-compat-pedantic',
                                     '-std=c++14'
                }

                if (toolChain in Gcc) {
                    cppCompiler.args '-Wall', '-Wextra', '-Wpedantic', '-Werror',
                                     '-Wno-c++11-compat', '-Wno-c++14-compat',
                                     '-std=c++14'
                }

                checkedBy $.tasks.testBackend
            }
        }

        test(NativeExecutableSpec) {

            sources.cpp {
                lib library: 'backend', linkage: 'static'
            }

            binaries.all {

                cppCompiler.define "NDEBUG"
                cppCompiler.args '-O3', '-march=native', '-flto', '-ftrapv'
                linker.args '-lgmp', '-lmpfr', '-lmpfi', '-lboost_unit_test_framework'

                if (toolChain in Clang) {
                    cppCompiler.args '-Weverything', '-Werror',
                                     '-Wno-padded', '-Wno-comma',
                                     '-Wno-disabled-macro-expansion', '-Wno-global-constructors',
                                     '-Wno-c++98-compat', '-Wno-c++98-compat-pedantic',
                                     '-std=c++14'
                }

                if (toolChain in Gcc) {
                    cppCompiler.args '-Wall', '-Wextra', '-Wpedantic', '-Werror',
                                     '-Wno-c++11-compat', '-Wno-c++14-compat',
                                     '-std=c++14'
                }
            }
        }
    }
}

task testBackend(type: Exec, dependsOn: 'testExecutable') {
    commandLine 'build/exe/test/test'

    // save the results here
    //standardOutput = new FileOutputStream('build/test-results/test.out')
    //errorOutput = new FileOutputStream('build/test-results/test.err')
}

// Make sure the C++ code is up to date when running the Java program
run.dependsOn "backendSharedLibrary"
//run.dependsOn "coverExecutable"

run.doFirst {
    // Having this hardcoded isn't the best, put it works for now
    //environment 'LD_PRELOAD', '/usr/local/Cellar/jemalloc/5.0.1/lib/libjemalloc.so.2'
    //commandLine "./test.fish"
}

1 个答案:

答案 0 :(得分:0)

根据stackoverflow的介绍,我最终通过在我的eclipse项目中添加了gradle.properties文件来解决了这个问题,该文件向gradle显示了在何处查找JDK。因此,为了澄清起见,我必须安装一个JDK,并将gradle指向该JDK。