Start IntelliJ IDEA from the command line

2017-02-27  |   |  java   ide   tool  

You can start IntelliJ IDEA from the command line which is handy when you live in a terminal like me. But you need to enable that feature.

Open IntelliJ IDEA, go to Tools->Create Command-Line Launcher... and optionally adjust the location and name of the script that will start IntelliJ IDEA. Voilà! Now from your command line, you can type:

  • idea . to open the project in the current directory
  • idea pom.xml to import the Maven project
  • idea diff <left> <right> to launch the diff tool.

The generated script has an annoying flaw though, it does reference your preference and cache directories in a hard coded fashion. And for some reason the IntelliJ folks embed the version number in these directories (e.g. IdeaIC2016.2) That's annoying as it will likely break the minute you move to another (major?) version.

Antonio has a solution for that which is a simpler and more forgiving script in good anti-fragile fashion. The script is not generic and only runs for macOS.

#!/bin/sh

# check for where the latest version of IDEA is installed
IDEA=`ls -1d /Applications/IntelliJ\ * | tail -n1`
wd=`pwd`

# were we given a directory?
if [ -d "$1" ]; then
#  echo "checking for things in the working dir given"
  wd=`ls -1d "$1" | head -n1`
fi

# were we given a file?
if [ -f "$1" ]; then
#  echo "opening '$1'"
  open -a "$IDEA" "$1"
else
    # let's check for stuff in our working directory.
    pushd $wd > /dev/null

    # does our working dir have an .idea directory?
    if [ -d ".idea" ]; then
#      echo "opening via the .idea dir"
      open -a "$IDEA" .

    # is there an IDEA project file?
    elif [ -f *.ipr ]; then
#      echo "opening via the project file"
      open -a "$IDEA" `ls -1d *.ipr | head -n1`

    # Is there a pom.xml?
    elif [ -f pom.xml ]; then
#      echo "importing from pom"
      open -a "$IDEA" "pom.xml"

    # can't do anything smart; just open IDEA
    else
#      echo 'cbf'
      open "$IDEA"
    fi

    popd > /dev/null
fi

The GitHub gist version of this script. It does not offer the call to IDEA's diff though. I'm from an era where we did resolve > based diff conflicts in Notepad so that does not bother me much.

I think I'll go for Antonio's solution, that will avoid some nasty WTF moments when the preference directory moves and I will have forgotten all of this.


Autocompletion is crap

2013-05-28  |   |  java   tool   ide  

I can't believe autocompletion is still so stupid in 2013. Alright, I said it, I feel better. It's actually not quite true and a lot of smart things are going on in Eclipse or IntelliJ IDEA but completion remains incredibly stupid when it comes to discovering an API and offering the most appropriate choice.

It is very important for fluent APIs tailored to offer only the APIs that make sense in a particular context. So when the autocompletion offers you the full monty of useless methods of Object, it kills the hours of work an API designer put into his fluent API and its discoverability. Seriously notify(), notifyAll() and wait() x 3!

I developed, help develop or gave advice on a number of fluent APIs in Java including:

  • Hibernate Search and Hibernate Validator programmatic mapping API,
  • an upcoming Hibernate OGM programmatic configuration API
  • ShrinkWrap API
  • Hibernate Search Query DSL API

So I feel very personally offended when something gets between my work and the end user.

Eclipse

I use Eclipse these days and the sort by relevance works only in so far as relevance is synonymous to alphabetical. In other words it sucks... but in a predictably alphabetical way.

Eclipse autocompletion

IntelliJ IDEA

IntelliJ IDEA is smarter as it puts in bold the methods hosted on the type and leave supertype methods in regular.

IntelliJ IDEA autocompletion

If you dig enough though, you can find a setting

Sort lookup items lexicographically

by opening up preferences and search of autocompletion. Weirdly enough, if you uncheck this box, IntelliJ IDEA does the better thing and put the elements in bold next to each others.

IntelliJ IDEA smarter
autocompletion

And that concludes this half rant, half tip, half call to action to IDE developers. Yes I know but it takes at least 3 halfs for a good cocktail :)


Setting global environment variables in IntelliJ IDEA and other test config goodies

2012-05-09  |   |  tool   ide  

