Rss

Archives for : java

IntelliJ, Eclipse and Dropbox

For Java development, I use the best* Java IDE available, IntelliJ. My friend who I did some java programming with the other week uses Eclipse. Working together on the same project^, we thought we’d try using Dropbox to sync our work, since it was only a three day project with 2 people, so setting up a fully featured source repository wasn’t needed, and may have actually been harder to use than Dropbox for the same project.

Anyway, I thought I’d report back on how it went. It went really well, considering I was expecting it to break in the first 10 minutes. If having 2 IDE’s wasn’t enough to potentially cause problems, conflicting files was sure to halt us after 20 minutes. As it turns out, we didn’t really have any conflicting files that caused problems, though as I mentioned, it was only a small 3 day project, and we were both in the same room. 2 IDE’s? Well, naturally, Eclipse doesn’t support IntelliJ, so it was up to IntelliJ to support Eclipse, which it did better than expected. It did crash once when the .classfile was changed by my friend and Dropbox automatically synced it, but considering it was doing all the inter-IDE support and Eclipse was just using it’s own stuff, and we were using Dropbox for automatic syncing, I’ll let that small issue slip.

A few things to be aware of if you are going to try something like this out:

  • It only really worked because it was a small project and we were in the same room. We were able to talk to each other to pretty much avoid any file conflicts. Becasue of this, I think it was better than using a source control system like SVN. That also said, start adding more people, or making it a bigger project, and all of a sudden source control is starting to look really good.
  • Eclipse will need some setting’s changed to automatically refresh files when it detects changes. I think Eclipse has some form of caching system, so if a file gets changed on another computer, it will have an “out of sync” message. I don’t know how to do this, I don’t use Eclipse, and IntelliJ doesn’t have a problem with it.

Happy coding.

* Subjective, I know.
^ Annoyingly IntelliJ and Eclipse use the word project to mean different things. We were working on the same task if that makes it better.

The joys of developing software with no API

I’ve finally pushed out my first Android app, one that I’ve put a lot of time into (and still am, got to improve the UI experience and fix up a few bugs!) and one thing I realised in the development process is, it isn’t fun trying to develop for something that has no external API, or a company that even wants this to happen.

In the development process, I have been blocked from logging into Virgin Mobile’s website, where I just kept getting 404 Not Found or 405 Not Allowed errors (fun times.) That said, it was linked to my IP address, not my account, because it worked when I tried it at uni. And right now, I can no longer pay any bill by credit card, as I made a few mistakes in a yet unreleased feature (though I’m sure you can guess,) and whilst those mistakes have been fixed in the program (I think), I cannot make payments to my from my IP address (changed IP address, same problem), account (tried a different account, same problem) credit card, making it very hard to test.

Not to mention the lack of ability to simulate events that either don’t happen that often (turn of the month, to ensure that the app handles data and months properly,) or that you don’t ever want to happen (overdue bill.) I can also only test it on my own account, which severely limits the testing scope. I do have a friend from Uni who I get to do beta testing, but without many different accounts at your disposal and connected to your debugger, it does make finding and fixing bugs quite difficult. For example, I have had 2 bugs so far that I cannot simulate, I can’t work out why the data isn’t been fetched properly without an account that it happens on. One of them I’ve put in if and try…catch statements that prevents the error from happening, but it comes at a cost (the data usage for that user will not be displayed. That said, maybe Virgin Mobile still have postpaid accounts without data? If so, the bug should be fixed.) What I really need is users to bombard me with emails, screenshots of their account and the HTML source code of some of their account pages (I don’t need username and passwords, just the user to be willing to test it after I’ve made a few changes.)

It’s also annoying when you can only work with the information you’re given, but that information isn’t available. For example, I want to have a progress bar throughout the month of how much credit the person has used, but I can’t find anywhere on the accounts page a value that states how much credit the user has in their cap (it does for data, but not for cap credit.) I’ve tried to overcome this by detecting the type of cap (that information is available,) and storing the credit allowance and the amount for that cap in the code of the program, and matching it up. This isn’t the best way to do it (though it’s the only way I can,) as I have to manually keep it updated, and what if it’s wrong? To overcome it been wrong I’ve set it to alert the user when they login what the cap is detected as, and that they can change it if it’s wrong, but it’s better when the user has to input as few things as possible. I’m just getting away with username and password, I’m hoping when I implement a prepaid account it can automatically be detected (which reminds me, I need to run out tomorrow and get a prepaid SIM, I need an account to test it with!)

I haven’t yet heard from Virgin about unusual activity on my account, and I hope to keep it that way, but there’s only so much you can develop without actually testing it on your own account a few times, and things inevitably go wrong. It would be nice if Virgin Mobile released an official app, or an API, as it is what users want.

