Tag Archives: jvm

Installing Leiningen on Mac OS

leiningen-full

I’ve been playing with Clojure for quite sometime now. It’s a functional language hosted on the JVM, with a growing community and a very rich ecosystem. You can find libraries for every major problem, but in case you cannot find, you can count on the interoperability with Java will bridge the gap.

Nowadays, every programming language and platform offer a development environment with a build tool as foundation. Take as example Maven, Gradle and Sbt. It isn’t different in the Clojure world. It uses Leiningen as a build and project management tool. So, I would like to share with you how I’ve managed to install and configure Leiningen on my Mac.

First, create the directory /Applications/clojure to store Clojure’s related tools:

$ sudo mkdir /Applications/clojure

Navigate to the new directory, download the Leiningen script and give to the downloaded file permission to execute:

$ cd /Applications/clojure
$ sudo curl -O https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein
$ sudo chmod +x lein

Add the new directory to the $PATH to be able to execute it everywhere in your system. To do that, navigate to the directory /etc/paths.d and create a file named clojure.lein with the content /Applications/clojure:

$ cd /etc/paths.d/
$ sudo echo "/Applications/clojure" >> clojure.lein

If the output of the command above is “permission denied” then you don’t have enough privileges to add something to /etc/paths.d/. In this case, you can add lein exclusively to your user profile.

$ cd ~
$ echo “export PATH=$PATH:/Applications/clojure” >> .bash_profile

Close the current terminal and open a new one to activate the changes. Execute the following command to check whether the $PATH variable was correctly modified to include Leiningen’s path:

echo $PATH

You should be able to find the path /Applications/clojure concatenated within the value.

Finally, execute the command lein for the first time to install Leiningen:

$ lein

It will download the latest version of Leiningen and make you ready to program in Clojure. Start the Clojure REPL with the following command:

$ lein repl

Have fun with Clojure and Leiningen!

What Comes Next

In my previous post I explained why I left JavaEE behind. Now, I’m going to explain my reasoning process to come up with what I’ll learn, teach and use next. The criteria I’m using are:

  • Cloud friendly: the technology should be ready to scale horizontally, without constraints, additional products or exponential use of resources.
  • Learning curve: I should be able to learn and teach fast even if it requires me to change the way I think about programming. I recognize I have lots of new concepts to learn before I realise the advantages of other technologies.
  • Performance: everything I write I want to be faster than any interpreted language. I know that premature optimization is a bad idea, but I need a technology that even when I decide to postpone optimizations it will be fast enough.
  • Community: the community doesn’t need to be big, but it should be active and kind with newcomers. The majority of libraries they produce should be open source.
  • Reusability: I should be able to reuse the libraries I’m used to, or find equivalent ones.
  • Coverage: I should be able to write the same kind of software I’m used to and not be limited if I decide to do more.
  • Documentation: the technology should be well documented, with books, websites, blogs, wikis and teaching materials.
  • Development Stack: the stack should implement MVC for web applications, database migration, database abstraction, SSL, authentication, authorization, REST web services, etc.

The fundamental choice starts with the programming language. It must support functional programming in order to be cloud friendly, but it also has to avoid mutable state to prevent concurrency issues and make it difficult for programmers to write code with side effects. I already use Java, but at this point we have to eliminate it because mutable state is the default behaviour in the language. To avoid it, we have to write a whole bunch of additional code that needs to be tested as everything else. Java requires the use of design patterns to overcome the deficiencies of the language. Static analysis tools, such as SonarQube, are required to keep the code safe, but, unfortunately, they require a significant effort that has nothing to do with the business problem we are solving.

“Most people talk about Java the language, and this may sound odd coming from me, but I could hardly care less. At the core of the Java ecosystem is the JVM.” James Gosling (2011, TheServerSide)

