Java 8 for Eclipse Kepler via the Eclipse Marketplace

Eclipse Foundation Announces Java 8 Support! One day after my post about Java 8 in Eclipse Kepler (4.3) and Eclipse Luna (4.4), the Eclipse Foundation announced official support for Java 8 in Eclipse Kepler. Here is there blog post straight outta Ottawa, Canada:

http://eclipse.org/org/press-release/20140402_eclipse_foundation_announces_java8_support.php

You can now install Java 8 support to Eclipse Kepler (4.3) via the Eclipse Marketplace:

2014-04-0Eclipse

A little bit late (!), but finally the easiest way to use Java 8 in Eclipse.

Best regards,
Thomas

Java 8 in Eclipse (Kepler and Luna)

Java 8 is officially available since a couple of days. It was released on March 18th this year. Yeah! But – Eclipse, the biggest and most popular Java IDE is a little bit behind the schedule (at least IMHO). There is no official Eclipse version for Java 8 right now!

But there are two other thing: an up-to-date nightly build of the new Eclipse version Luna which will be released with Java 8 support and a patch for the current Eclipse version Kepler! Since it is sometimes (= always) hard to find the correct Eclipse version on eclipse dot org or anything useful at all, here is how to do it.

Java 8 in Eclipse Juno (4.2)

If you still use Eclipse Juno (4.2), you use an old version of Eclipse without Java 8 support and I strongly recommend to use a new version (as described below). If you cannot do that (or don’t want to do that) here is an old tutorial from me how to setup Java 8 in Eclipse Juno: http://tuhrig.de/java-8-in-eclipse-juno.

Java 8 in Eclipse Kepler (4.3)

2014-03-31 12_16_15-Program Manager

Kepler (4.3) is the current version of Eclipse. You can download it from http://www.eclipse.org/downloads. After you downloaded it, it will not have Java 8 support right out of the box! To add it, you have to install a feature patch:

  1. Download and install Java 8 (e.g. from Oracle JRE/JDK or OpenJDK)
  2. Add it to Eclipse:
    2014-03-31 12_25_37-Java - Java8Project_src_Test.java - Eclipse
  3. Install the feature patch via Help > Install New Software...:

    2014-03-31 12_04_14-Java - test_src_test_Hello.java - Eclipse

You have to restart Eclipse during the installation and accept a license and click several next-buttons. But after you installed the feature patch, you have Java 8 support in Eclipse Kepler (4.3). If you have problems with the installation process, download a new Eclipse Kepler version.

2014-03-31 12_34_14-

Java 8 in Eclipse Luna (4.4)

