How to install OpenJDK 8

Updated post here: https://codeyarns.github.io/tech/2015-06-25-how-to-install-openjdk-8.html

Unsupported major.minor version error

Problem

I executed a Java program downloaded from the web and got this error:

Exception in thread "main" java.lang.UnsupportedClassVersionError: net/filebot/Main : Unsupported major.minor version 52.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)

Solution

This error from the Java virtual machine (VM) executing the Java class file indicates that the version 52.0 is not supported by it. This is usually means that the Java program requires a newer version of Java VM for its execution.

The mysterious part is the number 52, which is much higher than any Java version we know. This is a Java class file format version number. The Java version that maps to 52 can be found from the table of class file format version to Java version mapping. For 52.0, it is Java 8. So, this error means that I need to have Java 8 VM to execute this class file. That was correct since I had Java 7 on this computer.

How to compile and run Java applet code

The Java programming language can be used to write applets that run in a browser. Applet source code is similar to a typical Java program source code, except that it does not have a main method. Applet source code can be created in .java files.

Compiling them is the same as usual:

$ javac foo.java

This generates a foo.class compiled bytecode file.

However, running the generated .class file using the Java VM gives this error:

$ java foo
Error: Main method not found in class foo, please define the main method as:
   public static void main(String[] args)

Applet can only be executed in a browser by opening a HTML file that refers to this applet .class file. A minimal HTML file that does is:

<html>
<body>
<applet code=foo width=400 height=200>
</applet>
</body>
</html>

Save this as foo.html file in the same directory as the foo.class file. Open it in a browser that can run Java applets and you can see the result. 🙂

Tried with: Java 1.7, Firefox 34 and Ubuntu 14.04

Java applet error in Chrome

Problem

I opened a webpage with a Java applet in the Google Chrome browser. The message “This plug-in is not supported” was displayed in the canvas of the applet.

Solution

Chrome has changed the API it offers to plugins from the Netscape Plugin API (NPAPI) to Pepper Plugin API (PPAPI). I use the IcedTea Java plugin for Chrome. It seems that this plugin was written to use NPAPI, so it does not work in Chrome now.

Firefox seems to still support NPAPI, so I opened the Java applet page in it and it worked fine.

Tried with: Chrome 39.0.2171.95, Firefox 34 and Ubuntu 14.04

Python for the Busy Java Developer

20150118-pybook

I use Python once in a while to get some simple work done, like read, write, process or plot some data. I inevitably end up Googling or looking up analogues to C++ constructs on StackOverflow to get the job done. On one such recent stint, I found it useful to refer to the book Python for the Busy Java Developer as I worked on object-orientifying a piece of Python code. On tweeting this, my friend Deepak Sarda, who is also the author of that book offered me a copy of the book for review.

Python for the Busy Java Developer is a short book that enables folks familiar with Java to get started on reading and writing Python with ease. If you already have experience working with other object-oriented languages with a C-like syntax (like C# or C++), this might also be the right book for you. Following a short introduction, the meat of the book is essentially Chapter 2, which is pretty long and takes the reader on a trip through the syntax, lists, functions and classes. Chapter 3 lists the tools and libraries that any seasoned developer would be looking out for while writing code.

There is a big difference between Googling a problem and learning from an experienced person. It is just that kind of insight that I found at many places in this book. By just looking for analogues to C++, I had never realized of the alternate or extra possibilities for certain language constructs. As an example, it never occurred to me that attributes or methods of a class could be deleted outside the class. This is of course in the very nature of a dynamic language like Python, but the thought never occurs to a person coming from a statically typed language.

Though I’ve been writing short Python scripts for a few years, from this book I found myself learning many tiny details that are sure to help me write code that is more Pythonic. My familiarity is with C++, but I found the analogies to Java in the book quite straightforward to relate to. The concepts are introduced in a natural order and the book can be easily finished in a couple of hours. Places where Python might behave differently or in ways that is better than you except are pointed out. The book is beautifully typeset, which is a quality that seems to be sorely missing in tech books today. A book of this length has to leave out a lot and that is possibly the biggest downside of the book. I was left wanting to learn more of the Python analogues to the other C++ constructs I’m aware of.

I can easily recommend Python for the Busy Java Developer as a quick guide to learn Python if you are coming from C++. You can buy it here for the price of a cup of coffee. But wait! Deepak is offering the readers of this blog a 10% discount, just use this link to buy the book. 🙂

How to install Java to Eclipse CDT

People who work with C or C++ use Eclipse CDT. When they need to work with Java too, then adding this support to Eclipse CDT is easy:

  • Check if you have a Java Development Kit (JDK) installed. Else install one. I like to use the open source OpenJDK. I installed it using:
$ sudo apt install openjdk-7-jdk
  • Open Help -> Install New Software. This opens a dialog where you can choose what plugins to install.

  • In the Work With dropdown, choose your Eclipse version. For example, I chose Luna. The Software list below should get populated with all the software that can be installed to your Eclipse.

  • In this list choose Programming Languages -> Eclipse Java Development Tools. Choose Finish.

After installation, Eclipse will ask to be restarted. After the restart, you should be able to create and work with Java projects.

Tried with: Eclipse Luna 4.4.1 and Ubuntu 14.04

Eclipse: Silent Startup Error

Yesterday Eclipse started to show a strange problem. Invoking eclipse.exe would display the splash screen and would exit after that!

Since the program would not even display an error message, I first tried invoking it from the console to see any errors. It would exit without writing anything on the console. I then thought it might be a corruption of settings or workspace, but cleaning those did not help either. I then looked at the runtime options I could pass to eclipse.exe and tried those options related to a clean invocation and logging. Nothing helped.

I had almost given up when I noticed the eclipsec.exe in the same directory. This turned out to be a console executable of Eclipse. Invoking this finally showed the problem:

C:\Program Files\Eclipse>eclipsec.exe
Error occured during initialization of VM
java/lang/NoClassDefFoundError: java/lang/Object

So, there was a problem with the JVM! I downloaded and installed the latest JRE and Eclipse worked fine after that! 🙂

Ubuntu: Javadoc

The Javadoc tool is used to generate documentation from comments in Java source code. To generate documentation in Eclipse, choose ProjectGenerate Javadoc. You may be required to provide the path to the javadoc binary in the Javadoc command field. This is typically /usr/bin/javadoc.

On a typical installation of Eclipse in Ubuntu 9.10 (Karmic Koala), you may not find the javadoc binary. To get this binary, install the openjdk-6-jdk package.