Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Saturday, June 11, 2011

Setting up Java in Linux

Setting up Java in Linux can be a real pain when you are an Ubuntu noob. It took me quite some time (nearly a day to be precise) to get it working. After a lot of purges, fresh installs and reboots, its successfully installed. I thought of sharing with you the goldmine "Java installation in Linux made easy" :P

I had initially installed Java from the official site by Oracle but it always gave me a lot of errors whenever I opened Eclipse, saying "Java 6 was not installed". I am sure there were many ways to work around it with a lot of commands etc. But, c'mon let's face it, we are Ubuntu noobs. So, I had to purge the whole installation and cleaned the entire system of Java before starting all over again. This time I used the method mentioned below and it worked.

Open the terminal and install sun java with the command

sudo apt-get install sun-java6-jdk sun-java6-jre

This should the whole sun java installation process. To be on the safer side, after this installation, open Synaptic Package Manager (System>Administrator>Synaptic Package Manager) and search for sun-java6-bin, sun-java6-jre, sun-java6-jdk and sun-java6-plugin. If they are already marked as installed, well and good, otherwise install them. They say the jdk does include the jre and you don't have to install both of them. But, I installed both.

JDK and JRE will mostly be installed at /usr/lib/jvm/java-6-sun-1.6.0.24. Ubuntu will create a symbolic link to java and javac at /usr/bin/java and /usr/bin/javac for easier access.

Next, we have to create a symbolic link to show where Java is installed in the system. This can be done with the following commands.

sudo ln -s /usr/lib/jvm/java-6-sun-1.6.0.24/bin/java /usr/bin/java
sudo ln -s /usr/lib/jvm/java-6-sun-1.6.0.24/bin/javac /usr/bin/javac

Lastly, you have to set the JAVA_HOME variable. This can be done by opening the .bashrc file in the home directory as superuser and editing by including this line at the end.


export JAVA_HOME="/usr/lib/jvm/java-6-sun-1.6.0.24"

Finally, you can check if Java is installed by typing the following command in the terminal


java -version

If it returns the version number of Java installed in your system, then Bingo! You have got Java successfully installed in your system.

Now, you can install your JDK SE or EE bundles from the Oracle site.

Wednesday, June 8, 2011

Opening existing Java projects in Eclipse IDE

I am currently working on developing a GUI  using Java Swing components and am using the Eclipse IDE (the current version is Helios) for it. I have used Netbeans as well but Eclipse seems to have a lot more plugins which adds to its functionality. For this project, I am using the Visual Editor plugin which can be downloaded from here. It has a palette view where you can drag and drop Swing components and add events to them with ease. A nice tutorial from IBM which should help you get started with the Visual Editor can be found here. I still however recommend learning a little about Swing components.

Now, I found a flaw in the Eclipse IDE. Ok, it may not be a flaw, maybe I just didn't know how to go about it. Opening existing Java projects in the Eclipse IDE was just not possible. You could only open Java files and when you run them, they would ask for an ant build and so on. I finally found a way around it. First of all, make sure your project folder is in the eclipse workspace folder. For me, the workspace folder was home/eclipse_workspace. Then, open the eclipse IDE and go to File>Import. In that, General>Existing Projects into Workspace. Then you browse for your project directory in the workspace folder and there you go. Its imported and you can Build and Run!

I don't know why the Eclipse developers would not make something as easy as File>Open Project. There might be another way around this. Do let me know through comments.

Tip: I came across this recently. For any Java file being developed using Eclipse, you can add packages required by the file with the shortcut Ctrl+Shift+O. All the required packages are automatically added.