Despite the Java programming language being out of the picture, the Java Virtual Machine (JVM) is still relevant. It’s a portable and mature platform capable of running multiple programming languages in several operating systems with a transparent memory and thread management, with performance peaks compared to C/C++. What Java doesn’t do for us, other programming languages do, running in the same virtual machine and reusing the existing Java ecosystem, which is huge! Therefore, languages that have their own compilers and independent virtual machines are discarded because they hardly will reach the maturity and popularity of the JVM and the Common Language Runtime (CLR). So, at this point we discard Erlang, Harskell, Go, and all other languages that don’t run in the JVM or CLR.

I have mentioned CLR, the .net runtime, but I have no experience with it so far. So, I have to narrow my choices to JVM hosted languages. I couldn’t find any official  or reliable survey about the popularity of JVM languages, but I did find several pools showing that Groovy, Scala, Clojure and JRuby are indeed the top four JVM hosted languages, in no specific order.

scala-groovy-clojure

Groovy‘s popularity is due to the fact that it looks very much like Java, but without its cumulative historical problems. Therefore, Groovy’s learning curve for Java developers is by far the lowest one compared to Scala and Clojure. Scala comes next with its richer type system and object orientation. We’re able to map our Java knowledge within Scala, but this language is so full of possibilities that it’s the hardest one to master. We have problems to read other people’s code because developers have too much freedom to express themselves. Clojure, on the other hand, is the hardest one to start programming because of its radical differences from Java, but it’s the easiest one to master because of its simplicity. We do a lot more with less code and the code is readable as long as you know functional programming principles. Since JRuby didn’t perform well in the surveys above, I’m discarding it for the moment.

The chart bellow shows job trends in the US, according to indeed.com. It actually reflects the size and influence of the community gravitating those languages. Groovy has been performing well since the beginning, but it is now threatened by Scala, although it isn’t clear yet which one will stand out. The interest for Clojure is constantly and shyly increasing, as functional programming becomes popular and the learning material available helps to reduce the introductory learning curve. In any case, it still has a long way to go.

Groovy, Scala and Clojure have at least the same coverage as Java, with the advantage of writing less to do more. There is absolutely no problem that can be solved only by using Java. Actually, concurrency problems are far more complex to solve with Java, making those alternatives much more interesting.

In order to master those programming languages, I had a look at the volume and quality of the documentation available. This is very hard to measure. For those who like numbers, I have compiled the following table:

Language Appeared Google StackOverflow
Groovy 2003 602K 11468
Scala 2003 1.510K 35207
Clojure 2007 350K 9278

The problem is that this table can be interpreted in many different ways:

  1. These numbers are far from precise. They change everyday because of the nature of the internet.
  2. Looking at the volume, we might conclude that the more entries we get the more documentation we can find, but it can also be a sign of complexity, taking a lot more documentation to explain a thing. Therefore, the fact that Clojure has less entries doesn’t mean it is less documented than Scala or Groove.
  3. Some languages are older than others, accumulating exposure to the community, thus producing more content. But in this case, older content are counted but hardly relevant nowadays.

I can say that the documentation I found was fairly good enough to address all my questions so far.

The last point is the development stack, a set of libraries and frameworks covering most of the needs of a regular enterprise developer. The following table shows a non-exhaustive list:

Feature Groovy Scala Clojure
Build Tool Gradle SBT Leiningen
Persistence Grails Slick HoneySQL
Database Migration Grails Play Framework Joplin
MVC Grails Play Framework
Lift
Compojure + Ring
Security Spring Security SecureSocial
Silhouette
Buddy
Testing Spock Scala Test Expectations
IDE Support IntelliJ
Eclipse
IntelliJ
Eclipse
IntelliJ
Eclipse
LightTable
Emacs
NightCode
Vim
RESTful Web Services Grails Spray
Play Framework
Liberator

Notice that Grails appears several times in the Groovy column. It’s a web framework offering a good deal of productivity thanks to a Convention-over-Configuration approach. The same happens in the Scala column with several occurrences of Play Framework. While the approach followed by Groovy and Scala offers more productivity and reproducibility, it also reduces the flexibility of the architecture, making it hard to replace an inefficient part by a more efficient one. Clojure is more concerned about the architecture and offers a separate library for every feature. In case a competitor library becomes more efficient than the one we are using, we can easily integrate the new library and gradually replace the inefficient one.

