0

My JavaFX project runs in the editor fine but when I try to build to .jar it wont run. I am using java 17 so I need to manually import it and I have imported the libraries properly in settings.json and have added the vmArgs in launch.json. When I run- java gui.java (gui.java is my class name) I get errors saying stuff like: error: package javafx.application does not exist , error: package javafx.geometry does not exist , error: package javafx.scene does not exist. I know that I should use a different version of java or use something other that vs code but I don't know how to convert a project to an earlier version of java and I want to use vs code. Does anyone know how I can get a .jar or any executable working for it. I am even fine with something like a bash script that compiles it manually I just need to run it outside the editor. Here is my gui code I know that allot of it is bad.

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Scanner;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class gui extends Application {
    
    ArrayList<CheckBox> checkBoxes = new ArrayList<CheckBox>();
    static String currentDir = System.getProperty("user.dir");
    static File checkListFile = new File(currentDir + "\\list.TXT");
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) throws Exception {
        BorderPane root = new BorderPane();
        Scene scene = new Scene(root, 450, 250);
        VBox vbCenter = new VBox();
        TextField input = new TextField();
        input.setOnKeyPressed( event -> {
            if( event.getCode() == KeyCode.ENTER ) {
                addCheckBox(input.getText(), vbCenter);
                input.setText("");
                saveCheckBoxes();
            }
        });
        vbCenter.getChildren().add(input);
        
        HBox hbButtons = new HBox();
        Button reset = new Button("Reset");
        reset.setOnAction( event  -> {
            removeCheckBoxes(vbCenter);
        });
        hbButtons.getChildren().add(reset);
        hbButtons.setAlignment(Pos.CENTER_LEFT);
        
        loadCheckBoxes(vbCenter);
        
        root.setPadding(new Insets(20));
        root.setCenter(vbCenter);
        root.setBottom(hbButtons);
        primaryStage.setTitle("Checklist");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    public void addCheckBox(String name, VBox vbCenter) {
        CheckBox checkBox = new CheckBox(name);
        checkBoxes.add(checkBox);
        vbCenter.getChildren().add(checkBox);
    }
    
    public void removeCheckBoxes(VBox vbCenter) {
        for (CheckBox checkBox : checkBoxes) {
            vbCenter.getChildren().remove(checkBox);            
        }
        checkBoxes = new ArrayList<CheckBox>();
        try {
            clearTheFile(checkListFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void clearTheFile(File file) throws IOException {
        FileWriter fwOb = new FileWriter(file); 
        PrintWriter pwOb = new PrintWriter(fwOb, false);
        pwOb.flush();
        pwOb.close();
        fwOb.close();
    }
    
    public void saveCheckBoxes() {
        String[] name = new String[checkBoxes.size()];
        for (int i = 0; i < checkBoxes.size(); i++) {
            name[i] = checkBoxes.get(i).getText();
        }
        writeData(name, checkListFile);
    }
    
    public void loadCheckBoxes(VBox vb) {
        String[] data = readData(checkListFile);
        for (String s : data) {
            CheckBox checkBox = new CheckBox(s);
            checkBoxes.add(checkBox);
            vb.getChildren().add(checkBox);
        }
        
    }
    
    public String[] readData(File file) {
        String[] result = new String[0];
        try {
            result = new String[(int)Files.lines(file.toPath()).count()];
            Scanner scanner = new Scanner(file);
            int index = 0;
            while (scanner.hasNextLine()) {
                result[index] = scanner.nextLine();
                index++;
            }
            scanner.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    
    public void writeData(String data, File file) {
        try  {
            FileWriter writer = new FileWriter(file);
            writer.write(data);
            writer.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void writeData(String[] dataArr, File file) {
        String data = "";
        for (int i = 0; i < dataArr.length; i++) {
            data += (dataArr[i] + "\n");
        }
        writeData(data, file);
    }
}

launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "java",
            "name": "Launch Current File",
            "request": "launch",
            "mainClass": "${file}"
        },
        {
            "type": "java",
            "name": "Launch gui",
            "request": "launch",
            "vmArgs": "--module-path C:/Users/Billy1301/Downloads/openjfx-17.0.1_windows-x64_bin-sdk/javafx-sdk-17.0.1/lib --add-modules javafx.controls,javafx.fxml",
            "mainClass": "gui",
            "projectName": "Productivity_5753000c"
        }
    ]
}

settings.json

{
    "java.project.referencedLibraries": [
        "lib/**/*.jar",
        "c:\\Users\\Billy1301\\Downloads\\openjfx-17.0.1_windows-x64_bin-sdk\\javafx-sdk-17.0.1\\lib\\javafx.base.jar",
        "c:\\Users\\Billy1301\\Downloads\\openjfx-17.0.1_windows-x64_bin-sdk\\javafx-sdk-17.0.1\\lib\\javafx.controls.jar",
        "c:\\Users\\Billy1301\\Downloads\\openjfx-17.0.1_windows-x64_bin-sdk\\javafx-sdk-17.0.1\\lib\\javafx.fxml.jar",
        "c:\\Users\\Billy1301\\Downloads\\openjfx-17.0.1_windows-x64_bin-sdk\\javafx-sdk-17.0.1\\lib\\javafx.graphics.jar",
        "c:\\Users\\Billy1301\\Downloads\\openjfx-17.0.1_windows-x64_bin-sdk\\javafx-sdk-17.0.1\\lib\\javafx.media.jar",
        "c:\\Users\\Billy1301\\Downloads\\openjfx-17.0.1_windows-x64_bin-sdk\\javafx-sdk-17.0.1\\lib\\javafx.swing.jar",
        "c:\\Users\\Billy1301\\Downloads\\openjfx-17.0.1_windows-x64_bin-sdk\\javafx-sdk-17.0.1\\lib\\javafx.web.jar",
        "c:\\Users\\Billy1301\\Downloads\\openjfx-17.0.1_windows-x64_bin-sdk\\javafx-sdk-17.0.1\\lib\\javafx-swt.jar"
    ]
}
Billy1301
  • 11
  • 3
  • See: [How to create a standalone .exe in Java](https://stackoverflow.com/questions/69811401/how-to-create-a-standalone-exe-in-java-that-runs-without-an-installer-and-a-jr). – jewelsea Dec 15 '21 at 04:12
  • The `args` in `launch.json` will only be called when debugging. There's a java setting called `"java.jdt.ls.vmargs"`, set it value as VMargs's. If not help, you may try to use command to export jars with reference to @jewelsea commented. – Molly Wang-MSFT Dec 16 '21 at 08:59

0 Answers0