To Open another Application from main Application in JavaFX VsCode

56 Views Asked by At

Question Hey all. I am new to javafx coding so please level with me.

I coded three applications with their own primary stage. but now I want to open two of the applications from one application (which is going to be my main application) I am initializing two buttons in my main application and with the button click I want to open the other two application respectively.

Details: Main App; I created this app to establish a port connection with my device to communicate with it.

App 1 & 2: These app are created to generate a single page pdf file for different purpose. in these apps I am taking input from user but now I have to fill some fields by a button click that will take information from my device. For that matter I want to establish a connection with my device. This is the reason I want to connect these three apps. else I have to make a new project from scratch which I don't want to do now if there is any solution of my problem.

I tried all the solutions I can find on internet but nothing is working for me. I don't know what to do now. PLEASE HELP This one is my latest.

//Main App Code:
public class ConnectionTestApp extends Application {
public void start(Stage primaryStage) throws IOException {

//code that sets the stage
}

public void setupEventHandlers() throws IOException {

   **// this button open App 1**

btnOpenCertificate.setOnAction(event -> {
try {
ProcessBuilder CertificateAppBuilder = new ProcessBuilder("java","-cp",".","project.pdfcreation.CertificateAppFinal");
Process CertificateAppprocess = CertificateAppBuilder.start();

            InputStream CertificateAppinputStream = CertificateAppprocess.getInputStream();
            InputStream CertificateApperrorStream = CertificateAppprocess.getErrorStream();

            readStream("App Output: ", CertificateAppinputStream);
            readStream("App Error: ", CertificateApperrorStream);
        }
        catch (IOException e)
        {
            AlertMessages.showErrorDialogue("Error", "Unable to open app: "+ e.getMessage());
        }
            
    });
 **// this button open App 1**

    btnOpenInsDoc.setOnAction(event -> {
        try {
            ProcessBuilder InspectionAppBuilder = new ProcessBuilder("java","-cp",".","project.pdfcreation.InspectionAppFinal");

            Process InspectionAppprocess = InspectionAppBuilder.start();

            InputStream InspectionAppinputStream = InspectionAppprocess.getInputStream();
            InputStream InspectionApperrorStream = InspectionAppprocess.getErrorStream();

            readStream("App2 Output: ", InspectionAppinputStream);
            readStream("App2 Error: ",  InspectionApperrorStream);
        }
        catch (IOException e)
        {
            AlertMessages.showErrorDialogue("Error", "Unable to open app: "+ e.getMessage());
        }

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


This is the error I am getting with this code

P.S:

When I change InspectionAppFinal to InspectionAppFinal.java or InspectionAppFinal.class (In process builder) it opens the file in VS code and gives no error/exception. I'm confused what I am missing or doing rong.

Context

I am using JavaFX in VSCODE.

Java version:

openjdk version "21.0.1" 2023-10-17 LTS OpenJDK Runtime Environment Temurin-21.0.1+12 (build 21.0.1+12-LTS) OpenJDK 64-Bit Server VM Temurin-21.0.1+12 (build 21.0.1+12-LTS, mixed mode, sharing)

Operating system and platform:

Windows 11 Version 10.0.22621 Build 22621 x64-based PC

1

There are 1 best solutions below

0
David Weber On

Solution 1:

If you have one JAR which contains your 3 applications, then you must refactor your code until you have only one application which wrapps them all.

Solution 2:

If you have 3 JARs, separate ones for each application, then you can use the class Runtime or ProcessBuilder for starting the other JARs.

Example:

ProcessBuilder pb = new ProcessBuilder("java", "-jar", "absolute path upto jar");
Process p = pb.start();

Never use relative paths. And keep in mind that there is no real directory structure in your JARs. You can try to use the JARs from outside the JAR, or zu get them via classpath with getClass(). getResource("/a/b/c.jar").

Solution 3:

You could think about using Docker with Docker Compose.