My strategy to use those three technologies from now on is the following:

  • Groovy: when I find a chaotic and inefficient JavaEE application, I will propose to migrate it to Groovy + Grails. It will make the project economically viable again and recover the time wasted with complexity. The team can start writing Groovy code right away in the same project, gradually replacing the Java code and JavaEE dependencies.
  • Scala: the main advantage of the Scala stack is its reactive platform, offering an unprecedented performance boost for concurrent applications. So, when performance is one of the main requirements of the application and the team is smart and organized, I will suggest Scala as the way to go.
  • Clojure: For everything else I will suggest Clojure, which is very productive, simple and has an excellent performance. That’s by far the best programming experience I ever had.

In summary, I still use JavaEE for existing well designed applications, but I will use Groovy to save chaotic JavaEE applications from complete failure. Scala and Clojure will be used for new projects, depending on their characteristics and context of use.

Installing Intellij IDEA on Mac and Ubuntu

It has been a year since I moved my professional and community development projects from Netbeans to IntelliJ IDEA. Netbeans is still a great IDE and I recommend it over any other open source alternative, but the productivity brought by IntelliJ is so great that the time I’ve saved using this IDE already paid off.

I have IntelliJ installed at home and at the office. It’s the same license but the deal is: you can install it in several computers but use one installation at a time. I use Mac and Ubuntu at home and my experience installing IntelliJ in those platforms was the following:

Installing on Mac

I’m not really going into step by step here. IntelliJ is pretty easy to install on Mac, but I had a problem with the JDK and I’m going to focus on that now. IntelliJ uses the JDK distributed by Apple by default, which is a JDK 6 implementation. Well, this is not a big deal, since we can install the most recent JDK and configure our projects in the IDE to use it instead. But, for some unexplained reason, I couldn’t configure the IDE to start the application server in a JDK different from the one used by IntelliJ (JDK 6). In the image below, you can see I’ve configured JDK 8 to run WildFly, which requires JDK 7 or superior, but it didn’t work.

wildfly-configuration

So, I had to change the JDK used by the IDE. For that, I:

  1. closed IntelliJ;
  2. went to the folder where all applications are installed (/Applications) and selected the file “IntelliJ IDEA ##.app”;
  3. accessed the context menu (mouse click with two fingers) and selected “Show Package Contents”;
  4. opened the file “/Contents/Info.plist” and
  5. located the JVMVersion to change its correspondent value to 1.8*.

After this configuration, I could finally make IntelliJ run Wildfly.

Installing on Ubuntu

The installation on Linux is traditionally more complicated. I wonder why people complain about the low number of Linux desktop users. 🙂 The IntelliJ IDEA download page mentions only two steps:

  1. unpack the “ideaIU-XX.Y.Z.tar.gz” file using the command “tar xfz ideaIU-XX.Y.Z.tar.gz” and
  2. run “idea.sh” from the bin subdirectory.

However, this instructions don’t deliver IntelliJ as delivered in other platforms. People don’t go to the installation folder and execute the file idea.sh. They either create a desktop icon or add the bin directory to the path, but these steps are missing. So, in my understanding, the installation is not completed. To launch IntelliJ from anywhere in the command prompt:

Become the root user:

sudo -i

Move the unpacked folder to “/opt/idea”:

mv ideaIC-XX.Y.Z /opt/idea

Edit the file .bashrc:

gedit ~/.bashrc

Add the following line to the end of the file:

export PATH=/opt/idea/bin:$PATH

Log out and log in to the change take effect.

To add the launcher icon on the desktop, there is a soft and a hard way.

The Soft Way

Fortunately, IntelliJ can help you once you run it for the first time. In the welcome window, select “Configure”:

intellij-configure

And then select “Create Desktop Entry”.

intellij-configure-desktop-entry

That’s it!

The Hard Way

As a good Linux user, you may prefer doing it the hard way, as follows:

Create a desktop file:

cd /opt/idea
gedit idea.desktop    

