Wednesday, October 28, 2009

Installing Java (for JAVA Developers)

I am sure that lot of you guys might have gone through the pain of installing JDK(java development kit) on your PC. Well i went through the pain too. I just wanted to share my knowledge to help you guys out there.

INSTALLING JAVA ON YOUR PC
For WINDOWS USERS:

  1. search for Java JDK in google
  2. visit the first link in your search.
  3. Select a "Java SE Development Kit" from all the links visible (Make sure that you select a JDK not a JRE and ill tell you the difference).
  4. Select the appropriate OS.
  5. Download the file.
  6. Double click the file and install.
  7. go to C:\Program Files\Java\ (or the path where you installed java).
  8. go to the jdk folder that comes up.
  9. go to the lib folder. copy the path of the folder in the Explorer address bar.
  10. Go to start. Right click on My Computer and go to properties (for Vista users, its properties of "Computer").
  11. go to "advanced"->"environment variables".
  12. go to system variables.
  13. double click open "CLASSPATH" variable in system variable. If "CLASSPATH" or "classpath" doesnt exist create one.
  14. Make sure that CLASS and PATH are a single word as in "CLASSPATH" not as in "CLASS PATH" or "class path".
  15. in variable value of "CLASSPATH" add a dot and a semi colon as in ".;"
  16. now paste the entire path after the ".;" .It should be similar to the following: ".;C:\Program Files\Java\jdk1.6.0_16\lib"
  17. now open "PATH" variable in system environment variables.
  18. go to the end of the variable value add a semicolon and then paste the text as like in previous steps but MAKE SURE THAT you change the "lib" to a "bin".
  19. the variable value of "PATH" should be like in "something;C:\Program Files\Java\jdk1.6.0_16\bin".
  20. click on OK and come out.
  21. open cmd from run in start menu.
  22. type in "javac" command to make sure that path is set right. You can also type "java" to check but, its a good idea to try "javac" as javac only comes with a JDK not a JRE which is by default installed in a system.
  23. write your program in a text editor like notepad or textpad. compile and execute the program to make sure that it works.
Now i will explain the above steps:
Steps 1 to 12 are all about installing java.
Steps 13 to 16 make sure that you give the "CLASSPATH" right.
Steps 17 to 19 make sure that you give the "PATH" right.


So, whenever you create a executable and you want others to access it via command prompt from any directory. You add the executable file path to the "PATH" variable.



For LINUX Users: There are lots of Linux distros available and there are many ways to install it. So, i will tell you two easy ways to install Java on a popular Linux Distro UBUNTU.

First Method: This is more easier than in WINDOWS :p as there is no need of configuring CLASSPATH or PATH. Just follow the commands and start developing in java.
  1. open the terminal.
  2. type the following command.
  3. sudo apt-get install sun-java6-jre sun-java6-jdk sun-java6-plugin
  4. the above command will install java runtime environment (JRE), JDK, java6 plugin (for web browser i suppose).
  5. Restart the terminal to check if jdk is installed by typing in following commands: "javac" and "java"
Second Method:
  1. Visit http://java.sun.com/javase/downloads/index.jsp
  2. Select Java Development Kit and select Linux (32 bit/ 64 bit).
  3. Download the bin file.
  4. open the terminal browse to the folder where the bin file exists.
  5. type in the following command $chmod a+x downloadedbinfile
  6. In above command chmod a+x would give all (user group and others) execute permission.
  7. then type the following command in terminal in the folder where the file exists "./downloadedbinfile".
  8. Then follow the instructions in the terminal.
  9. After its done. Open bash.rc file in /home/yourusername directory in root mode and add the following:
    export PATH=$PATH:/path to jdk/bin
    export CLASSPATH=:.:/path to jdk/lib
  10. restart the terminal and type in javac and java to check if its working.

Now answers to questions that may arise in your mind after following above process (if you are a noob java programmer)

  • WHAT IS CLASSPATH?
CLASSPATH contains all the precompiled class files that you need to run your program. For example in the following program:

import java.util.Random;
class RandomGenerator extends Random {
public int nextInt(int L_limit,int U_limit)
{
return nextInt ((U_limit - L_limit+1))+L_limit;
// the above line always generates numbers b/w U_limit and L_limit.
}
}
public class RandomDemo
{ public static void main(String[] arguments_stored_here)
{
RandomGen x = new RandomGen();
System.out.println(x.nextInt(1,5));
// change 1,5 to number u like to generate random numbers between them
// 1 and 5 are inclusive here.
}
}


java.util.Random refers to Random class in your system. You can view by going into the CLASSPATH folder and opening the tools.jar file with winrar. you can find a file called Random.class under java->util folder. Similarly you can also find System->out (in for System.out.println)

So, basically CLASSPATH is the repository of all class we refer in import statement in our java progam. If we do not set the path, the compiler does not know where to look for the classes in order to compile the program. The "." in class path refers to current folder.

  • WHAT IS PATH?
PATH makes sure that your commands like "javac" or "java", etc work from any directory in your system. If you observe there are files by the name "javac" and "java" in bin folder of the jdk directory. For example a simple command like "ipconfig" in command gives you the ip configuration of your PC, even ipconfig is a file under system32 folder .

  • WHAT IS JRE?
JRE stands for Java Run Time Environment. It is the software required to execute java programs or programs that require java to run. You write the code in java and give the user the .class files. The JRE executes the .class files. It contains the JVM (Java Virtual Machine).
  • WHAT IS JDK?
JDK stands for Java Development Kit. It is the software required required to develop java programs or java related applications. It also includes a JRE (for the developer to test the developed applications).

Saturday, October 24, 2009

Generate random numbers between two numbers: JAVA

The other day i came accross an interesting task where i had to generate random numbers between two numbers.

Unfortunately, there is no built-in methods in Java to generate random numbers between two numbers.

I searched many forums to get my answer. I am writing this in my blog as i found it interesting.



References: http://bytes.com/topic/java/answers/587355-random-numbers-between-number-range-java

Tuesday, October 13, 2009

Microsoft Windows is not bad as we think: filename length restriction in Windows XP

The other day i was busy backing up important data in windows xp manually.....

I was using a x86 machine running a Windows XP SP3....... i was copying data saved on a different hard disk to my new machine (probably a windows xp but older version).

I noticed a flaw while trying to modify data in a folder. I could not copy a file from a hard disk. The length of the path of the folder (address bar in the explorer window) was very lengthy and windows would not allow me to copy it saying the length of the file is too long.

I then checked out the filename length(it was 254, may it has restrictions on file length of 255 or 256)......

The thing to notice here is at first how did the previous OS agree to copy the file with that filename length?? Now, while copying it back in the new OS reveals that Microsoft has fixed that problem..... That is why it kept giving me error message asking me to limit the file length size.

i thought writing this in my blog would be good reminding me that this bug existed before in previous windows of XP.....