How to embed external exe files in a Flutter project for Windows?
Asked Answered
C

2

10

In my Flutter project for Windows, I want to run some .exe files that I want to embed in my project, instead of copying/pasting them by hand in the correct location.

So I tried to add the .exe files in my assets folder, like the following:

assets
  \exe
  \images

and add the exe folder in my pubspec.yaml file like the following:

flutter:
  assets:
    - assets/images/
    - assets/exe/

The good news is: the exe files are copied in the correct location when I build my project. The bad news is: I don't know how to get the absolute path of my exe folder in Dart.

So, is there a way to get the absolute path of the exe folder for a Windows project, or is there another way to embed my .exe files so I can get their paths and run them (using Process.start())?

Thanks.

Chelyuskin answered 24/9, 2021 at 9:30 Comment(5)
facing the same problem did you find any solution ??Pya
@AtulChaudhary My answer belowChelyuskin
Here is a possible answer (spoiler: you can't get the file path): https://mcmap.net/q/437361/-how-do-i-get-the-asset-39-s-file-path-in-flutterGrath
I faced the same problem, and my workaround was to include the exe file inside the installer (not inside the project). In my case I used Msix installer for FlutterGrath
@gowthamanC My answer belowChelyuskin
C
5

Here is what I did to get the absolute path of the exe folder (only tested on Windows) :

// returns the abolute path of the executable file of your app:
String mainPath = Platform.resolvedExecutable;

// remove from that path the name of the executable file of your app:
mainPath = mainPath.substring(0, mainPath.lastIndexOf("\\"));

// concat the path with '\data\flutter_assets\assets\exe', where 'exe' is the
// directory where are the executable files you want to run from your app:
Directory directoryExe = Directory("$mainPath\\data\\flutter_assets\\assets\\exe")
Chelyuskin answered 30/12, 2021 at 9:46 Comment(4)
I've a problem with this solution in Linux (I assume that in Windows could be similar): when you try to start the process of your exe file, it throws a "permission denied" exceptionGrath
@PabloInsua try this, chmod a+x yourfileOccidentalize
@Chelyuskin Hi, how do I get similar path for MacOs?Undertaking
flutter build removes execute permissions from assets. You need to chmod +x those executables manually or in your CI/CD pipeline.Albertoalberts
T
0

If you are using flutter run you can just use this path

const exePath = "assets\\exe";

But if you plan on using flutter build you will need to use the approach by @matteoh

// Get the executable path and extract the parent directory
String appPath = Platform.resolvedExecutable;
appPath = appPath.substring(0, appPath.lastIndexOf("\\"));

String exePath = "$appPath\\data\\flutter_assets\\assets\\exe";

In my experimentation workingDirectory does not work with .exe files.

Process pythonApp = Process.run("api.exe", [], workingDirectory: exePath); 

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: ProcessException: The system cannot find the file specified.

But if you call the exe directly like so

Process pythonApp = Process.run("$exePath\\api.exe", []); 

It works!

Thing answered 2/5, 2024 at 17:26 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.