0

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:

the project structure

How can I fix this, please?

Ryan Dawson
  • 11,832
  • 5
  • 38
  • 61
  • 1
    https://stackoverflow.com/questions/39167762/url-getresource-not-working-when-in-jar-file –  Aug 29 '18 at 12:01
  • I need to get the path of the file , but in this solution they talk about reading the resource as stream. As far as I know, this won't help me with the path problem, right? – Ori Netanel Ben-Zaken Aug 29 '18 at 12:05
  • You have to include that python file into your jar. How do you build your jar? – m4gic Aug 29 '18 at 12:05
  • Have you checked, if the resource is included in the jar? `jar -tf yourJar.jar` should list everything included in the jar (your IDE may also allow you to browse the jar, at least NetBeans provides this feature). – fabian Aug 29 '18 at 12:06
  • Yes, the file is in the jar. I build the jar with IntelliJ – Ori Netanel Ben-Zaken Aug 29 '18 at 12:08
  • 1
    @OriNetanelBen-Zaken is the desired path relative to the main source folder or a full absolute path in the filesystem? – Aboca Aug 29 '18 at 12:17
  • I believe relative would be enough, but I'd like to know how to get the absolute too – Ori Netanel Ben-Zaken Aug 29 '18 at 12:28

0 Answers0