Brandon
2012-04-26 12:36:55
The below code will EDIT your environment path variables to switch between Java 6 and Java 7.. This is for the guys who have both Java 6 & 7 installed and need to switch between them for smart and whatever other reasons. For the code Guru's.. Yes I know it can be optimized but it's a simple console program that takes 0 CPU usage so forget it.
Instructions:
I compiled with codeblocks so here are the pictures:
1:

2:

3 CHOOSE A C++ FILE, Click Next, Click Next then you get this window.. Save as Anything you want then press Finished:

4 Copy all the code below into the editor and change the two strings shown below:

5. Press the play button with the gears on it to compile.. Look on your desktop and you will have an EXE file. Run as administrator and choose your Java and there you go.
Instructions:
I compiled with codeblocks so here are the pictures:
1:

2:

3 CHOOSE A C++ FILE, Click Next, Click Next then you get this window.. Save as Anything you want then press Finished:

4 Copy all the code below into the editor and change the two strings shown below:

5. Press the play button with the gears on it to compile.. Look on your desktop and you will have an EXE file. Run as administrator and choose your Java and there you go.
#include <iostream>
#include <windows.h>
#include <string>
#include <stdlib.h>
using namespace std;
string getEnvVar(string const & key)
{
char * val = getenv(key.c_str());
return val == NULL ? string("") : string(val);
}
bool EditRegKey(string NewValue)
{
HKEY key;
DWORD buffersize = 1024; // Declare the size of the data buffer
char* lpData = new char[buffersize];
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment", 0, KEY_ALL_ACCESS, &key) == ERROR_SUCCESS)
{
RegQueryValueEx(key,"Path",NULL,NULL,(LPBYTE)lpData,&buffersize);
if (RegSetValueExA(key, "Path", 0, REG_EXPAND_SZ, (LPBYTE)NewValue.c_str(), NewValue.size() + 1) == ERROR_SUCCESS)
{
DWORD result2 = 0;
LRESULT result = SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)"Environment", SMTO_ABORTIFHUNG, 5000, &result2);
}
RegCloseKey(key);
}
delete lpData;
}
string Java6To7(string CurrentVariables, string Java6, string Java7)
{
int Pos1 = CurrentVariables.find(Java6);
if (Pos1 != string::npos)
{
int Pos2 = CurrentVariables.find(";", Pos1 + 1);
if (Pos2 != string::npos)
CurrentVariables.replace(Pos1, strlen(Java6.c_str()), Java7);
}
return CurrentVariables;
}
string Java7To6(string CurrentVariables, string Java6, string Java7)
{
int Pos1 = CurrentVariables.find(Java7);
if (Pos1 != string::npos)
{
int Pos2 = CurrentVariables.find(";", Pos1 + 1);
if (Pos2 != string::npos)
CurrentVariables.replace(Pos1, strlen(Java7.c_str()), Java6);
}
return CurrentVariables;
}
bool isDigitsOnly(string Input)
{
double DoubleParseTest = 0;
if (Input.size() < 1) return false;
for (int I = 0; I < Input.size(); I++)
if (!isdigit(Input[I]))
{
return false;
}
return true;
}
int main()
{
string CurrentVariables = getEnvVar("PATH");
string Java6 = "C:\\Program Files\\Java\\jdk1.6.0_31\\bin\\";
string Java7 = "C:\\Program Files\\Java\\jdk1.7.0\\bin\\";
cout<<"1: Java 6 To 7.\n";
cout<<"2: Java 7 To 6.\n\n";
string Choice = "0";
while (Choice != "1" || Choice != "2")
{
cout<<"Choice: ";
cin>>Choice;
if (Choice == "1" || Choice == "2")
break;
cout<<"\n";
}
if (Choice == "1")
{
CurrentVariables = Java6To7(CurrentVariables, Java6, Java7);
EditRegKey(CurrentVariables);
}
else
{
CurrentVariables = Java7To6(CurrentVariables, Java6, Java7);
EditRegKey(CurrentVariables);
}
cout<<"Finished..\n";
system("PAUSE");
}