2014-03-31 16_18_56-C__Users_tuhrig_AppData_Local_Temp_7zOADE2.tmp_repositories • - Sublime Text 2 (

Luna (4.4) is the upcoming version of Eclipse. It will be released this summer and it will contain Java 8 support. But you can already download some nightly builds of it which contains Java 8 support.

You can download Eclipse Luna here. Just make sure you download the correct build version, since not all builds have Java 8 support already! When you download the correct version, you can just run Eclipse and use the new Java 8 features (you also need to install Java 8, e.g. from Oracle JRE/JDK or OpenJDK).

2014-03-31 12_38_38-Eclipse Project Downloads

By the way, some nightly builds still contain test errors. This is due to the fact that Eclipse Luna is still in development. But to play around with Java 8 or for a small project it should already be good enough.

2014-04-01 07_42_46-Eclipse Project

A simple Java 8 example

To test your Eclipse IDE, here is a very simple Java 8 example:

Troubleshooting

If you have problems with Java 8 in Eclipse check your project settings. If your compliance level is not set to 1.8, you cannot use Java 8. And if you don’t even have the compliance level 1.8 then your Eclipse somehow doesn’t support Java 8 and something went wrong. In this case, get yourself a new Kepler version and install the feature patch or download an appropriate Luna build as described above.

2014-03-31 12_12_56-Java - test_src_test_Hello.java - Eclipse SDK

Resources

Best regards,
Thomas

Media Night Apps

This year there is something new on the Media Night, the student fair at our university.

The booklet is dead!

In its past, the Media Night had had a printed booklet with its program and time table in it. The booklet was produced by one of our printing faculties for which the HdM is pretty famous. It was distributed for free during the evening of the fair.

old_bookelt

Long live the booklet!

However, since this semester the good old booklet is history! Instead of it, the Media Night has a brand new app with all projects, students and even an indoor navigation. You can find it in Apple’s App Store and Google’s Play Store.

Most innovative projects

But why only have one app if you could have two? In case you have an Apple iPad or iPhone you can get an app about the 10 most innovative projects of this Media Night. And – TechTrends is one of it!

innovative_app

I hope you enjoy the apps, the projects and the evening.

Best regards from the Media Night,
Thomas

Writing an online scraper on Google App Engine (Python)

Sometimes you need to collect data – for visualization, data-mining, research or whatever you want. But collecting data takes time, especially when time is a major concern and data should be collected over a long period.
Typically you would use a dedicated machine (e.g. a server) to do this, rather then using your own laptop or PC to crawl the internet for weeks. But setting up a server can be complicated and time consuming – nothing you would do for a small private project.

A good and free alternative is the Google App Engine (GAE). The GAE is a web-hosting service of Google which offers a platform to upload Java and Python applications. It comes with its own user authentication system and its own database. If you already have a Google account, you can upload up to ten applications for free. However, the free version has some limitations, e.g. you only have a 1 GB database with a maximum of 50.000 write-operations per day (more details).

One big advantage of the GAE is the possibility to create cron-jobs. A cron-job is a task that is executed on fixed points in time, e.g. all 10 minutes. Exactly what you need to build a scraper!

But let’s do it step by step:

1. Registration

First of all, you need a Google account and you must be registered by the GAE. After your registration, you can create a new application (go to https://appengine.google.com and click on Create Application).

01_gae_registration

Choose the name for your application wisely, you can’t change it later on!

2. Install Python, GAE SDK and Google Eclipse plugin

To start programming for GAE, you need to set up some simple things. Since we want to develop an application in Python, Python (v. 2.7) must be installed on your computer. Also, you need to install the GAE SDK for Python. Optional, you can also install the Google plugin for Eclipse together wit PyDev which I would recommend, because it makes life much easier.

3. Create your application

Now you can start and develop your application! Open Eclipse and create a new PyDev Google App Engine Project. To make a GAE application, we need at least two files: a main Python script and the app.yaml (a configuration file). Since we want to create a cron-job, too, we need a third file (cron.yaml) to define this job. For reading a RSS stream we also use a third-party library called feedparser.py. Just download the ZIP-file and unpack the file feedparser.py to your project folder (this is ok for the beginning). A very simple scrawler could look like this:

Scrawler.py

app.yaml

Note: The application must be the same name as your registered on Google in the first step!

cron.yaml

Done! Your project should look like this now (including feedparser.py):

02_gae_project

4. Test it on your own machine

Before we deploy the application on GAE, we want to test it locally to see if it is really working. To do so, we have to make a new run-configuration in Eclipse. Click on the small arrow at the small green run button and choose “Run configurations…”. Then, create a new “Google App Engine” configuration and fill in the following parameters (see the pictures):

Name:
GAE (you can choose anything as name)

Project:
TechRepScrawler (your project in your Eclipse workspace)

Main Module:
C:Program Files (x86)Googlegoogle_appenginedev_appserver.py (dev_appserver.py in your GAE installation folder)

Program Arguments:
--admin_port=9000 "C:UsersThomasworkspace_pythonTechRepScrawler"

04_gae_run_config_1

04_gae_run_config_2

After starting GAE locally on your computer using the run configuration, just open your browser and go to http://localhost:8080/ to see the running application. You can also go to an admin perspective on http://localhost:9000/ to see, e.g. some information about your data.

5. Deploy your application to GAE

The next – and last step! – is to deploy the application on GAE. Using the Google Eclipse plugin, this is as easy as it can be. Just click right on your project, go to PyDev: Google App Engine and click upload. Now your app will be upload on GAE! On the first time, you will be asked for your credentials, that’s all.

05_gae_upload

06_gae_deployed

Now your app is online and available for every one! The cron-job will refresh it every 10 minutes (which just means, it will surf on your site like every other user would do it). Here’s how it should look:

07_last

Best regards,
Thomas Uhrig

Java 8 in Eclipse (Juno)


Note: Here is an up-to-date tutorial for the new Eclipse versions Kepler and Luna: http://tuhrig.de/java-8-in-eclipse-kepler-and-luna


Since last July the new Java 7 is generally available. However, even the newer Java 8 is already available as an Early Access Preview. It can be download as part of the OpenJDK including lambda-support. To use it with Eclipse, some additional steps are required. Here’s how it goes:

  1. Download the JDK from http://jdk8.java.net/lambda and install it.
  2. Create a new Java project in Eclipse.
  3. Change some project settings in Eclipse in order to use the new javac compiler of Java 8:
    1. Click right on your project and select Properties.
    2. Select Builders on the left side and uncheck the default Java Builder.
    3. Click on New and select Program as the type of the new builder.
    4. Open javac from the JDK 8 on your disk (it’s in the folder bin).
    5. Configure the new builder by providing some arguments:
    6. Select the tab Build Options and check the During auto builds box, to let the builder run automatically.
  4. Done! Now you can write and execute Java 8 code!

The main feature of Java 8 is the ability to write lambdas and use a functional-like programming style. This can be very useful, especially in GUI-programming. It can reduce the amount of code, because anonymous classes that only implements a listener-interface are avoided. Here’s a very simple example:

The code creates a common JFrame with two buttons. To each button an action-listener is added. But instead of implementing an anonymous class, a method reference is passed in the first case and a lambda in the second case. This reduces the amount of code compared with the old solution that looked like this:

However, the Java compiler will create exactly such a thing of the lambda expression or function reference. Therefore, the passed method must fulfill the interface described by ActionListener. Also, we can only use lambdas instead of interfaces that only describe one single method. Such interfaces are called functional interfaces.

Since Eclipse Juno is made for Java 7 it will not recognize the new lambda syntax. Hence, the editor will mark parts of the code as red errors. But it’s still possible to compile and to execute the code using the new javac compiler form Java 8.

Best regards,
Thomas Uhrig

Thomas’ Functional Interpreter

I just finished a small functional interpreter for an university course. To be precise, the interpreter itself is not functional, but it understands a functional, LISP-like language/syntax. The whole project is written in Java 7 and provides an interface to all Java classes on the class-path, too.

I also tried to create a small IDE in Swing. I built an editor with syntax-highlighting and code-completion (thanks to the RSyntaxTextArea project), an environment inspector and a small debugger to keep track of the stack. Take a look at it.

I called it ThoFu for Thomas’ Functional Interpreter. I think it teats good… You can get some brief impressions from the slideshow below.

The whole project is open, you can find it as an Eclipse project on my Bitbucket account. It contains the actual code, all libraries, screen-shots, unit tests and (of course) some documentation like UML diagrams. Here are the links to the GIT repository:

Bitbucket: https://bitbucket.org/wordless/thofu-interpreter
Git: https://bitbucket.org/wordless/thofu-interpreter.git

Here is the executable JAR-file (Note: You need Java 7). Just download it, double-click it and go-ahead:

Executable program: ThoFu Interpreter.jar
Read-Me: README.md

And this is how it goes. First a simple “hello world” program:

Now, a more sophisticated one. Here’s a quicksort-algorithm for my interpreter. It contains some user-defined functions, multi-line statements and some comments.

And this is how the Java API works. The code will create a JFrame with a JButton and a JLabel. When you press the button, the listener will change the text of the label from “click me” to “hello“. The syntax is inspired by the Java Dot Notation by Peter Norvig. I used the Apache Commons Lang library for the reflection-tricks to load class and so on.

You can just copy and paste the code snippets into the editor or save them as common text files and open them. They are also in the repository. The JUnit tests contain many more examples as well.

For those who are interested in the topic, you can find my resources (libraries, tool and literature) in the Read-Me.

Best regards,
Thomas Uhrig

Tools to understand Java code

I have recently read a question on Stackoverflow whether there are any tools to help understanding Java code. Somebody asked it, because he started to use a pen and a sheet of paper to get the clue of some code given to him. Not uncommon.

But the problem was, that nobody tried to answer the question, instead giving him tips like “learn Java” or “read tutorials”. I think they have misunderstood the question – because there are definitely tools for that:

  • A good IDE just like Eclipse is one basic tool to understand code. It helps you in various ways:

    • It shows the structure of folders and files of the project. You can easily get an overview of everything. Just import the project and have a look what you got.
    • It gives you the ability of syntax-highlighting (trivial, I know), but also the ability to format code like your are used to in a second. That helps reading it, even (or especially?) when you have some experience in the language.
    • It shows you the JavaDoc comments right away at every position in your code. So you will always have the documentation with you when needed.
    • It provides tools such as a debugger, a call-hierarchy view and many more. All this tools will help you to get an understanding of how the code works.

    And it can to much more for you. So it’s really worth to get familiar with your IDE.

  • UML is a widely used standard for modeling and visualizing code. The purpose of UML is simple and exactly what was asked: it should help you to understand code. The strength of UML is the abstraction. You can easily visualize a big architecture without mention any detail at all. On the other hand, you can add any detail you want if it is important for the understanding of the system.

    The basics of UML are not hard to learn, a quick look at Wikipedia should be enough to draw a class diagram. But UML is also enough standardized, that it could not only be read by humans, but also by machines – so you can generate it (so called round-tripping)!

    Tools like ObjectAid helps you to generate class diagrams as easy as possible. Just install it in your IDE, create a new class diagram and drag all Java classes in it.

  • Tests, especially unit tests (e.g. JUnit tests) could be also very useful to understand the code. A good test is something like a additional documentation or a contract on code-level. It exactly defines how a method can be used (e.g. input parameters) and what it should make (that’s what the test is testing ;)). So, if you don’t understand what a method should do and you have tests, take a look at them. And what if you don’t have any tests? Write your own ones! This is a good approach to explore an API in a structured and repeatable way. Whenever you are not sure what the API does, try to create a test about your assumption.

So, what to do if there is a bunch of new code?

  1. Import the project to your IDE.
  2. Get an overview of folders, files, packages and so on.
  3. Explore the code: What’s in the JavaDoc? What types are used? What about the call hierarchy?
  4. Run the unit tests and try to understand what the methods are doing.
  5. Try to run the debugger for a test or two.
  6. Draw or generate a UMl diagram.

I have uploaded some screenshot in the slideshow above for the open-source RSyntaxTextArea library from Fifesoft. But you will also find some good tutorials in the internet for every point mentioned before. Try to get familiar with at least some of the tools.

Best regards,
Thomas Uhrig