Running Win32 program from Bash for Windows 10
TL;DR: Microsoft didn't initially make it possible to run Windows Apps.
As of Insider Preview build 14951, announced 19 October 2016, you can run native Windows programs from Bash under Ubuntu under Windows (WSL)
Windows binaries can now be invoked directly from the WSL command line. This gives users the ability to interact with their Windows environment and system in a way that has not been possible. As a quick example, it is now possible for users to run the following commands:
$ export PATH=$PATH:/mnt/c/Windows/System32 $ notepad.exe $ ipconfig.exe | grep IPv4 | cut -d: -f2 $ ls -la | findstr.exe foo.txt $ cmd.exe /c dir
https://msdn.microsoft.com/en-us/commandline/wsl/release_notes#build-14951
The announcement said
Windows Subsystem for Linux: Today we are happy to announce two large updates to WSL!
Official Ubuntu 16.04 support. Ubuntu 16.04 (Xenial) is installed for all new Bash on Ubuntu on Windows instances starting in build 14951. This replaces Ubuntu 14.04 (Trusty). Existing user instances will not be upgraded automatically. Users on the Windows Insider program can upgrade manually from 14.04 to 16.04 using the do-release-upgrade command.
Windows / WSL interoperability. Users can now launch Windows binaries directly from a WSL command prompt. This is the number one request from our users on the WSL User Voice page.
(my emphasis)
Cause of error
The message
cannot execute binary file: Exec format error
This means Bash is convinced that adb.exe is the wrong type of file for execution.
To find out why, try using:
file /mnt/c/Android/sdk/platform-tools/adb.exe
For example:
$ pwd
/mnt/c/Windows/System32
$ ./notepad.exe
bash: ./notepad.exe: cannot execute binary file: Exec format error
$ file notepad.exe
notepad.exe: PE32+ executable (GUI) x86-64, for MS Windows
Windows Susbsystem for Linux
Note that Microsoft's initial objectives for WSL were
Using Bash, you can run command-line Linux tools and apps.
Note that does not include
- graphical (X11) Linux tools and apps.
- Windows tools and apps.
32-bit binaries
I believe Microsoft's Windows Subsystem for Linux (WSL) currently only supports 64-bit ELF binaries, not 32-bit ELF binaries.
Windows binaries
Also, from this question it seems WSL at time of writing this answer did not support running Windows executables from within Bash.
This is confirmed by HowtoGeek
Unfortunately, there’s no way to actually launch a Windows program or run a Windows command from within a Bash script or the Bash shell. However, you can incorporate Bash commands into a Batch script or PowerShell script
Why not :-(
If you look at how Microsoft did this you can see that Bash.exe
is a native Windows console-mode app that, I believe, basically acts as a very thin front end that communicates with an unmodified /bin/bash
which does all the actual interpretation and execution of commands.
WSL ... is primarily comprised of:
- User mode session manager service that handles the Linux instance life cycle
- Pico provider drivers (lxss.sys, lxcore.sys) that emulate a Linux kernel by translating Linux syscalls
- Pico processes that host the unmodified user mode Linux (e.g. /bin/bash)
Since an unmodified /bin/bash does not expect to be able to run programs that are not native Linux "ELF" binaries, it objects to Windows "PE" binaries.
I certainly don't want to run down the rabbit hole of running WINE under WSL. I suggest you avoid this idea too.
Possible solution
See project cbwin
Launch Windows programs from "Bash on Ubuntu on Windows" (WSL)
main features:
- Win32 command line tools in the console, invoked from WSL
- Win32 command line tools with redirections to WSL (stdin/stdout/stderr to or from pipe/file)
- suspend/resume propagation (Ctrl-Z suspends the Win32 processes, fg resumes them)
- exit codes propagation
- launch "detached" GUI Windows programs (uses start of cmd)
Related videos on Youtube
Gabriel Sanmartin
Updated on September 18, 2022Comments
-
Gabriel Sanmartin over 1 year
I got the latest update from Windows that adds bash, and I was trying to script the execution of an Android emulator through this; however the Path variables of Windows are not taken into account for bash.
Plus I tried executing adb manually and I got:
andtest@DESKTOP:/mnt/c/Android/sdk/platform-tools$ exec ./adb.exe bash: /mnt/c/Android/sdk/platform-tools/adb.exe: cannot execute binary file: Exec format error
Is there something I'm missing here? I want to use this Windows computer as a test server for Android.
-
codaamok over 7 yearsCan you not just use an absolute bath to call
exec
, or create an alias? -
Randy Schuman over 7 yearsDid you see this posting? stackoverflow.com/questions/10681101/…
-
Orphans over 7 yearsI may be really wrong, but you can't execute .exe files in bash - even in windows
-
Gabriel Sanmartin over 7 years@adampski I get an "exec format error" when I run exec ./adb.exe
-
Randy Schuman over 7 yearsTry it without the .exe extension.
-
Gabriel Sanmartin over 7 years@RandySchuman same thing, also with JAVA_HOME i get this: ERROR: JAVA_HOME is set to an invalid directory: /mnt/c/Program Files/Java/jdk1.7.0_79/, I tried with quotes, also with "Program\ Files" but to no avail :(
-
HDave over 7 yearsUpgrade to win10 build 14955 and reinstall WSL (e.g. lxrun /install). You will get Ubuntu 16.04 and running win binaries with Just Work.
-
-
Gabriel Sanmartin over 7 yearsThanks, this also explains why the JAVA_HOME is not valid: all Java binaries are for Windows.
-
user5249203 over 7 yearsYou could use
apt-get install default-jre default-jdk
and then you'd be able to use bash to compile .java to byte-code and run it. You should, in principle, be able to run java applications you've compiled under Windows and vice versa. -
HDave over 7 yearsThis is no longer valid. With the latest preview build of windows 10, WSL can execute windows binaries just fine.