Calling SH Unix command via Java


By far, one of the most simple, yet overlooked simple solution to batch and process executions.

Running an SH via Java isn’t really hard – there’s already an existing Object Utility for it, just need to call it correctly though. Java Code:

java_code_call_sh

the samplescript.sh is a custom script I created using nano. Basically, I pass alvin jay and reyes as parameters and get them by using $1,2 and 3.

nano_script

Console Output: I manage to get the output from the output stream object of the Process.

dir_sh

Download the source code here:

JavaFX Tutorial – Basics


JavaFX seem to be gaining ground on the RIA space. With the right tools and development support, it wil definitely have its toll on the next best technology “thing!”. I’m not writing any JavaFX review here since there is a lot of Technology critiques out there that probably reviewed it extensively, but instead, I will write a simple tutorial on how can you develop JavaFX application in your MacOSX lion. :)

First some pre-requisites:

  • JavaFX Runtime Environment
  • Java Runtime Environment
  • JavaFX SDK
  • JavaFX Scene Builder
  • JavaFX IDE – I had chosen NetBeans 7 as it already has support for JavaFX.

This can all be downloaded on the Oracle Website. You may google it. :)

Requirement: Create a simple application that accepts (you guess it) person details (simple registration), a custom web browser and some analytics.

Technology: JavaFX and JPA :)

Step 1: Create the Database and Tables

Simple Database and Table, download the SQL file: here

Step 2: Create the User Interface and specify the controller

Using the JavaFX Scene Builder, create the user interface.

Step 3: Development

Code the App!

NetBeans has its support for JPA – so I used it to interact with the database.

Download the source:  here

Its basically a very simple application, but I think this sample will give you a brief introduction or a head start on how to actually develop an application using the platform.

Not a bad alternative if you want to create Desktop Applications, which of course, .net offers much better solution. Though the main take away here is that JavaFX redefines Java Desktop Application Development – flexible enough to support the best of both worlds (Desktop and Web) and if thats not enough, it also supports mobile phones.

SVN File List – Listing SVN Files in Java


Requirement 1: Given an SVN Repository URL with Username and Password, list all Files inside the directory.

Initialize SVN First by declaring a repostiroy object:

The above method will initialize the repository and call the list of files located on the given path “deploy”. This means all files at: svn://localhost/branches/project/deploy

The above code will re-curse to get all the list of files. We need to do a recursion to exclude the Directories and consider only files. :)

Sample code: here

Download the SVNKIT (Java API for SVN): here
Enjoy!

Java 7 – Files API


Java 7 Introduce a new I/O package to handle File Access form the Java Virtual Machine more  ”simple”. Well thats how I actually look at it after using it in one of my sideline projects.

Although upon exploring the net on potential cause on why a new File I/O Api was developed, I found this to be the most straight forward reason. For proof that the prior Java 7 File IO api has it flaws, I listed what is indicated on the above site and give my inputs to each.

  • Many methods didn’t throw exceptions when they failed, so it was impossible to obtain a useful error message. For example, if a file deletion failed, the program would receive a “delete fail” but wouldn’t know if it was because the file didn’t exist, the user didn’t have permissions, or there was some other problem.
    • Well this is quiet a start, no exception handling at all for some methods. For read, create and write process are fine since developers can handle them accordingly in their code, but the delete method (as indicated above), has no excuse at all. There should at least have a decent exception message that should be thrown. Yet is this enough for a re-architecture?
  • The rename method didn’t work consistently across platforms.
    • This is interesting, a Java known to be cross platform has a method that doesn’t work in all platform. If you’re developing a Web-based Java Driven Application that is expected to run on different platforms, hold your horses first. 
  • There was no real support for symbolic links.
    • I would say the symbolic links (or soft link to many) is tolerable, since this can be created. Although for me, it is more of a good addition to the File IO than a lacking on the prior.
  • More support for metadata was desired, such as file permissions, file owner, and other security attributes.
    • Not much support for metadata access. With the introduction of cloud storages, File metadata access are more important now. Instead of storing files on database and add meta-data information to it. It would be more efficient if all of its information can be saved in its own metadata.
  • Accessing file metadata was inefficient.
  • Many of the File methods didn’t scale. Requesting a large directory listing over a server could result in a hang. Large directories could also cause memory resource problems, resulting in a denial of service.
    • Imagine using this in a enterprise application, that renders an xml file and transform it (using xslt) and display it in your screen. If this is the case for the prior File I/O, then I guess you need to revisit your design and put an improvement plan for future (possibly monolithic addition of data) implementations.
  • It was not possible to write reliable code that could recursively walk a file tree and respond appropriately if there were circular symbolic links.
    • We lack symbolics then we cannot guarantee the stability of recursion with it.

I guess, the new Java 7 IO solved most of this issue. Yet still, I haven’t tried putting all of their answers to the issue above to the test. Although I did experience a bit of it in my sideline project. :)

My example above is just a basic file manipulation example. You can see that its more simpler to apply file management capabilities to your code using the new API.

Interface Summary:

My cents: I think it is worth checking out, although for implementations out there that uses the old File IO, its unfortunate to inform you that this is re-architecture of the File I/O, you would have to re-code your application to use it. Although it uses the same File IO objects (File, Paths etc.) so I guess, it wouldn’t be too hard to switch. :)

Twitter API on your Java Application


Ever wonder of attaching your tweets to your Java Applications? I searching for the best API out there for this and lucky for me, I found it!

http://twitter4j.org/
A Simple How To:

The first thing we need to do is to create an application within your twitter account, grant it access and generate authentication tokens (although, the consumer tokens will also work and it will be automatically generated upon creating the application).

1) Create an Application in your twitter account: Go To > Settings > Applications > Developers > “here”. You must login with your twitter account (or the account that you will use in your API).

2) Create your application: Enter required fields.

3) A consumer keys will be given to you. if you’re only looking to read status updates, this is sufficient enough for your needs.

The Code:

For this example, I’m using Eclipse IDE.

1) Create a New Java Project and include the Twitter4j Library (download it here).

2) Create a class and check the code below:

Console Output:

And the Actual Twitter Feed:

Download the code :p

Enjoy!