0

I am using jpackage to convert a Java application into an executable .app file.

When running on macOS, as shown below, the application cannot read user input.

open -n /Applications/MyApp.app

The application can read user input if it is run as

open -n /Applications/MyApp.app/Contents/MacOS/MyApp.app

This is the code to read user input (using batch script is not applicable here)

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
   String line = reader.readLine();
   // process the string line here
}

What needs to be done to read user input when launching an application using open -n /Applications/MyApp.app (or another convenient command) ?

1
  • You should ask yourself what exactly you are trying to read. Commented Jul 25 at 19:17

1 Answer 1

1

Use Terminal to Launch the Executable

Instead of using open, you can directly invoke the executable file within the app bundle: bash

/Applications/MyApp.app/Contents/MacOS/MyApp

This ensures the application attaches to the terminal and can read user input. i hope*

Sign up to request clarification or add additional context in comments.

4 Comments

This works, the app is connected to the OS terminal and allows reading user input. Is there a way to collect user input when the app is launched, by clicking on its icon in the Dock or by running it as /Applications/MyApp.app?
On macOS, when you start a .app via Finder / Dock / open, it launches as a GUI app with no controlling terminal. That means stdin is not attached to a TTY (often /dev/null), so readLine() will block and you won’t be able to read from the keyboard. There isn’t a flag in jpackage that makes a GUI app suddenly get a terminal.
1) osascript -e 'tell application "Terminal" do script "/Applications/MyApp.app/Contents/MacOS/MyApp" activate end tell' 2) Create MyApp.command (make it executable with chmod +x) anywhere (e.g., alongside the .app) with: #!/bin/bash /Applications/MyApp.app/Contents/MacOS/MyApp read -n 1 -s -p "Press any key to close..." Double-clicking MyApp.command opens a Terminal window and runs the app.
If your app must be launched like a normal Mac app (Finder/Dock), don’t use stdin. Replace the BufferedReader code with a small Swing/JavaFX (or even AWT) dialog to capture inputt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.