Windows下切换JDK的小工具

电脑有很多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);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值