IntelliJ IDEA Java IDE: Second Impressions

Edit: Just bought it. It is that good after a few days of use.

I have already given my unfavourable first impressions of IntelliJ IDEA, but I was forced to continue editing the post with an apology (the errors that were created were my fault) and another edit saying that I actually found it to be really good.

I decided I’ll give it another go (on my desktop, as the trial on my laptop has pretty much expired… or will expire in a few days, and my desktop has a lot more screen real estate, so is a much better developing environment.) Any all I can say, is if you look past the interface (it doesn’t blend into Windows too well, but you sort of get used to it,) all I can say is wow. Eclipse, I’m leaving you.

It’s true, IntelliJ does have everything. It is absolutely amazing, especially the static analysis tools that it offers. What are some of the absolutely amazing features I’ve noticed? Well, for one, the static analysis tool. Not only does it detect potential problems in your code, it can go ahead and fix them, without any problems at all! And how good are these static analysis tools? Well, I have a for loop. I could have used a for each loop, but didn’t because I was too lazy. IntelliJ suggested I use a for each loop, and actually went ahead (with my approval of course) and changed it to a for each loop, without breaking any code! Or maybe the small, but nice thing, when I was developing a reg-ex in another program, it automatically put a ‘\’ in front of every quote that needed it. I could quite literary paste the string and not have to change it at all.

I have barely scratched the surface and I’m already loving it. I don’t think I could use Eclipse again, unless it all of a sudden got really better or I don’t have a choice (e.g. in a work environment.)

So, this isn’t a review, but just a correction to say my first impressions weren’t correct. I’m definitely going to buy it when I get the funds together.

IntelliJ IDEA Java IDE: First Impressions

Edit: See the first comment in the comments section. These were first impressions, but as it turns out, they weren’t unique to IntelliJ, so they’re not really fair first impressions. When I’ve re-installed Windows, I’ll give IntelliJ another shot disregarding my first impressions.

Edit 2: I tried IntelliJ again on my desktop, and whilst it does take a little while to get used to the interface, it is actually quite good. I used it to finish off my first Android app, Virgin Mobile Usage Australia (it’s probably what gave me the motivation to finish it off.) I also setup a SVN server and I can say it integrates very well. I think I will buy a license after all.

Edit 3: I’m sick of editing this post with updates, so I wrote a new post (though it’s already got an edit… I love editing posts!). Check out my second impressions.

With a new computer, one of the first things I always do is go ahead and download all the programs that I’d generally need. As I do some Java development, I would normally download Eclipse, though whilst looking around, I stumbled upon IntelliJ IDEA, which some people have seemed to indicate is the absolute best Java IDE, better than Eclipse by a long way.

Now, I’ll start by saying I’m not a fanboy of any Java IDE… I used Eclipse because it was what I knew, and it worked. I am definitely open to changing if it will make my development more efficient.

So, when I finally downloaded it on my throttled connection, I launched it and tried to import a project I had already made. I had just plain java source files in Dropbox, rather than a whole project. Importing it was harder than I expected. Copying it into the source directory is a nice addition that Eclipse doesn’t support, but when I opened IntelliJ, the source files hadn’t appeared. I finally worked out I needed to make the package that these source files were all apart of in order to successfully add them. It would have been nice if it automatically made them, especially since it doesn’t have an import option (which isn’t a problem, if drag and dropping them doesn’t work.)

So, I got that project up in IntelliJ. It took me a little while to get it compiled, as whilst I appreciate that you have heaps more options for compiling, it doesn’t help when you actually don’t know how to use them. So I did work it out, and finally got it compiled. Well, all I can say is, that in IntelliJ, I get an exception that I don’t get in Eclipse. I don’t know what it is, nor have I found a fix for it. I’m still looking.

So, unable to successfully use my current project, I thought I’d make a test project from scratch. It was simply called “TestProject,” I made a package called “main,” and within it made a class called “test.java” with a main() function. When trying to run it, I have to chose a configuration. When choosing a configuration though, I need to chose a “main” class. I selected my test class, but a message comes up saying that “test is not acceptable,” which means I sort of can’t run my application.

So, it would seem, first impressions of IntelliJ are poor. Of course, they could just be me, but I have read through the help documentation to try and understand specific ways I need to do things, and also looked online for understanding IntelliJ for Eclipse users. Whilst I’m still going to try to keep using IntelliJ, as I definitely want to be able to improve my coding efficiency dramatically, if I can’t get it working, I’m not going to go out and buy it then.

… but that doesn’t mean it’s a bad choice to use Java

This post is in response to It *never* made sense to learn Java.

