This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use different JDK versions

 
Sudhir Srinivasan
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,

To install NetBeans IDE 6.9.1 requires JDK 6.0 update 13 or later, as my existing IDE 6.0.1 does not support Spring, hibernate etc.

I have JDK 1.5 already installed on my Windows XP (SP 2) machine. My question is:

Do I have to uninstall this before installing JDK 1.6.0_22? OR can I have both versions of the JDK installed in, say, different paths where

JDK 1.5 is in the existing C:\Program Files\Java\jdk1.5.0_06\ directory and
JDK 1.6 is installed in the D:\<myname>\Java\jdk1.6.0_22\ directory (new path)

and use the version of java from the respective directories?

If this is possible, should I just add the new path leading to the bin directory to the PATH environment variable (under System variables of My Computer > Properties > Advanced > Environment variables) for Windows to find the java executables of the JDK concerned.

My existing environment variables (in System variables) are:

CLASSPATH .;C:\Program Files\Java\jre6\lib\ext\QTJava.zip

JAVA_HOME C:\Program Files\Java\jdk1.5.0_06\

PATH C:\oracle\product\10.2.0\db_1\bin;%CommonProgramFiles%\Microsoft Shared\Windows Live;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program
Files\QuickTime\QTSystem\;C:\Program Files\Java\jdk1.5.0_06\bin

with the JDK 1.5 bin directory highlighted.

Would much appreciate a quick reply from the forum experts.

Thanks,
Sudhir



 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can install as many JDKs as you want. The default install directory is different for each version so you can install them in
C:\Program Files\Java\jdk1.5.0_06\ directory
and
C:\Program Files\Java\jdk1.6.0_22\ directory
or you can install them in other directories of your choosing.
If you want to compile and run programs from the command line then you will need to add the bin directory to your PATH environment variable but Windows will use the first javac/java executable it finds. Other than bu including the full path in the command line, you can't choose which javac/jave will be used.

However if you are using Netbeans, you can configure Netbeans with the available JDKs and then select whichever JDK you want to use for each individual project. Netbeans help should tell you how to do this.
 
Sudhir Srinivasan
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank you for your response Joanne. Your clarification
Joanne Neal wrote:
You can install as many JDKs as you want. The default install directory is different for each version so you can install them in
C:\Program Files\Java\jdk1.5.0_06\ directory
and
C:\Program Files\Java\jdk1.6.0_22\ directory
or you can install them in other directories of your choosing.

helped me in successfully installing the new JDK in addition to the existing JDK 1.5.

Also, as suggested
Joanne Neal wrote:
However if you are using Netbeans, you can configure Netbeans with the available JDKs and then select whichever JDK you want to use for each
individual project. Netbeans help should tell you how to do this.

I've configured Netbeans 6.9 to the JDKs installed using the Java platform manager as described in Netbeans help.

I also want to use the command line to compile and run programs.
Joanne Neal wrote:
If you want to compile and run programs from the command line then you will need to add the bin directory to your PATH environment variable but
Windows will use the first javac/java executable it finds. Other than bu including the full path in the command line, you can't choose which javac/jave
will be used

So, I added the path to the bin directory (of the newly installed JDK 1.6) to the PATH environment variable.

I prefixed the JDK 1.6 bin directory

C:\oracle\product\10.2.0\db_1\bin;%CommonProgramFiles%\Microsoft Shared\Windows
Live;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files\QuickTime\QTSystem\;C:\Program
Files\Java\jdk1.6.0_22\bin
;C:\Program Files\Java\jdk1.5.0_06\bin

to the existing JDK, as highlighted.

However, compiling and running the program from the command line

Step 1: change to the bin directory

Step 2: change to the source directory

Step 3: compile the program

Step 4: on compilation, change to the classes directory

Step 5: list the .class files in the directory

Step 6: run the program

throws the Exception in thread "main" java.lang.NoClassDefFoundError: TestSortStrings (wrong name: testMore/myPrograms/TestSortStrings).

A little bit of research on google showed http://docs.oracle.com/javase/tutorial/getStarted/problems/index.html this runtime error is due to java's inability to find the .class files. Initially, I tried running the program from the source directory which threw the same exception. Given that the source & class directories are separate, steps 4,5 and 6 done as per the suggestions in the link, without success.

While the solution seems to be centered on changing the CLASSPATH ref: http://docs.oracle.com/javase/tutorial/getStarted/problems/index.html, http://docs.oracle.com/javase/tutorial/java/package/managingfiles.html, I'm not sure of the same given the forum's http://www.coderanch.com/t/412282/java/java/we-change-tha-java-version suggestions.

My current settings in Environment variables > System variables is

CLASSPATH .;C:\Program Files\Java\jre6\lib\ext\QTJava.zip
JAVA_HOME C:\Program Files\Java\jdk1.5.0_06\
PATH C:\oracle\product\10.2.0\db_1\bin;%CommonProgramFiles%\Microsoft Shared\Windows Live;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files\QuickTime\QTSystem\;C:\Program Files\Java\jdk1.6.0_22\bin;C:\Program Files\Java\jdk1.5.0_06\bin

Please help in resolving this.

Many thanks,
Sudhir



 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the TestSortStrings class inside the package testMore\myPrograms? Then move to the src folder and execute the below command
java testMore\myPrograms\TestSortStrings

For example if a class named Test is inside the package com, then I use below commands to compile and run them.


code - Test.java

 
Sudhir Srinivasan
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi John,

Thank you for your reply. I was so focused on CLASSPATH and PATH environment variables that I forgot I was not allowing the compiler and the JVM to construct the path to the .class files [which it does by adding the package (& the classes thereof) to the classes directory], at compile and runtime.

This is aptly shown
John Jai wrote:Is the TestSortStrings class inside the package testMore\myPrograms? Then move to the src folder and execute the below command
java testMore\myPrograms\TestSortStrings

For example if a class named Test is inside the package com, then I use below commands to compile and run them.


code - Test.java



in your sample code and thereby helped me compile / run my program from the command line successfully!

Many Thanks,
Sudhir
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic