How to switch Java versions in Windows (updated 03/2021 for Java 16)
As a Java programmer, I work on various projects based on different Java versions, especially since the transition to the six-month release cycle. From time to time, I also have a piece of Java code that I want to try out on different Java versions – on the command line without having to click through the menus of my IDE. So it is helpful to be able to quickly and easily change the Java version to be used for compiling code or executing class files.
In this article, I’ll show you how to install several Java versions simultaneously in Windows and how to switch between them on the command line with short commands.
Installation of multiple Java versions
Installing several Java versions at the same time is incredibly easy in Windows. You can download and run the installer for each version, which automatically installs the versions in separate directories.
Download sources
- Java SE 1.1 – You can no longer install this version on 64-bit Windows.
- Java SE 1.2 – Installed to C:\jdk1.2.2\ and C:\Program Files (x86)\JavaSoft\JRE\1.2\ by default – I recommend changing this to C:\Program Files (x86)\Java\jdk1.2.2\ and C:\Program Files (x86)\Java\jre1.2.2\ for the sake of clarity.
- Java SE 1.3 – Installed to C:\jdk1.3.1_28\ by default – I recommend changing this to C:\Program Files (x86)\Java\jdk1.3.1_28\ .
- Java SE 1.4 – Installed to C:\j2sdk1.4.2_19\ by default – I recommend changing this to C:\Program Files (x86)\Java\jdk1.4.2_19\ .
Starting with the following versions, you don’t need to change the default installation directories:
Attention – you may use the following Oracle distributions only for private purposes and development:
The following version is currently an early access build. You should use it only for testing purposes:
Environment variables
In most cases, the following two environment variables decide which Java version an application uses:
- JAVA_HOME – many start scripts use this variable.
- Path – is used when running a Java binary (such as java and javac ) from the console.
These variables should always point to the same Java installation to avoid unforeseen problems due to inconsistencies. Some programs, such as Eclipse, define the Java version in a separate configuration file (for Eclipse, for example, this is the entry «-vm» in the eclipse.ini file).
Setting environment variables manually
The installers of the Java versions listed above already create various environment variables, which you need to clean up first (see below). The fastest way to change the environment variables is to press the Windows key and type «env» – Windows then offers «Edit the system environment variables» as a search result:
At this point, you can press «Enter,» and the system properties appear:
Here you click on «Environment Variables…», after which the following window opens:
As the standard version, I currently use the latest release version, Java 16. Therefore, you should make the following settings:
- The top list («User variables») should not contain any Java-related entries.
- The lower list («System variables») should contain an entry «JAVA_HOME = C:\Program Files\Java\jdk-16\» (please check the version number, you may have a newer one). If this entry does not exist, you can add it with «New…». If it exists, but points to another directory, you can change it with «Edit…».
- Delete the following entries under «Path»:
- C:\ProgramData\Oracle\Java\javapath
- C:\Program Files (x86)\Common Files\Oracle\Java\javapath
- Insert the following entry instead:
- %JAVA_HOME%\bin
The path list should then look like this:
The last entry ensures that Path and JAVA_HOME are automatically consistent. Attention: this only works for the default setting configured here. If you change JAVA_HOME via the command line, you have to adjust Path accordingly (more about this below).
Now open a command line to check the settings with the following commands:
As a result, you should see this:
Scripts for changing the Java version
Finally, we come to the possibility to change the Java version quickly and easily. Therefore I create a separate command for each Java version, called, for example, java16 , java15 , java14 , java13 . I put these commands in the directory C:\Program Files\Java\scripts , which I add to the environment variable «Path» as described before:
The files are named, for example, java16.bat , java15.bat , java14.bat , java13.bat , and change the environment variables as shown in the following example of java16.bat :
This script, admittedly kept very simple, inserts the bin directory of the Java version to be activated at the beginning of the Path variable, so that is where, for example, java.exe is looked for first. The path variable becomes longer with each switching. However, since this is only the case for the currently opened command prompt, I don’t think this is a problem.
The following ZIP file contains all my scripts from Java 1.2 to Java 17 (you might have to adopt one or the other Java directory to your installation): scripts-up-to-jdk17.zip
You can quickly test whether the scripts work in a command line. Attention: after adding the scripts directory to the Path variable, you have to open a new command line.
At this point, the setup is complete. I hope this article was helpful for you, and I’d love to hear your comments. If you liked the article, I’d be happy if you shared it via one of the following share icons.
Multiple Windows in Java
I have written many FORTRAN codes, but am new to Java, which I find much more powerful and more difficult.
I have a program that was written several years ago in MS Access.
The program consists of a main window with buttons that take you to other windows where you input data and make queries.
Things have changed and the program needs to be rewritten. I want to rewrite it in Java.
I have written many small programs in Java, but they just do engineering calculations and only involve one window or screen.
To write Java programs, I have been using netbeans 5.5 and can easily design guis and add functionality.
However, I cannot figure out how to code multiple windows that interact with each other. For example, I would like to do the following:
1. Add a button1 to JFrame1, which will be the main screen-size window (I think they call it the Parent)
2. Add a button2 to JFrame2, which will be a secondary screen-size window (I think they call it a Child).
3. Add functionality so that when button1 is clicked, JFrame2 is displayed instead of JFrame1.
4. When button2 is clicked, JFrame1 is displayed again. Can you suggest how I can do this? I don’t see it covered in the three Java books I have been using.
Can supply a simple Java code that does this or direct me to a reference that explains how to do write the code.
Thank you.
- 5 Contributors
- forum 19 Replies 524 Views 4 Years Discussion Span
- comment Latest Post 8 Years Ago Latest Post by twmgeo
Recommended Answers
As long as the two frames have references to one another, you can easily interact between the two. Another idea may be to use a non-visual controller that can act as a mediator between the two frames and synchronize interactions and state changes as needed.
Here is a small example with a view controller. The two view classes defined have essentially the same code here (which you would never want to code into a real app), but since they just represent your other two frames I made them separate for clarity of the example.
The HashMap just provides a mechanism to look up a frame reference by name (the string key assigned when it was registered). The showView() method just loops through all of the views in the map and sets all but the requested one to not be visible.
Yes definitely. I only included all of those as static inner classes for compactness of the example. Each of those would normally be an independent class on it’s own. You will noticed that I used them as such in the main() method.
and oh, get into the habbit of always using packages for everything.
It may not be required by the language spec (an omission which by many is considered a major historical flaw in the language) but it’s good practice and many application frameworks and libraries expect all classes to be …
All 19 Replies
As long as the two frames have references to one another, you can easily interact between the two. Another idea may be to use a non-visual controller that can act as a mediator between the two frames and synchronize interactions and state changes as needed.
Here is a small example with a view controller. The two view classes defined have essentially the same code here (which you would never want to code into a real app), but since they just represent your other two frames I made them separate for clarity of the example.
This in itself is still probably not a solution to your needs, but without any more information on the relationship between your two views, it’s at least a starting point perhaps.
Thank you very much for the FrameExample.java code. It works very well and has provided an excellent start for what I need to do. I like the use of the Controller. Can you tell me why the hash map was needed or refer me to a reference that explains it? I do know what a hash map is, but I don’t understand why it is needed here. Apparently the program needs Lines 5 and 22-27 to compile.
The HashMap just provides a mechanism to look up a frame reference by name (the string key assigned when it was registered). The showView() method just loops through all of the views in the map and sets all but the requested one to not be visible.
This was just a quick-and-dirty example of using a controller to mediate some of your component interactions. There may be a much more suitable arrangement for your interface and usage needs, but you would need to post some more specific information the app and it’s organization.
The HashMap just provides a mechanism to look up a frame reference by name (the string key assigned when it was registered). The showView() method just loops through all of the views in the map and sets all but the requested one to not be visible.
This was just a quick-and-dirty example of using a controller to mediate some of your component interactions. There may be a much more suitable arrangement for your interface and usage needs, but you would need to post some more specific information the app and it’s organization.
The java program I am planning will display a number of windows, maybe 12, by clicking on a button in one window to move to another window. I believe I should have a separate Java file for each window because of the different actions required in each window. I tried to split the java file with a view controller, which was posted earlier, into two files, one for the main view and the other for the detail view, but was unsuccessful. When I ported them in a folder as a project to netBeans 6, I was unable to get a successful build because of errors associated with the Controller. Apparently, I did not properly provide the correct Controller statements in each of the two files. Can you divide the single java file into two files so that the Controller errors will be eliminated and result in a successful build and run? From what I have read, the use of a Controller is growing in popularity.
jdseader
Yes definitely. I only included all of those as static inner classes for compactness of the example. Each of those would normally be an independent class on it’s own. You will noticed that I used them as such in the main() method.
Yes definitely. I only included all of those as static inner classes for compactness of the example. Each of those would normally be an independent class on it’s own. You will noticed that I used them as such in the main() method.
Yes, I did notice the static inner classes. However, when I separated the file into two files, I was unable to eliminate the Controller errors.
I would appreciate any help, advice, or code you could send.
What are the Controller errors you are getting? I have no trouble putting them in separate files here. Are you placing the classes in the same package? If they are not in the same package you will need to add import statements for those classes. Beyond that I can’t see where you would get errors with those classes.
What are the Controller errors you are getting? I have no trouble putting them in separate files here. Are you placing the classes in the same package? If they are not in the same package you will need to add import statements for those classes. Beyond that I can’t see where you would get errors with those classes.
I did not put the two files in a package. Therefore, I did add import statements to the second file. The two source files were together in a project. The problem seems to be related to Controller Statements that may be needed in the second file, which goes from line 66 to 89 in the code that was sent. The second file begins with the 7 import statements. Lines 1 to 64 and Line 90 became the first, main, file. In line 66, I changed the word «static» to «public». The error messages all said that the class Controller could not be found in the second file.
Thank you for your interest and help.
If the classes are in the same package (directory) then you should not need the import statements. In Netbeans, you can also right click in the code editor and «Fix Imports».
I did not put the two files in a package. Therefore, I did add import statements to the second file. The two source files were together in a project. The problem seems to be related to Controller Statements that may be needed in the second file, which goes from line 66 to 89 in the code that was sent. The second file begins with the 7 import statements. Lines 1 to 64 and Line 90 became the first, main, file. In line 66, I changed the word «static» to «public». The error messages all said that the class Controller could not be found in the second file.
Thank you for your interest and help.
I am still getting errors with the Controller class when I try to build the project in NetBeans 6.0.
The Project consists of two Source files made from the single source file posted above.
The first file consists of lines 1 to 65 plus line 90. However, the following line has been added before line 1:
package multiplewindows.frames;
The second file consists of lines 66 to 89. However, before line 66, I added the line:
package multiplewindows.frames;
and after that line, I added the import lines 1 to 8.
When I try to build the project, I get the following error messages for the second file:
cannot find symbol
symbol : class Controller
location: class multiplewindows.frames.DetailView
final Controller viewController;
and the error message:
cannot find symbol
symbol : class Controller
location: class multiplewindows.frames.DetailView
public DetailView(Controller controller)<
It appears that I am not using the Controller properly with the two files. Can this be corrected?
Ok, that is because you left the Controller nested in FrameExample. Really each of those static classes should be moved up to their own top-level public classes. You don’t want the Controller nor the main view to remain nested in FrameExample.
You can leave main() as it is in FrameExample, but make each static class it’s own file in that same package and it’ll be fine.
Ok, that is because you left the Controller nested in FrameExample. Really each of those static classes should be moved up to their own top-level public classes. You don’t want the Controller nor the main view to remain nested in FrameExample.
You can leave main() as it is in FrameExample, but make each static class it’s own file in that same package and it’ll be fine.
Thank you very much for your prompt and very useful reply. I un-nested the Controller and MainView Classes in FrameExample. I also removed the word, «static» in lines 22 and 42 of the posted code. In the new DetailView java file, in the orginal line 66, I changed the word, «static» to «public». Now the code builds and runs successfully.
Clearly, I have a great deal to learn about Java programming.