Windows下切換JDK的小工具-創(chuàng)新互聯(lián)

電腦有很多JDK,每次想用Terminal運(yùn)行一些java程序的時(shí)候,高版本和低版本會(huì)有不兼容的問題,每次去系統(tǒng)環(huán)境變量里面改JAVA_HOME也太笨了吧。
所以寫了個(gè)這么個(gè)小玩意兒,難度不大,但是很方便。
建議PATH里面添加這個(gè)程序所在目錄。
內(nèi)容很簡(jiǎn)單,不需要注釋。
使用就很簡(jiǎn)單啊,比如編譯完叫main,那就main jdk8 -y命令,在cmd、win+r、都可以用
需要在Visual Studio 2022的編譯選項(xiàng)中加上以管理員運(yùn)行。
嗯,用起來還算方便一點(diǎn)的。

創(chuàng)新互聯(lián)一直通過網(wǎng)站建設(shè)和網(wǎng)站營(yíng)銷幫助企業(yè)獲得更多客戶資源。 以"深度挖掘,量身打造,注重實(shí)效"的一站式服務(wù),以成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、移動(dòng)互聯(lián)產(chǎn)品、全網(wǎng)營(yíng)銷推廣服務(wù)為核心業(yè)務(wù)。十多年網(wǎng)站制作的經(jīng)驗(yàn),使用新網(wǎng)站建設(shè)技術(shù),全新開發(fā)出的標(biāo)準(zhǔn)網(wǎng)站,不但價(jià)格便宜而且實(shí)用、靈活,特別適合中小公司網(wǎng)站制作。網(wǎng)站管理系統(tǒng)簡(jiǎn)單易用,維護(hù)方便,您可以完全操作網(wǎng)站資料,是中小公司快速網(wǎng)站建設(shè)的選擇。
#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);
	}
}

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧

當(dāng)前題目:Windows下切換JDK的小工具-創(chuàng)新互聯(lián)
文章起源:http://muchs.cn/article8/dpcsop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、Google、營(yíng)銷型網(wǎng)站建設(shè)軟件開發(fā)、用戶體驗(yàn)標(biāo)簽優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站