Search code examples
javajavafxtime4j

javafx open window no effect.application launch with then throw class not found


Ok,so for starting a javafx app,we need to launch the javafx app. Then by right if we want to add a new window we can just simply do stageobj.show(); right?

Below is part of my code,that I tried to create the new window.I already launch my app and wanted to call this new code from my app.

//imports all needed class

public String plugstart(){
        try{Application.launch();
        net.time4j.ui.javafx.CalendarPicker obj=net.time4j.ui.javafx.CalendarPicker.gregorianWithSystemDefaults();
        javafx.scene.Scene scn=new javafx.scene.Scene(obj, 300, 250);
        Stage stage = new Stage();
        stage.setTitle("Date pick test");//scn.getChildren().add(obj);
        stage.setScene(scn);
        stage.show();
        return "ok";}catch(Exception e){java.io.StringWriter sw = new java.io.StringWriter();
        java.io.PrintWriter pw =new java.io.PrintWriter(sw);
        e.printStackTrace(pw);return sw.toString();}
    }
    
// not sure if this function is useful or not
    @Override
    public void start(Stage stage) {
        try{Application.launch();
        net.time4j.ui.javafx.CalendarPicker obj=net.time4j.ui.javafx.CalendarPicker.gregorianWithSystemDefaults();
        javafx.scene.Scene scn=new javafx.scene.Scene(obj, 300, 250);
        stage.setTitle("Date pick test");//scn.getChildren().add(obj);
        stage.setScene(scn);
        stage.show();
        }catch(Exception e){}
    }

to prove it compiled perfectly.

:\Coding\JavaSourceCode\plugin\userareaplugin\otherpartylibderived\time4jtest>javac -cp ;D:\Coding\JavaSourceCode\plugin\userareaplugin\otherpartylibderived\time4jtest\*;D:\Coding\JavaSourceCode\plugin\userareaplugin\otherpartylibderived\time4jtest *.java

D:\Coding\JavaSourceCode\plugin\userareaplugin\otherpartylibderived\time4jtest>

Ok so whats the problem?It should work right?Sadly no. Below pic is the stacktrace. show class not found

Anyone have similar experience or solution?

edit1,ok so someone ask about the java version,and i cannot comment.

D:\Coding\JavaSourceCode\plugin\userareaplugin\otherpartylibderived\time4jtest>java -version openjdk version "15" 2020-09-15 OpenJDK Runtime Environment (build 15+36-1562) OpenJDK 64-Bit Server VM (build 15+36-1562, mixed mode, sharing)

The jvm is complaining the Testdategui.class Yes,this is the class i that should have this new stage. Notice how it is actually already executing line 7 ,but still complain class not found.@@

Thank you very much

edit2, based on dear jetspiking suggestion. 1,i tried to make sure no launch the application twice.removed the launch crap. 2,make sure it is actually using javafx thread with javafx.application.Platform.runLater

Walah,Guess what happened?It actually returned "ok".BUT,the ui is not there.(sob)

new function state>

public String plugstart(){
        try{
        javafx.application.Platform.runLater(new Runnable(){public void run(){
            net.time4j.ui.javafx.CalendarPicker obj=net.time4j.ui.javafx.CalendarPicker.gregorianWithSystemDefaults();
            javafx.scene.Scene scn=new javafx.scene.Scene(obj, 300, 250);
        Stage stage = new Stage();
        stage.setTitle("Date pick test");//scn.getChildren().add(obj);
        stage.setScene(scn);
        stage.show();
            }});
        return "ok";}catch(Exception e){java.io.StringWriter sw = new java.io.StringWriter();
        java.io.PrintWriter pw =new java.io.PrintWriter(sw);
        e.printStackTrace(pw);return sw.toString();}
    }

ok i tried the solution and adapt it.I think it should work however i have classnotfound exception.

I think it is my own classpath setting problem not answer problem so I will accept the ans.

For those curious,now i have this new error.if i use the "brand new class".

:\Coding\JavaSourceCode\plugin\userareaplugin\otherpartylibderived\time4jtest\test3basedonSO_menoguide>java -cp %newcp%--module-path %newcp% --add-modules=all-modules Testdategui
Error: Could not find or load main class ;D:\Coding\JavaSourceCode\plugin\userareaplugin\otherpartylibderived\time4jtest\test3basedonSO_menoguide\*;D:\Coding\JavaSourceCode\plugin\userareaplugin\otherpartylibderived\time4jtest\test3basedonSO_menoguide
Caused by: java.lang.ClassNotFoundException: ;D:\Coding\JavaSourceCode\plugin\userareaplugin\otherpartylibderived\time4jtest\test3basedonSO_menoguide\*;D:\Coding\JavaSourceCode\plugin\userareaplugin\otherpartylibderived\time4jtest\test3basedonSO_menoguide

D:\Coding\JavaSourceCode\plugin\userareaplugin\otherpartylibderived\time4jtest\test3basedonSO_menoguide>

reference src,should work?but my side have prob

import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.application.Application;
public class Testdategui extends Application 
{
    
     @Override
    public void start(Stage primaryStage) throws Exception {
        // Launch all your JavaFX code and methods from inside this Entry Point function.
        Stage stage = new Stage();
        Scene scene = new Scene(net.time4j.ui.javafx.CalendarPicker.gregorianWithSystemDefaults());
        stage.setScene(scene);
        stage.show();
    }

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

edit 4,sorry no space in the module path but new error.I will try search answer on my own

java -cp %newcp% --module-path %newcp% --add-modules=all-modules Testdategui Error occurred during initialization of boot layer java.nio.file.InvalidPathException: Illegal char <*> at index 104: D:\Coding\JavaSourceCode\plugin\userareaplugin\otherpartylibderived\time4jtest\test3basedonSO_menoguide*

final edit,worked.java -cp %newcp% --module-path %cd% --add-modules ALL-MODULE-PATH Demo

Thannk you.


Solution

  • First you should try to create a minimalistic example to find where the issue is present. Next time please consider creating a reproducible example, so the community can help (debug) better.

    The "start()" method is called on the JavaFX Application Thread, when the system is ready for the application to begin running. You can not do JavaFX stuff outside of this thread, which is probably the reason for your crash. I read you are not sure if "start" is useful, but it is absolutely necessary for this reason.

    From where are you launching the plugstart method? If it is being launched outside your JavaFX Application Thread (so currently outside of override "start()" entry method) the application will crash.

    Here is a reproducible example of a JavaFX application.

    package sample;
    
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.stage.Stage;
    
    public class Main extends Application {
    
        @Override
        public void start(Stage primaryStage) throws Exception {
            // Launch all your JavaFX code and methods from inside this Entry Point function.
            Stage stage = new Stage();
            Scene scene = new Scene(new Label("I am a label object"));
            stage.setScene(scene);
            stage.show();
        }
    
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    Notice that the JavaFX code is being launched from inside the "start" method (so stage and scene are created there, etc), not from your "main" method. You only have to call "launch(args)" once from the main method, when you want your JavaFX application to start.

    You say you already launched the app, presumably from the main method. Why are you attempting to launch it in both the "start" and "plugstart" method? That will definitely cause a crash, especially since it is already running when the "start" method is called.