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的小工具-创新互联
文章起源:https://www.cdcxhl.com/article8/dpcsop.html

成都网站建设公司_创新互联,为您提供响应式网站Google营销型网站建设软件开发用户体验标签优化

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都做网站