很多java爱好者都知道,安装jdk一定要先设置java环境变量。而且设置java环境变量繁琐耗时间,要是有个jav环境变量自动设置工具就好了,省去很多麻烦。现在拓胜小编就分享一个用C语言编写的java环境变量自动设置工具,操作过程非常的简单,你只要设置JDK的安装路径就行了,其他的都默认设置就好了。如下图
主要代码:
#include "stdafx.h"
#include
#include
#include "resource.h"
#include "MainDlg.h"
TCHAR szJavahome[_MAX_PATH]={0};
TCHAR szClasspath[] = "%Javahome%\\lib;%Javahome%\\lib\\tools.jar";
TCHAR szPath[]="%Javahome%\\bin;%Javahome%\\jre\\bin";
BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog);
HANDLE_MSG(hWnd, WM_COMMAND, Main_OnCommand);
HANDLE_MSG(hWnd,WM_CLOSE, Main_OnClose);
}
return FALSE;
}
BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
{
HWND hJavahome = GetDlgItem(hwnd,IDC_Javahome);
HWND hClasspath = GetDlgItem(hwnd,IDC_ClassPath);
HWND hPath = GetDlgItem(hwnd,IDC_Path);
SetWindowText(hJavahome,TEXT("这里输入JDK路径!例如:C:\\Program Files\\Java\\jdk1.6.0_10"));
SetWindowText(hClasspath,szClasspath);
SetWindowText(hPath,szPath);
return TRUE;
}
void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
HWND hJavahome = GetDlgItem(hwnd,IDC_Javahome);
HWND hClasspath = GetDlgItem(hwnd,IDC_ClassPath);
HWND hPath = GetDlgItem(hwnd,IDC_Path);
switch(id)
{
case IDC_OK:
{
TCHAR szKey[] = "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment";
HKEY hkResult;
GetWindowText(hJavahome,szJavahome,sizeof(szJavahome)/sizeof(TCHAR));
if(lstrcmp(szJavahome,"") && lstrcmp(szClasspath,"") && lstrcmp(szPath,""))
{
if(RegOpenKey(HKEY_LOCAL_MACHINE,szKey,&hkResult) != ERROR_SUCCESS)
{
MessageBox(hwnd,TEXT("注册表打开失败,请检查你是否有权限对注册表进行操作!"),TEXT("Error"),MB_OK|MB_ICONWARNING);
}
int ret1 = RegSetValueEx(hkResult,"Javahome",0,REG_EXPAND_SZ,(unsigned char *)szJavahome,sizeof(szJavahome));
int ret2 = RegSetValueEx(hkResult,"Classpath",0,REG_EXPAND_SZ,(unsigned char *)szClasspath,sizeof(szClasspath));
int ret3 = RegSetValueEx(hkResult,"Javapath",0,REG_EXPAND_SZ,(unsigned char *)szPath,sizeof(szPath));
if((ret1 && ret1 && ret3) == 0)
MessageBox(hwnd,TEXT("Java系统环境变量设置成功!"),TEXT("Success"),MB_OK);
else
MessageBox(hwnd,TEXT("Java系统环境变量设置失败!"),TEXT("Error"),MB_OK|MB_ICONWARNING);
}
else
{
MessageBox(hwnd,TEXT("路径输入有错误,请检查你的输入是否完整!"),TEXT("Error"),MB_OK|MB_ICONWARNING);
}
}
break;
default:
break;
}
}
void Main_OnClose(HWND hwnd)
{
EndDialog(hwnd, 0);
}