Windows下切换JDK的小工具-创新互联

电脑有很多JDK,每次想用Terminal运行一些java程序的时候,高版本和低版本会有不兼容的问题,每次去系统环境变量里面改JAVA_HOME也太笨了吧。
所以写了个这么个小玩意儿,难度不大,但是很方便。
建议PATH里面添加这个程序所在目录。
内容很简单,不需要注释。
使用就很简单啊,比如编译完叫main,那就main jdk8 -y命令,在cmd、win+r、都可以用
需要在Visual Studio 2022的编译选项中加上以管理员运行。
嗯,用起来还算方便一点的。

创新互联一直通过网站建设和网站营销帮助企业获得更多客户资源。 以"深度挖掘,量身打造,注重实效"的一站式服务,以成都做网站、成都网站设计、移动互联产品、全网营销推广服务为核心业务。十多年网站制作的经验,使用新网站建设技术,全新开发出的标准网站,不但价格便宜而且实用、灵活,特别适合中小公司网站制作。网站管理系统简单易用,维护方便,您可以完全操作网站资料,是中小公司快速网站建设的选择。
#define _CRT_SECURE_NO_WARNINGS
#include#include#include#include#include#includeusing 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);
		}
		vectorlines = vector();
		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(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;
	mapconfigs;
	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);
	}
}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


新闻名称:Windows下切换JDK的小工具-创新互联
URL链接:http://hbruida.cn/article/dpcsop.html