电脑有很多JDK,每次想用Terminal运行一些java程序的时候,高版本和低版本会有不兼容的问题,每次去系统环境变量里面改JAVA_HOME也太笨了吧。
所以写了个这么个小玩意儿,难度不大,但是很方便。
建议PATH里面添加这个程序所在目录。
内容很简单,不需要注释。
使用就很简单啊,比如编译完叫main,那就main jdk8 -y命令,在cmd、win+r、都可以用
需要在Visual Studio 2022的编译选项中加上以管理员运行。
嗯,用起来还算方便一点的。
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<Windows.h>
#include<fstream>
#include<map>
#include<vector>
#include<string>
using namespace std;
class Config {
public:
Config(const char**envp,string jdkName) {
this->envp = envp;
readConfig();
checkCommand(jdkName);
if (!commandValid) {
exit(1);
}
checkJavaHome();
setJavaHome(jdkName);
}
Config(const char** envp, string jdkName, string options) {
if (options != "-y") {
cout << "[ERROR] Invalid operation,use -y to confirm changing JAVA_HOME" << endl;
system("pause");
exit(1);
}
confirm = true;
ignore = true;
this->envp = envp;
readConfig();
checkCommand(jdkName);
if (!commandValid) {
exit(1);
}
checkJavaHome();
setJavaHome(jdkName);
}
Config(const char** envp) {
this->envp = envp;
readConfig();
string jdkName = listJDK();
checkCommand(jdkName);
if (!commandValid) {
exit(1);
}
checkJavaHome();
setJavaHome(jdkName);
}
void readConfig() {
ifstream fis(configFile.c_str());
if (!fis) {
cout << "[ERROR] Cannot find config file." << endl;
valid = false;
system("pause");
exit(1);
}
vector<string> lines = vector<string>();
string line;
while (std::getline(fis, line)) {
lines.push_back(line);
}
for (string line : lines) {
size_t index = line.find("=");
if (index != string::npos) {
string key = line.substr(0, index);
string value = line.substr(index + 1);
if (value.length() == 0) {
cout << "[ERROR] Config invalid! key: " << key << " value: " << value << endl;
system("pause");
exit(1);
}
configs.insert(pair<string, string>(key, value));
}
else {
valid = false;
return;
}
}
if (configs.size() == 0) {
cout << "[INFO]:Cannot find any jdk home from config" << endl;
valid = false;
}
}
void checkCommand(string jdkName) {
auto end = configs.end();
if ((configs.find(jdkName) == end)) {
cout << "[ERROR] Cannot find the specified jdkName" << endl;
commandValid = false;
}
}
string listJDK() {
cout << "[INFO] JDK List:" << endl;
for (auto& [k, v] : configs) {
cout << k << " " << v << endl;
}
cout << "[INFO] Select one:";
string jdkName;
cin >> jdkName;
return jdkName;
}
void checkJavaHome() {
for (int i = 0; envp[i]; i++) {
string line = envp[i];
size_t index = line.find("=");
if (index != string::npos) {
string key = line.substr(0, index);
string value = line.substr(index + 1);
if (key == "JAVA_HOME") {
existJavaHome = true;
}
else if (key == "Path") {
path = value;
}
}
}
if (!existJavaHome) {
cout << "[INFO] JAVA_HOME not exist,It will be automatically created later." << endl;
}
}
void setJavaHome(string jdkName) {
string cmd = "setx JAVA_HOME \"" + configs.find(jdkName)->second + "\" /m";
if (!confirm) {
cout << "[INFO] This command will execute: \"" + cmd + "\" Type Y/y to confirm." << endl;
char ch;
cin >> ch;
if (ch == 'Y' || ch == 'y') {
confirm = true;
}
}
if (confirm) {
system(cmd.c_str());
}
else {
cout << "[INFO] Operation canceled." << endl;
}
if (!ignore) {
system("pause");
}
}
static void setConfigFilePath(string path) {
configFile = path;
}
private:
const char** envp;
bool valid = true;
bool existJavaHome = false;
bool commandValid = true;
bool confirm = false;
bool ignore = false;
map<string, string> configs;
static string configFile;
string path;
};
string Config::configFile= "jdkList.txt";
int main(int argc, const char** argv,const char**envp) {
bool existWindowsKit = false;
for (int i = 0; envp[i]; i++) {
string line = envp[i];
size_t index = line.find("=");
if (index != string::npos) {
string key = line.substr(0, index);
string value = line.substr(index + 1);
if (key == "WINDOWS_KITS") {
Config::setConfigFilePath(value + "\\jdkList.txt");
existWindowsKit = true;
break;
}
}
}
if (!existWindowsKit) {
cout << "[ERROR] Cannot find env:WINDOWS_KITS,thus,cannot find config file." << endl;
system("pause");
exit(1);
}
if (argc == 2) {
Config config(envp,argv[1]);
}
else if (argc == 3) {
Config config(envp, argv[1], argv[2]);
}
else {
cout << "[INFO] You must specify which JAVA_HOME to enable." << endl;
Config config(envp);
}
}