Migrate to openJDK 13 and openJFX 13

江枫思渺然 提交于 2019-12-25 01:24:38

问题


I had to switch to openJDK coming from JDK8 and I am not able to compile my program with maven. This is new to me and after days of googling I am more confused about the versions than I was before.

I could not find out:

  • Am I supposed to use the latest version of openJDK? (which is currently 13)
  • Does the openJFX has to match the version of openJDK?
  • Maven: Do I have to compile it with source and target with 13 or is it possible with 1.8 to be compatible with JRE1.8

Here is what I did in Eclipse

  • imported openJDK 13 into eclipse and set it as the default JDK.
  • updated the pom.xml to maven compiler 3.8.1 and set the source and target to 13.
  • added openJFX 13 dependencies

When I freshly import the project from GIT it runs without any errors. After I compile it or update the project via Maven I get a bunch of different errors and the program is not runnable anymore. I noticed that maven sets my Project JRE to [J2SE-1.5] every time I update it. How do I prevent this?

This is really strange. Inside Class I get compiler erros that something cannot be found, but in my package explorer everything seems fine:

Snapshot

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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>IBE_Calculator</groupId>
  <artifactId>IBE_Calculator</artifactId>
  <version>IBE</version>


  <build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
      <resource>
        <directory>src</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>

      <resource>
        <directory>main/resources</directory>
        <includes>
            <include>IBEDB.sqlite</include>        
        </includes>
      </resource>

      <resource>
        <directory>res</directory>
            <excludes>
              <exclude>**/*.java</exclude>
            </excludes>
        </resource>
    </resources>

        <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.1</version>
        </plugin>
      </plugins>
    </pluginManagement>

    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
              <source>13</source>
              <target>13</target>
            </configuration>
        </plugin>


        <plugin>
             <artifactId>maven-assembly-plugin</artifactId>
             <configuration>
                 <archive>
                     <manifest>
                         <mainClass>main.java.srcMain.Main</mainClass>
                     </manifest>
                 </archive>
                 <descriptorRefs>
                     <descriptorRef>jar-with-dependencies</descriptorRef>
                 </descriptorRefs>
             </configuration>
             <executions>
                 <execution>
                     <phase>install</phase>
                     <goals>
                         <goal>single</goal>
                     </goals>
                 </execution>
             </executions>
         </plugin>

        <plugin>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>0.0.3</version>
            <configuration>
                <mainClass>main.java.srcMain.Main</mainClass>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/classes/libs2</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>    
    </plugins>
  </build>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-base</artifactId>
            <version>13</version>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-graphics</artifactId>
            <version>13</version>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>13</version>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>13</version>
        </dependency>

        <dependency>
            <groupId>net.sourceforge.jexcelapi</groupId>
            <artifactId>jxl</artifactId>
            <version>2.6.12</version>
        </dependency>

        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.8.11.2</version>
        </dependency>

        <dependency>
            <groupId>org.dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>2.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.jgrapht</groupId>
            <artifactId>jgrapht-ext</artifactId>
            <version>1.0.1</version>
        </dependency>

        <dependency>
            <groupId>net.sourceforge.jexcelapi</groupId>
            <artifactId>jxl</artifactId>
            <version>2.6.12</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.9</version>
        </dependency>

        <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>2.5.0</version>
        </dependency>
     </dependencies>

    <name>IBECalc</name>
</project>

I feel like theres nothing wrong with my code but here is the current error:

Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: java.lang.Error: Unresolved compilation problems: 
    The method handle(WindowEvent) of type new EventHandler<WindowEvent>(){} must override a superclass method
    The method run() of type new Runnable(){} must override a superclass method

    at srcMain.Main$1.handle(Main.java:42)
    at srcMain.Main$1.handle(Main.java:1)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.stage.Window.fireEvent(Window.java:1368)
    at javafx.stage.Window$12.invalidated(Window.java:1122)
    at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:110)
    at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:145)
    at javafx.stage.Window.setShowing(Window.java:1174)
    at javafx.stage.Window.show(Window.java:1189)
    at javafx.stage.Stage.show(Stage.java:273)
    at srcMain.Main.start(Main.java:74)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    ... 1 more

I call my Main class from a new Class, because there seems to be a bug.

Class NewMain:

package srcMain;

public class NewMain {

    public static void main(String args[] ) throws Exception {
        Main.main(args);
    }
}

Class Main:

package srcMain;
import java.io.IOException;

import java.sql.SQLException;
import java.text.ParseException;

import SQLite.*;

import javafx.application.*;
import com.sun.javafx.application.*;

import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javafx.event.*;
import javafx.fxml.FXMLLoader;
import javafx.scene.*;



public class Main extends Application{
    static SQLite db;
    public static Boolean isSplashLoaded = false;
    public static Boolean data_fin = false;
    public static Stage parentWindow;
    Stage stage = new Stage();

    @Override
    public void start(final Stage primaryStage) throws Exception{
        /*
         * �ffnet den Loading Screen und startet die Dateneinlesung
         */
        try{

            parentWindow = primaryStage;
            FXMLLoader loader = new FXMLLoader(getClass().getResource("IBE_LoadScreen.fxml"));
            loader.setController(new ControllerLoadScreen());
            Parent root1 = (Parent)loader.load();

            //wenn die GUI angezeigt wird, wird die Dateneinlesung gestartet
            stage.addEventHandler(WindowEvent.WINDOW_SHOWN, new EventHandler<WindowEvent>() {
                @Override
                public void handle(WindowEvent window){
                    Platform.runLater(new Runnable(){
                        @Override
                        public void run(){
                            try {
                                try {
                                    readData();
                                } catch (ParseException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            } catch (SQLException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            data_fin = true;
                        }
                    });
                }
            });

            //wird der Close-Button gefr�ckt, schlie�t sich das gesamte Programm, nicht nur das Fenster
            Platform.setImplicitExit(false);
            stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
                @Override
                public void handle(WindowEvent event) {
                    Platform.exit();
                }
            });
            stage.setResizable(true);
            stage.setTitle("Wegeentgelt-Kalkulator");
            stage.setScene(new Scene(root1));
            stage.show();
        }catch(Exception e){
            System.out.println("Cant load new window");
            e.printStackTrace();
        }

    }


    public void readData() throws SQLException, ParseException {
        /*
         * erstellt ein ExcelHandler Objekt und ruft alle Methoden auf
         * die Daten von den Excel-Files einlesen.
         * 
         * Wenn diese Daten fertig eingelesen wurden, wird die Main-Maske angezeigt.
         */

        db.Hst();
        db.Halte();
        db.Marktsegment();
        db.bstn();
        db.basisdaten();
        db.TFZ();

        db.closeConnection();
        data_fin = true;

        //wenn die Daten eingelesen wurden �ffnet sich die Main-Maske
        if(data_fin){
            FXMLLoader loader = new FXMLLoader(getClass().getResource("IBECalc-Main_1024_768.fxml"));
            loader.setController(new ControllerMain("1024x768"));
            Parent root1;
            try {
                root1 = (Parent)loader.load();
                stage.getScene().setRoot(root1);
                stage.setResizable(true);
                stage.setMinHeight(680);
                stage.setMinWidth(1024);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args)  throws SQLException{
        //startet das Programm
        db = new SQLite();

        launch(args);



    }
}

I imported the project to another environment and now there is a new Error. It can't find the Application Class to start the GUI.

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method launch(String[]) is undefined for the type Main

    at srcMain.Main.main(Main.java:123)
    at srcMain.NewMain.main(NewMain.java:6)

回答1:


Am I supposed to use the latest version of openJDK? (which is currently 13)

You can use any version that is currently available / not end-of-life. At this time (October 2019):

  • you could continue with OpenJDK 8 until at least December 2020,
  • OpenJDK 9 & 10 are now end-of-life,
  • OpenJDK 11 is the latest LTS version,
  • OpenJDK 12 is now end-of-life, and
  • OpenJDK 13 is the latest version.

(You could even use an end-of-life version, but it is inadvisable because there are unlikely to be any security patches available.)

Does the OpenJFX has to match the version of OpenJDK?

Prior to Java 11 this question was moot as OpenJFX was bundled with OpenJDK (or Oracle JDK or JRE)

  • OpenJFX 11 (release notes) JDK 11 is recommended
  • OpenJFX 12 (release notes) requires JDK 11
  • OpenJFX 13 early access (release notes) requires JDK 11 or later. (This could be revised.)

So the versions do not need to match.

Maven: Do I have to compile it with source and target with 13 or is it possible with 1.8 to be compatible with JRE1.8

It is not clear what you are asking here. If you intend to run on the same version platform that you are building for, then the source and target versions should match that Java version. As a general rule, you can take classes built on an older platform and run them on a newer platform. (The exception is if you are using deprecated classes or methods that have been removed.)

If you want to build on a newer platform and run on an older one, then you need to set the source and target versions to match the older platform version. But that may not be sufficient. You also need to match sure that your code doesn't depend on newly access classes and methods that are not supported in the older runtime libraries.

The same principles apply with JavaFX applications. If you want your JavaFX app to run on Java 8, it is best to build on Java 8. If you build on Java 11, you will need to use 8 as the source and target versions, and you will need to restrict yourself to using only JavaFX 8 APIs.


Your exceptions seem to be caused by compilation errors that have not been corrected. You should not run code with compilation errors. Fix all of the compilation errors before you run the code.

The final compikation error is due to calling a static method that doesn't exist in the Main class. In fact, the launch method is a static method of Application, so you need to call it like this:

    Application.launch(args);

Static methods are not inherited in Java.




回答2:


Solution: All the compilation errors were caused by currpted OpenJFX libs via maven. I solved the problem by deleting all repositories and importing it again with the mvn update project function.



来源:https://stackoverflow.com/questions/58367850/migrate-to-openjdk-13-and-openjfx-13

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!