When you run a test from IntelliJ idea, you can customize some of the settings by selecting the list of tests and click Edit Configurations. From there you can change things like:

  • under which module classpath the test is run
  • the working directory the test should run from
  • virtual machine parameters
  • specific environment variables to use

In Hibernate OGM, we use the same test site for all NoSQL solutions. Changing the module classpath is useful in this situation. We also let you refine via an environment variable which hostname runs say MongoDB.

Unfortunately in Mac OS X, graphic applications do not inherit your .bash_profile config. I know, stupid, but what can you do against the Empire? Workaround!

You can set environment variables from each test but it gets tedious quickly. Alternatively, you can set an environment variable globally in IntelliJ - what they call parent environment variables. Go to Preferences and search for Path Variables. Set your global environment variables here and you are good to go. Note that you can ask a test not to inherit these global variables if you want to - in the test configuration.


Update:
It turns out I was wrong. Paths Variables is not where global environment variables can be set. If you know where, please let me know.

Update 2:
To set an environment variable visible by applications in Mac OS X, use

launchctl setenv MYPATH myvar

then restart your IDE (you might need to log out and back in as well).

Thanks @aagahi for the info. This is not great as it's remote from my IDE but at least that works. BTW, launchctl is supposed to set the data in /etc/lanuchd.conf or $HOME/.launchd.conf. Not on my system. If anyone knows where the data is put, I'm interested.


Notes to JetBrains:

  • your environment variable UI is plain counter productive: either give us a text free area or make tabs and co working
  • an option to inherit the .bash_profile configuration would be awesome :)


Mac OS X and IntelliJ crashes

2010-12-07  |   |  apple   ide   tool  

I've struggled in the past several weeks with repeated crashes from IntelliJ IDEA on Mac OS X. I suspect it started right after the upgrade to 1.6.5 and the latest Java VM update.

It seems it was cause by a couple of things.

Job scheduler crash
If your crash report shows something like
Thread 55 Crashed: Java: JobScheduler pool 1/2
This is a bug in the garbage collector. You need to pass an additional VM option to fix it
-XX:-ReduceInitialCardMarks
Open /Applications/IntelliJ IDEA x.y.z.app/Contents/Info.plist and update the VMOption key
<key>VMOptions</key>
<string>-XX:-ReduceInitialCardMarks -Xms128m -Xmx512m -XX:MaxPermSize=250m -ea -Xbootclasspath/a:../lib/boot.jar</string>
Font corrupted
Not sure if that was related to the previous issue but I initially had crashes with a different report involving directly the Garbage Collector (GC). My IntelliJ IDEA was using the Inconsolata font which apparently was somehow corrupted.

Removing fonts from ~/Library/Fonts solved the issue. What you can do is move them to a different directory and slowly re-inject them to find the culprit.

I am quite happy all this is behind me. I chose:

  • A Java VM
  • On Mac OS X
  • with IntelliJ IDEA

to precisely avoid this kind of bad experience and time waste :) Back to normal.


IntelliJ's Live Template

2010-08-25  |   |  ide   java   tool  

If you remember, I like to write my getters this way (long story here)

public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
private String name;

I have been doing it manually for a while but it turns out IntelliJ has a super nice feature for that: Live Templating which allows you to use parameterized and contextual templates to generate code.

Go to your Settings->Live Templating and create a new one.

Add this template:

public $TYPE$ get$UpperName$() { return $lowername$; }
public void set$UpperName$($TYPE$ $lowername$) { this.$lowername$ = $lowername$; }
private $TYPE$ $lowername$;

Then edit the variables:

  • TYPE: the expression should be classNameComplete() to refer to the class name context
  • lowername: the expression should be decapitalize(UpperName). Also tick skip if defined to not have to validate the computed value (this saves you one key stroke).

I've named my Live Template "get". When I type "get" and TAB, I am asked to type the property type (which is suggested). The second variable asked is the capitalized property name. Then the Live Template infers the rest (decapitalize my property name to fill up lowername).

Pretty nice feature!


Name: Emmanuel Bernard
Bio tags: French, Open Source actor, Hibernate, (No)SQL, JCP, JBoss, Snowboard, Economy
Employer: JBoss by Red Hat
Resume: LinkedIn
Team blog: in.relation.to
Personal blog: No relation to
Microblog: Twitter, Google+
Geoloc: Paris, France

Tags