When I was reading the blog post about how learning Java generally isn’t a good idea, I tried to think back to when I actually did learn Java. What did I come out with? I don’t think I really ever did learn Java. When I was 14 I think I may have tried to read though a Java book, but I didn’t really learn anything from it. What have I actually learnt? Probably, C++ the most, as it’s what my university primarily teaches (and for Computer Science students, I think it’s one of the best choices, provided C get’s mixed in there as well, which for me it does.) That said, I still program in Java, and here I’m going to explain why.

The main thing that really caused me program in Java is Android development. There really isn’t any good alternative for the majority of app’s (You can use C++, but you loose all the API’s, and for most app’s it’s not required, then there’s MonoDroid, but that’s still in beta, and C# and Java are quite similar.) So I never really learnt per say, I just hacked together small code snippets that I found online using the concepts that I learnt at school, university and personal study. So I did learn it, just after I started to use it, as I already know all the OO concepts from learning C++. Secondly, I already knew C#, and as I mentioned before, the syntax is very similar.

Better Library Support
So, why did I choose Java for my most recent, non-Android software project? The biggest factor was library support. In genera, I find that Java libraries are less fragmented and better supported than C++ libraries that I found. For the project I was working on, I needed a Microsoft Office API and a email API. For Java, there’s Apache POI and JavaMail. The first one is supported by Apache, meaning that it has quite a lot of support, the second one’s developed by Sun/Oracle, so it also has a lot of support. I quickly did a search for C++ libraries, and was disappointed with the results for Office APIs (Visual C++ doesn’t count, sorry,) and the email library looked decent, but wasn’t backed by a big company.

Better Cross-Platform Reliability
C++ can be compiled cross-platform in most cases, but it’s definitely not as easy to deploy cross-platform programs with C++ than it is with Java. Java, unless using specific OS APIs, will work cross platform. C++ should work cross platform, but I’d put more trust in Java to work straight out cross-platform than I would with C++.

Backing
As already mentioned when it comes to library support, C++ to me does seem a little fragmented. Java seems more centralized and organised, as well as having larger backing (Oracle, which is probably why it (and C# backed by Microsoft) appeals to enterprise users a lot. Knowing that there is a big central company backing a language is quite important for many application developers.

Also, along side backing is jobs: a quick search in Australia on Seek pulls up 4350 jobs for the keyword “Java,” and only 651 for “C++.”

Speed isn’t everything
Computers these day’s are fast. Java is slow, that I am not disputing. But computer’s are fast, definitely fast enough such that there wouldn’t be any noticeable difference to the average user that of a Java application and a C++ application. That means that it’s more or less up to the points above if speed isn’t an important factor. That of course isn’t to say that speed is never an important factor, it most definitely is for certain types of programs, in which case I would compromise and choose C++ over Java.

Where Java doesn’t cut it
The original blog post linked to above mentions systems programming, and how Java most definitely isn’t suited to systems programming. The first thing I thought was, who would have ever thought that it was? I would never use Java for systems programming, and I’d even have reservations for using C++ (C is the best language for systems programming.) Java isn’t designed to be used for systems programming, so I don’t even see how that would ever be an issue. Just because a language can’t be used for systems programming doesn’t mean it shouldn’t be used for application programming.

Summary
I’d definitely suggest for people learning Computer Science to learn C++: from the concepts you learn in C++, you’ll be able to pickup on Java with minimal effort, whilst having a greater understanding of lower level concepts such as pointers, and knowing how to program well (I suspect C++ isn’t as tolerant as Java, so you’re more likely to learn to program “better” in C++, but this will flow over to other languages). Learning C++ as a first language doesn’t mean you shouldn’t ever use Java. Java and C++ both have their place, there’s a reason we have different languages, as different languages suite different needs (though we probably do have too many.)

Android: Using startActivityForResult()

Sometimes in Android, we want to be able to start an activity, but then return back to the activity that called it, and send a message (in the form of an integer in this case) along with it. This is what startActivityForResult() allows us to do.

Before I explain how to use it, let me explain the situation that I was trying to achieve. I’ll start with the diagram below.
Now, the idea behind this is: an Activity Main starts as soon as the program starts. It doesn’t have any layout associated with it. Then, based on a few conditions, it launches one of two Activities with layouts (in this case “Login” or “Details” Activity, but the names are completely irrelevant.) Of course, you want to be able to get to from one screen to the other (e.g. you login, or logout.)

Of course, I could have started with the login screen straight away, and if a condition states that I’m already logged in, launch the details screen straight away without doing anything, but personally I think this way is more versatile. At the moment the other way would work, but this I think gives more control over how your application behaves (though that could indeed be a bad thing.)
So, what did I have in my Main.java? Let’s have a look:
private void launchUserInterface()
{
SharedPreferences settings = getSharedPreferences(PREFERENCE_FILENAME, MODE_PRIVATE);
Intent i;
if (settings.getBoolean(“LOGGEDIN”, false) == false)
       i = new Intent(this,Login.class);      
else
       i = new Intent(this, Display.class);      
startActivityForResult(i, 0);
}
If you’re not sure what’s going on with the preferences, Google around for a tutorial on preferences. Basically, PREFERENCE_FILENAME is a constant I have defined, and MODE_PRIVATE is a constant that was defined for use with SharedPreferences.
This is pretty straight forward: if the user is logged in, it will proceed to launch Display.class, if not, it will display Login.class to prompt them to login. The above class is called in the onCreate function of Main.
Now, you are hopefully familiar with the way Intents work (if not, you probably should be reading this tutorial,) the interesting thing comes with startActivityForResult(). It’s not really that much different to the standard startActivity(), but it allows for the activity thats being launched to set an integer result. So, first you pass your intent, and an integer that identifies the call (not covered in this tutorial, so set to 0).
In your activity (either Login or Display,) you will at some stage want to go back to the main activity. To do this, just do the following:
public void onClick(View arg0) {
setResult(1);
finish();
}
Or something to that effect. You can set your integer result, and then call finish(). finish(), if called within an activity, should automatically kill that activity and return back to the previous one.
In your Main.class, you want something like this to catch the result:
@Override
    protected void onActivityResult(int pRequestCode, int resultCode, Intent data)
{
if (resultCode == -1)
finish();
else
launchUserInterface();     
    }
So, as soon as an activity that was called from Main.class with startActivityForResult() is finished(), this function will be called and you can do whatever you please. For example, every time that an activity finishes, I called launchUserInterface() again, which will re-launch the user interface based on whether you’re logged in or not. The idea being, when someone logs in, the activity stores it in the preferences and closes the activity. Main then runs launchUserInterface() which will launch the Display activity instead, since you’re now logged in.
Now, there is a downside to using this method: the more control over it you have, the more you need to take into consideration. The problem here is, when you press the “Back” button, you’re finishing the Activity. So, if you’re in the Login or Display screen and press the back button, you’re pretty much doing the same thing as calling finish(). This is bad, because when it get’s back to main, it will run the onActivityResult() and re-launch the activity. So pressing back will close and re-launch the activity.
In the above code, you will notice that there’s a condition that if resultCode == -1, then it will finish() the current activity rather than launch the user interface again. This is the one place I used the resultCode.
In both Login and Display, I did something like the following:
@Override
public void onBackPressed()
{
// Return to main and close activity.
setResult(-1);
finish();
return ;
}
This takes control of what happens when the back button is pressed. If you override onBackPressed() and didn’t put anything in it, then pressing the back button when that activity is present will do nothing. So, what I have done here is fairly straight forward: setResult to -1, so when it gets back to onActivityResult(), it will then proceed to finish() the main activity, instead of re-launching the interface. This is the expected result in this scenario, as it will take you back to the home screen (in most cases.)
So, there’s a quick introduction to using onActivityForResult(). There is so much more that can be done with it, for example, using the contact picker. Read more in the Android Developer site to further your understanding of onActivityForResult().

Update: Using this method can create problems later on. I came across it, but did manage to fix it. Check out this Stack Overflow question (and the accepted solution) for what you should do in addition to this.

Love-Hate Java

During my experiences programming (which, at 19, isn’t really that much, but still 5 or so years) I’ve come across Java a few times, but always passed on using it seriously. The first time I seriously tried it, the book I was learning from taught the Swing interface (read: ugly,) it’s compiled and interpreted (read: slow, but can have some perks such as been cross-platform.)

So the other day I thought I’d start to get into Android development (for real this time.) I had a few false starts, partly just getting really annoyed at the interface designer (I’ve since got over that,) and am on my way to releasing my first (not hugely complicated, but quite useful) Android application. Of course, this meant re-learning Java to be able to write the app (I could have done some hackish implementation with a cross between Java and C, but since my app isn’t memory or processor hungry, I didn’t really need the extra speed. Generally you would reserve doing that for games.) As someone who likes to program all my Windows applications with a GUI in C#, I was aware Java and C# are quite similar in their syntax, and after a little while I did start to notice big similarities.
One plus that I have found whilst programming for Android is that threading is very elegant. A lot easier than I remember threading to be, and is definitely important for a program that fetches data from the net (which can take upwards of 10 seconds, which is too long to lock up the UI thread for.) I don’t know if to commend Google or Sun (erm, Oracle.)
So, prejudice aside, I did start to get used to programming Java quite quickly, and to my surprise, was able to start making a working application fairly easily. I still have a heap to learn, but I’m learning as I go, and I really look forward to starting to make a lot more (and complex) Android applications in the future.