0

As of 2023, it is trivial to open project in IDEA or VS Code from command line

E.g. on MacOS just add this to your .bashprofile/.zprofile

# Run ms VSCode from terminal
# https://stackoverflow.com/questions/30065227/run-open-vscode-from-mac-terminal
code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;}

#open /Applications/IntelliJ\ IDEA.app/
# https://www.jetbrains.com/help/idea/working-with-the-ide-features-from-command-line.html#c668f7d8
idea () { open -n /Applications/IntelliJ\ IDEA.app/ --args $* ;}

The just idea . to open current folder project in IDEA

Question: How to open Eclipse from command line?

I can narrow down to just maven/gradle project using bash (on macOS/Linux)

Paul Verest
  • 60,022
  • 51
  • 208
  • 332
  • 1
    `open -n -a Eclipse` opens Eclipse. There isn't an argument for a single project `-data /path/to/workspace` opens a workspace. – greg-449 Jul 03 '23 at 16:12
  • Welcome to 2023, it's `eclipse .` (assuming your Eclipse is up to date). – howlger Jul 03 '23 at 19:59
  • @greg-449 `eclipse ` starts Eclipse, if not yet running, and opens the file and `eclipse ` opens the smart import dialog for the directory (_File > Open Projects from File System..._). – howlger Jul 03 '23 at 20:02
  • @howlger Note that you can't just type "eclipse" in a mac terminal / script, it is necessary to use `open` because Eclipse is a mac app not a unix executable. – greg-449 Jul 03 '23 at 20:27
  • @greg-449 But it should be possible via `.bashprofile` to create an alias `eclipse` that passes e.g. the current directory (`.`) as argument to the Eclipse app, right? `eclipse () { open -n -a Eclipse --args $* ;}`? – howlger Jul 03 '23 at 20:37
  • @howlger Sure (it is actually .zprofile these days as macOS defaults to zsh now). – greg-449 Jul 03 '23 at 20:45
  • 1
    @greg-449 Please add an answer with the exact line for `.zprofile` and/or `.bashprofile`. – howlger Jul 03 '23 at 20:52

3 Answers3

1

MacOS applications are typically launched via the open command.

But the open command does not support passing arguments to the application being opened.
However, you can use the open command with the -a option to open an application, and you can specify the application name instead of the path:

open -a Eclipse

This will not open a specific project directly, since Eclipse does not natively support opening specific projects via command line.


An alternative is to create an AppleScript that starts Eclipse and then uses the UI scripting capabilities of AppleScript to navigate the menus to open a project. You can see an example in "Open multiple Eclipse workspaces on the Mac"

It is better to use a shell script as a wrapper around the AppleScript, since AppleScript itself does not support command line arguments.

For instance:

#!/bin/bash

# Check if a parameter was given
if [[ $# -eq 0 ]] ; then
    # No parameter given, ask for workspace
    osascript -e '
    -- Ask for a new workspace path
    set newWorkspace to choose folder with prompt "Select your workspace folder:"
    
    -- Open the selected workspace
    do shell script "open /Applications/Eclipse.app -n --args -data " & POSIX path of newWorkspace'
else
    # Parameter given, use as workspace
    workspace="$1"

    # If the parameter is a dot, expand to current working directory
    if [[ "$workspace" == "." ]] ; then
        workspace="$PWD"
    fi

    # Open the workspace
    osascript -e "do shell script \"open /Applications/Eclipse.app -n --args -data $workspace\""
fi

You can save this script in a file, for example eclipse_workspace.sh. Make sure to make it executable by running chmod +x eclipse_workspace.sh.

You can then call this script from the command line with or without a parameter:

  • With a parameter: ./eclipse_workspace.sh /path/to/workspace
  • With a dot as a parameter: ./eclipse_workspace.sh .
  • Without a parameter: ./eclipse_workspace.sh

Result:

  • In the first case, it will use the given path as the workspace.
  • In the second case, it will use the current working directory as the workspace.
  • In the third case, it will ask you to select a workspace.

You can then define an alias to eclipse_workspace.sh (I like alias e=/path/to/eclipse_workspace.sh).
And then, from command-line, you would type...

e

Or

e .

Or

e /path/to/workspace
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

first of all open your .bashprofile file using a text editor

vi ~/.bashprofile

then add this to your.bashprofile file

eclipse () { open -n /Applications/Eclipse.app/ --args $* ;}

now save the changes, exit the file, and enter this line:

source ~/.bash_profile

from now on, you can easily open Eclipse like the line below:

eclipse .

good luck !

Freeman
  • 9,464
  • 7
  • 35
  • 58
  • please note that the solution I mentioned was for macOS. For Linux, it is straightforward. Just provide the location where Eclipse is installed. If you think I should also include that solution for you, please let me know. – Freeman Jul 07 '23 at 15:23
  • The question was How to open project in Eclipse from command line (not how to edit or content of `.bashprofile`). `open -n /Applications/Eclipse.app/ .` did not worked for me: > The application cannot be opened for an unexpected reason, error=Error Domain=RBSRequestErrorDomain Code=5 "Launch failed." UserInfo={NSLocalizedFailureReason=Launch failed., NSUnderlyingError=0x12b614cd0 {Error Domain=NSPOSIXErrorDomain Code=162 "Unknown error: 162" UserInfo={NSLocalizedDescription=Launchd job spawn failed}}} Awarding bounty anyways, as it was the only answer. – Paul Verest Jul 12 '23 at 12:18
0

This function works both in Macos and Linux :

eclipse(){
    local -a cmd data
    test -v 1 && data=(-data "$(builtin cd "$1"; pwd)")
    [[ "${OSTYPE-}" = darwin* ]] &&
        cmd=(open /Applications/Eclipse.app --args) ||
        cmd=(command eclipse)
    "${cmd[@]}" "${data[@]}"
}

it allows to start in default workspace with

eclipse

start with current directory as workspace

eclipse .

start with a particular directory as workspace

eclipse ../eclipse-workspace2
Philippe
  • 20,025
  • 2
  • 23
  • 32