2013年3月30日土曜日

Exactly two months with Java

So, it has been exactly two months since I became a professional Java developer. (I have about 10 years experience in .NET and C#)

Actually, my work involves dealing with some .NET applications so I am not 100% pure Java developer, but still the team I am part of is nearly 100% Java developers.

I like to share my experiences so far.

Java language
Of course, two months is not long enough for me to see all of the major aspects of any language, but being similar to C# in terms of syntax as well as how it runs in a managed memory environment (virtual machine), my knowledge in areas such as .NET Reflection, IDisposable, generational garbage collector, design patterns, exception handling, all help.

Java syntax
I won't cover the syntax war such as where to put the curly braces. I just follow team's convention so it doesn't bother me, but it does take some getting used to.

Exceptions
This is annoying. Java is more strict about exception potentials. If a method has a potential to throw an exception, you either have to handle it or declare it as part of the method signature. I prefer C# on this where it does't require you to declare it in the signature. Seems this is too much work.

Extends and Implements
In Java, if you implements an interface, you have to say implement and if you extend a class, you have to say extends. Makes sense. However, I liked C#'s simple syntax of ':', but this is something you can get used to quickly.

LINQ
I miss it! Java has nothing like that.

Iterators
In Java, you use "for" with a special syntax that involves a semi-colon. In C#, you use "foreach". I like kind of like Java syntax too.

Events
I miss it in C#. There are no intrinsic notion called Event in Java that supports publisher-subscriber design pattern. As you know, in C# you can declare an event with EventHandler and have clients do += to subscribe to it. Very simple and neat. Java doesn't have that, and you pretty much have to pass a callback interface and have the event source object to "call" the handler. (I don't know if Java has delegate/functor.. does it? If so, you could pass delegate - still new on this)

Eclipse
OK, where do I start with Eclipse... I have so many things to say about this IDE.

Visual Studio vs Eclipse?
I would have to say that Visual Studio is better in the following aspects:

  • Visual Studio is so much faster to load. Period. Even Eclipse-classic, which is the bare-minimum feature set to do vanilla Java development can sometimes spend minutes to load.
  • Visual Studio provides nice feature packaging. In this regard, Visual Studio is like Apple product. You pick an edition from a few choice, and you live with what it comes with (well, there are extensions these days so not true but for most part you are all set once you install a Visual Studio). With Eclipse, you have to either work with EE package which is bloated with lots of feature that you might never use and deal with slowness during both start up time and during use. Trust me. EE was so horribly slow that I set up ProcessMon to see what it's doing, and it was reading ton of files for plugin stuff, taking like 10 minutes in response to a menu item. Its horrible.
  • Visual Studio is much more stable. Back in .NET days, I might have had three instances of Visual Studio running for several months. With Eclipse, I probably forcibly killed Eclipse process (which also creates java.exe and javaw.exe processes on top of eclipse.exe process) maybe like 50 times. It happens less after I built a lean-and-mean Eclipse created from bare-minimum Eclipse classic. Maybe Eclipse appeared hung when it was just working on something. But I couldn't tell and I lost many hours just waiting for it to complete its jobs. Its horrible.

Now, there are nice things about Eclipse too. I haven't come to appreciate it much but I see some potential, and its extensibility.  Feature set is not dictated by a single company. Its built by a community. Writing your own plugin (extension module) is easy - it even came with a tutorial right within Eclipse.

So, Eclipse is much more flexible, but that flexibility also implies complexity and lack of uniform testing of the products. You have to tame it.

Some surprises for Visual Studio users
You have .sln for Visual Studio. Eclipse doesn't have one. Instead, it uses a "folder" which is designated as "workspace". Its very file system oriented.

Namespace hierarchy and file system hierarchy must match. This is Java characteristics. I am so used to be able to put my .cs files at any folder organization, when you see com\company\product\foo\bat\hello.java (and usually deeper than this), you are like "oh crap, do I have to dig deep into this folder structure everytime I wanna open a .java file? (By the way, you don't. If you use Eclipse's Package Explorer, you can get to each .java class directly)

In Visual Studio, .csproj keeps track of the membership of source files. If you have a junk .cs file in your project but if its not part of .csproj then it doesn't matter. Its not part of the project. In Eclipse, it is part of your project. Its file based. If a file is in the folder that contains source, then it is subject to build.

Maven
Maven was very confusing to me at first, but after going through some tutorials it makes sense and it is a nice build system. I don't have much experience with Ant (but have done Nant and MSBuild) but many Java programmers seem to appreciate Mavan in terms of ease of use compared to Ant.

Its concept of repository is very powerful. Its very open-source oriented in the sense that you have a gigantic repository of stuff people build and you build your project by using those in the repository. Your project itself can be part of the repository. Its a wonderful open-source reuse mechanism.

Also dependency management becomes easier as you don't have to be aware of the hierarchical dependency tree. Maven will take care of downloading all needed for you.

I like it. I am not aware of .NET counterpart for this.

I tried to use Maven's POM file (its Maven't project file, if you will) with Eclipse using a special plugin just for that. It didn't work for me (but its due to the projects' complexity and my lack of understanding on my part). But once I gain experience with Maven Eclipse plugin, I would probably prefer to use Maven POM over Eclipse's native .project and .classpath file. (These are kind of similar to Visual Studio's .csproj but not quite


JBoss
This is just one of the web server. Its quite different from IIS. Like many open-source applications, you don't have a Setup.exe for this. You just un-zip a bunch of files to a location you like, and done.