Copy the content bellow to the file:

      [Desktop Entry]
      Name=IntelliJ IDEA 
      Type=Application
      Exec=idea.sh
      Terminal=false
      Icon=idea
      Comment=Integrated Development Environment
      NoDisplay=false
      Categories=Development;IDE;
      Name[en]=IntelliJ IDEA

Install the desktop file:

desktop-file-install idea.desktop

Create a symlink:

cd /usr/local/bin
ln -s /opt/idea/bin/idea.sh /usr/local/bin/idea.sh

Finally, display the idea icon in dash:

cp /opt/idea/bin/idea.png /usr/share/pixmaps/idea.png

At this point, you will finally feel IntelliJ as an application, integrated with the desktop and always ready to be executed.

JavaFX Must be a Joke

More than 3 years ago, I wrote the post “Has JavaFX a Strategy?” saying:

Don’t you think that the fastest way to spread the JavaFX adoption is allowing the improvement of existing applications? Why to spend a lot of resources to drag an applet from the browser to the desktop if we need the network anyway?

At that time, JavaFX Script was the only alternative to develop richer Java desktop applications. Guess what Oracle did right after Sun’s acquisition: they stopped evolving a pretty stupid new language which looked more like Json, which is designed for data; migrated the whole thing to a new Java library; and allowed the integration with legacy code (i.e. Swing applications). If they didn’t get inspired by my old blog post, then they just followed the sane common sense.

Meanwhile, Adobe gave up Flex and Microsoft gave up Silverlight. Strangely, Oracle kept insisting on JavaFX. We can see a lot of JavaFX demos out there, but not so much production-ready apps.  That’s a shame after all these years. The single example I have of a real world application is from a Belgian company called Health Connect. That’s all! Anybody else?! JavaFX evangelists would give a lot of confidence to Java developers if they promoted JavaFX applications in production. We have the impression that those evangelists are paid to have fun. This is really unexpected from Oracle, which looks like a very serious company. Take the example of Apple. They are very efficient on that. Every time they present their gadgets they also present outstanding apps developed for that device. It definitively makes developers excited!

Fake announcement of JavaFX for
the mobile world in 2009.  Since then
zero apps shipped so far!

I was about to be excited when Oracle announced JavaFX running on OS6 with an app called JavaFX Ensemble, but when I realised that the goal of the app was simply to add more demos to the shelf, I got immediately frustrated 🙁 Is it so difficult to convince a company out there to write a useful app in JavaFX and make it available on OS6?! Come on! We are tired of demos! In fact, this “alpha” project is not enough to convince us that Java is going mobile. In my previous post I made the following prediction:

“… in 3 or 4 years there will be no Java on devices, and the development community will have enough knowledge to live with that. Therefore, Java will be basically a server-side technology.”

Now, let’s imagine that JavaFX is a great technology and everybody is adopting it. Is everything ok now? Nooooooo! Even if everybody is convinced about JavaFX, there is no stable, reliable and easy to use JVM out there for the client side at the moment. Therefore, we cannot efficiently distribute JavaFX apps on desktops. We have to be within a company, with full control over the network, to manage the installation, security and upgrades of the JVM in all desktops in order to distribute the application. Well, that’s silly :-/

Error during my latest attempt to update my JVM.
Message: “Failure to download required files for installation.”

I’m quite confident to advise you to leave JavaFX aside and go for HTML5. Most of its features are already cross-browser compatible and it’s possible to build amazing user interfaces with that. Client-side Java is over, so get used to an exclusively java server-side world soon. Surprisingly, it doesn’t make me sad, but happier 🙂

The Consequences of Deferring Project Jigsaw

Mr. Mark Reinhold has announced in July 2012 that they were planning to withdraw Project Jigsaw from Java 8 because Jigsaw would delay its release, planned for September 2013 (One year from now). This date is known because Oracle has decided to implement a two years roadmap planning for Java, so September 2013 is actually 2 years after the release of Java 7.

According to Jigsaw’s website…

