SRL Archive - C/C++ Help and Tutorials

Java Switcher 6 to 7 & vice-versa


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.


#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");
}

Echo_

2012-04-26 17:32:10

You could write a simple batch script to do this

Brandon

2012-04-26 17:39:55

From: Echo_
You could write a simple batch script to do this


And I did I just don't remember what I had it in.. I used SetENV in the file but meh..

YoHoJo

2012-04-26 20:29:01

Do you have do use \\ or can you just do \ when putting the file location?
Would
C:\Program Files (x86)\Java\jre6\bin
be fine?

Brandon

2012-04-27 02:20:47

From: YoHoJo
Do you have do use \\ or can you just do \ when putting the file location?
Would
C:\Program Files (x86)\Java\jre6\bin
be fine?


It has to be double slashes.

One slash is the escape character slash. The next slash is the one to be escaped.

So
\\ is read as \
\n is new line.
\r is return.

YoHoJo

2012-04-27 18:12:18

I did it all, pressed play button with gears, but see no .exe file.
Just the .cpp file :/

E: When I press play/gear nothing even seems to happen.

E: I see in your example you use Program Files, bur isn't 32 bit java in program files (x86) like this
string Java6 = "C:\\Program Files (x86)\\Java\\jre6\\bin\\";
string Java7 = "C:\\Program Files (x86)\\Java\\jre7\\bin\\";

YoHoJo

2012-04-28 03:44:02

Hulp Mi