I've been trying to find a solution for this problem but everything I tried just failed.
I am using IntelliJ, and trying to get a resource from Resource folder (which marked as Resources folder and it is in the path of the src folder).
I created a simple example that produces the same problem. Lets say I want to set a label's text to the full path of a python file in the resources folder:
package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URISyntaxException;
public class Main3 extends Application {
Stage window;
Label lbl = new Label();
@Override
public void start(Stage primaryStage) throws Exception {
window = primaryStage;
window.setTitle("Title");
VBox vbox = new VBox();
Button loginBtn = new Button("Log In");
loginBtn.setOnAction(e->{
try {
printScriptFile();
} catch (Exception ex) {
ex.printStackTrace();
}
});
vbox.getChildren().addAll(loginBtn, lbl);
Scene scene = new Scene(vbox, 300,200);
window.setScene(scene);
window.show();
}
public void printScriptFile() throws IOException {
String path = null;
try {
path = getClass().getClassLoader().getResource("SuspendProcess.py").toURI().getPath();
} catch (URISyntaxException e) {
e.printStackTrace();
}
lbl.setText(path);
}
public static void main(String[] args) {
launch(args);
}
}
This code works just fine in the IDE, but when I run it from the jar it's not working.
Structure:
How can I fix this, please?