“The goal of this Project is to design and implement a standard module system for the Java SE Platform, and to apply that system to the Platform itself and to the JDK. The original goal of this Project was to design and implement a module system focused narrowly upon the goal of modularizing the JDK, and to apply that system to the JDK itself. The growing demand for a truly standard module system for the Java Platform motivated expanding the scope of the Project to produce a module system that can ultimately become a JCP-approved part of the Java SE Platform and also serve the needs of the ME and EE Platforms.”

They also say:

“Jigsaw was originally intended for Java 7 but was deferred to Java 8.”

Now they want to defer it to Java 9 🙁 More details of their decision making are available in a Q&A post on Reinhold’s blog. You may read and follow the discussion there. Here is my opinion:

Without Jigsaw, I believe that it’s very difficult to put Java everywhere. Without Jigsaw, the idea of multi-platform is getting restricted to servers in a age of smartphones and tablets. Jigsaw may be “late for the train”, but it is letting Java late for the entire platform ecosystem.

Deprecated installation screenshot
Observing the market, we can see that development is becoming platform-dependent (iOS, Android, etc.) Only Java can beat this trending because of its large experience on multiplatform implementation, and the time to do it is NOW! Otherwise, in 3 or 4 years there will be no Java on devices, and the development community will have enough knowledge to live with that. Therefore, Java will be basically a server-side technology.

The reasoning behind my prediction is the following: mobile devices are limited in terms of resources and a modular JVM would allow the creation of tailored JVM considering the constraints of each device. I put myself in the shoes of those devices manufacturers: “I wouldn’t distribute something in my products that might impact negatively the user experience in terms of performance”. That was the argument (at least the public one) Apple used to avoid distributing the Flash plugin for iOS’s browser. Probably because of that, Adobe definitively gave up Flash on mobile devices. A modular JVM would simplify a lot Oracle’s negotiation with many device players. It would be reasonable for Apple to include Java as a language for iPad and iPhone applications; Google would finally embed the JVM into Android to evolve faster with new Java language features, getting busy just with a module to extend the JVM to specific Android’s capabilities; it would be even possible to save Nokia from bankruptcy 😀

You may wonder whether Apple and Google would ever adopt JVM as a standard runtime platform. Have you heard about opportunity cost? It states that our current choices and activities are actually blocking other possible choices and activities. The tricky part is to chose the opportunity that is least costly or with the highest profit. Having said that, we can see the scenario considering that Java was not an option because it wasn’t modular when those companies made their decisions. If Java was modular and Apple had adopted it, iOS platform would have at least three times more apps than Android. “Java” was in Google’s strategy to catchup with Apple. Only Java could allow Google to do it in such a short period of time. So, it’s not so simple to ignore Java.

Now, Oracle vs. Google: Of course the effort to move Java forward should be economically viable, and in order to use Java, Google would have to spend some money. Unfortunately, Oracle and Google work with different currencies. While Oracle thinks in terms of licenses, Google thinks in terms of advertising. These currencies are incompatible, very difficult to convert, because while license is cost, advertising is profit. Therefore, Oracle would never reach a deal increasing Google’s cost, but it would be possible to get a deal decreasing Google’s profit. In other words, Oracle could have a percentage of Google’s profit on advertising sold through Java apps in order to make Java available for Android. Google makes this kind of deal with a lot of companies like Yahoo, AOL and others. Why not with Oracle?

If Oracle doesn’t give all resources that the JDK team needs to make Jigsaw a reality in Java 8, Oracle will be completely out of the pervasive game very soon. Without breaking the JDK into manageable and efficient pieces, Oracle won’t have arguments to convince the industry that Java is the way to go on the long run.

Before deciding to drop Jigsaw out, I beg Oracle to think about the consequences! They must ignore the fixed release roadmap and accept the difficulty of the task. We can stay happy with Java 7 (it’s not widely adopted anyway) as long as Jigsaw is on the way to Java 8. This fixed release cycle can actually come back after Java 8.

I would love to be wrong and be taken by surprise with an official Oracle’s announcement of the definitive support for JavaFX on Apple and Android devices during the next JavaOne 😉 However, I think the likelihood is very low 🙁