It comes with script (both .sh and .bat for Linux and Windows). I use Cygwin and .sh files but you can easily use CMD to use .bat and do the same thing. To run a server (like IIS's "start"), you open up a console and run an appropriate batch file to start it.

Once you start it, then you would use brower to interact with an administrative page. This is very different from IIS where there is a Windows interface to work with various IIS configurations. You can use JBoss web admin interface to add "application" by pointing to a .WAR file (a zipped up web application content). You could also use file system based deployment of your web application by placing a set of web application files under a special folder called "deployments" and then place a so-called marker file in there. JBoss has a built-in scanner that watches this folder and takes a presence of a marker file as a command. Kinda weird and feels like 80ish way, but it works.

PostgreSQL
Our team happens to use this. I just thought this is nice database. I have been using Microsoft SQL Server but I can see myself using this instead of SQL server. It provides a nice UI to manage database. I haven't had chance to work with this extensively yet but so far it seems a very nice easy-to-use database.

Hibernate
This is a counter-part of .NET ADO.NET Entity Framework. I don't have extensive experience with this either but it seems to have nice infrastructure to define mappings between your class and database schema. I love Entity Framework, but it has some learning curve. I like to see how easy Hibernate is as I think there is a .NET package of hibernate.

OK, maybe that's it.

So far, I am enjoying working with Java and open-source server applications.

2013年3月15日金曜日

UML Modeling Tool for Eclipse

I want boxes and lines to describe the classes.

I could use:
  • Microsoft Power Point (I prefer not)
  • Microsoft Visio (I prefer not either. Its not even installed on my machine and am too lazy to find a copy licensed to the company I work for)
  • Google Presentation (My favorite choice for general diagramming, but its not a modeling tool)
Well, since I am in this wonderful open-source Java/Eclipse land, there got to be a nice free modeling tools out there.

There seems to be a lot of tools offered out there, and I am very much confused (e.g. there are UML2 SDK, some Graphical modeling tool, and Ecore, and some commercial versions?)

Anyway, I found one that's sufficient for my needs. The choice I made is Eclipse Modeling Framework (EMF). Actually, there is already a nice tutorial article about EMF.

This is what I did:

First, made a new copy of Eclipse specifically to have modeling related plugins (I wanted to avoid adding too much stuff for feature that may not be used all of the time)








Then hand-picked a couple of items (used the tutorial article I mentioned above).

Now, I can create UML-like class diagram.

I heard that you can generate Java source from the model. I haven't tried it yet but I am sure that would be useful. For now, having a UML-like tool is good enough for me.

TIP: I usually use the light-weight Eclipse class with few server-related plugins. That instance can't handle EMF files - it just shows the content of .ecorediag as plain XML file. That's fine. Only when I need to interact with it in graphical editor, I start "model" version of Eclipse.

2013年3月14日木曜日

Lean and mean Eclipse

I download Eclipse IDE for Java EE Developers JUNO (4.2).

It sucks. Its very slow and sluggish. It is unusable for me. Eventually I leaned that increasing the memory for JVM helps, but its still slow.

I noticed that Java EE package has a ton of plugins, some of which I care less.


If you compare packages, the Classic package seems to be a good baseline to start. So I decided to start all over with Eclipse Classic as the baseline, and gradually add feature to it.


The Classic package loads quickly and stays very responsive. The way it should be!

Now, I like to do these things with my Eclipse instance:

  • Edit XML files
  • Create web projects
  • Work on JSP and Servlet stuff
  • Work on HTML, CSS, and JavaScript
You can do these things if you install Web Tools Platform

At first, I thought there is a single package called Web Tools that contains all of these, but it seems that's not the case. So I manually picked half a dozen plugins that are part of the Web Tools Platform.

Here is how to add then.

First, choose "Install New Software..." menu.


Pick "juno" as a software site.


Expand "Web, XML, Java EE and OSGi Enterprise Development" node.
Then pick those items shown below.



Done. You see Web Tools Platform added..



Eventually, I will want to add Subversion, Maven, and JBoss related plugins, but I want to do that one at a time.

Happy coding in Eclipse now!

2013年3月9日土曜日

Eclipse IDE is slow

To speed up Eclipse IDE, you need to edit eclipse.ini, which is right where eclipse.exe is located. Go to the bottom of this article for the settings I have.


You would want to make sure that following lines are in it.


-Xms512m
-Xmx1024m
-XX:MaxPermSize=256m


Background

I have to use Eclipse IDE for the job I took 6 weeks ago.

Fine. So I go to the Eclipse web site.

It looked like there are a bunch of pre-configured packages for Eclipse, depending on what you indent to do with it. Since I am expected to do lots of server side stuff, Java EE seemed the right choice for me. So I downloaded it and installed it (which just involves un-zipping the entire contents to an arbitrary folder - a surprise to a .NET developer like me)

I never really used Eclipse before, so I wasn't ready, but the first impression I had was "holy cow, its so slow!" It was sluggish, non-responsive, and every time I did almost anything, it took like few seconds to sometime few minutes, not kidding! (Note, the machine I was given had 24 GB of memory with Xeon processor).

For example, when I switch my tab to view an XML file, it took like few seconds for it to show the content. When I did a right-mouse click on a Java editor, it look like a few seconds to show the menus. I probably killed the Eclipse (and its associated java process) several times using Windows Task Manager when I was staring at Eclipse splash screen for a couple of minutes. It was just not usable.

So, I Googled and found out that basically the default Eclipse IDE configuration is too conservative about its memory use, in terms of JVM (an equivalent of .NET run-time). 

There are lots of recommended configurations out there, but having these lines that mentioned earlier did the job for me.