Skip to content

Commit c7955e3

Browse files
committed
Initial commit
0 parents  commit c7955e3

File tree

6 files changed

+800
-0
lines changed

6 files changed

+800
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

JDKSwitcher_forWindows.cpp

Lines changed: 356 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,356 @@
1+
#include <io.h>
2+
#include <string>
3+
#include <vector>
4+
#include <fstream>
5+
#include <iostream>
6+
#include <bits/stdc++.h>
7+
#include <conio.h>
8+
#include <stdlib.h>
9+
#include <windows.h>
10+
#include <fstream>
11+
12+
std::string* getJava(){
13+
return new std::string(getenv("JAVA_HOME"));
14+
}
15+
std::string* quoteString(std::string* str){
16+
str->insert(0,1,'\"');
17+
str->insert(str->length(),1,'\"');
18+
return str;
19+
}
20+
void setJava(char* var){
21+
std::string* cmd=new std::string("");//empty
22+
std::string* junk1;
23+
cmd->append("setx ");
24+
cmd->append("\"JAVA_HOME\" ");
25+
junk1 = quoteString(new std::string(var));
26+
cmd->append(junk1->c_str());
27+
//cmd->append(" /U ");
28+
//cmd->append(username);
29+
const char * cmdConstant=cmd->c_str();
30+
system(cmdConstant);
31+
delete cmd;
32+
delete junk1;
33+
}
34+
void readFromConfig(std::vector<std::string>& jdks){
35+
std::fstream config("jdk.cfg");
36+
if(config.good()){
37+
std::string line;
38+
while(std::getline(config,line)){
39+
jdks.push_back(line);
40+
}
41+
}else{
42+
config<<"";//create;
43+
}
44+
}
45+
void getAllFiles(std::string path, std::vector<std::string>& files)
46+
{
47+
// 文件句柄
48+
long hFile = 0;
49+
// 文件信息
50+
struct _finddata_t fileinfo;
51+
52+
std::string p;
53+
54+
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1) {
55+
do {
56+
// 保存文件的全路径
57+
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
58+
59+
} while (_findnext(hFile, &fileinfo) == 0); //寻找下一个,成功返回0,否则-1
60+
61+
_findclose(hFile);
62+
}
63+
}
64+
void readFromProgram(std::vector<std::string>& jdks){
65+
std::vector<std::string> s;
66+
getAllFiles("C:\\Program Files\\Java",s);
67+
std::vector<std::string>::iterator i;
68+
for(i=s.begin();i!=s.end();++i){
69+
if((*i).find("jdk1.8",0)!=std::string::npos || (*i).find("jdk-1.8",0)!=std::string::npos || ((*i).find("j")!=std::string::npos )){
70+
jdks.push_back(*i);
71+
}
72+
}
73+
}
74+
#define white 0
75+
#define red 1
76+
#define blue 2
77+
#define yellow 3
78+
#define green 4
79+
#define purple 5
80+
#define cyan 6
81+
#define gray 7
82+
#define black 8
83+
#define DEBUG 0
84+
#define LL long long
85+
int getawaw(){
86+
std::cout<<"按任意键继续"<<std::endl;
87+
char a=_getch();
88+
}
89+
void color(LL n){
90+
if (n==0) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_RED);
91+
if (n==1) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED);
92+
if (n==2) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_BLUE);
93+
if (n==3) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_RED);
94+
if (n==4) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN);
95+
if (n==5) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_BLUE|FOREGROUND_RED);
96+
if (n==6) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_BLUE);
97+
if (n==7) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY);
98+
if (n==8) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0);
99+
}
100+
void cursorhide(){
101+
HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
102+
CONSOLE_CURSOR_INFO CursorInfo;
103+
GetConsoleCursorInfo(handle,&CursorInfo);
104+
CursorInfo.bVisible=false;
105+
SetConsoleCursorInfo(handle,&CursorInfo);
106+
}
107+
void cursorshow(){
108+
HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
109+
CONSOLE_CURSOR_INFO CursorInfo;
110+
GetConsoleCursorInfo(handle,&CursorInfo);
111+
CursorInfo.bVisible=true;
112+
SetConsoleCursorInfo(handle,&CursorInfo);
113+
}
114+
std::pair <int,int> getxy(){
115+
HANDLE hStdout;
116+
CONSOLE_SCREEN_BUFFER_INFO pBuffer;
117+
hStdout=GetStdHandle(STD_OUTPUT_HANDLE);
118+
GetConsoleScreenBufferInfo(hStdout,&pBuffer);
119+
return std::make_pair(pBuffer.dwCursorPosition.X,pBuffer.dwCursorPosition.Y);
120+
}
121+
void gotoxy(int x,int y){
122+
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
123+
HANDLE hConsoleOut;
124+
hConsoleOut=GetStdHandle(STD_OUTPUT_HANDLE);
125+
GetConsoleScreenBufferInfo(hConsoleOut,&csbiInfo);
126+
csbiInfo.dwCursorPosition.X=x;
127+
csbiInfo.dwCursorPosition.Y=y;
128+
SetConsoleCursorPosition(hConsoleOut,csbiInfo.dwCursorPosition);
129+
}
130+
void tellraw(std::string s,LL num1=0,LL num2=0,LL num3=0){
131+
//system("color 1A");
132+
color(white);
133+
bool colorful=false;
134+
bool pausing=false;
135+
int len=s.size();
136+
char c;
137+
for (LL i=0;i<len;++i){
138+
if (s[i]=='&'){
139+
i++;
140+
if (s[i]=='w') color(white);
141+
if (s[i]=='r') color(red);
142+
if (s[i]=='b') color(blue);
143+
if (s[i]=='y') color(yellow);
144+
if (s[i]=='c') color(cyan);
145+
if (s[i]=='p') color(purple);
146+
if (s[i]=='g') color(green);
147+
if (s[i]=='a') color(gray);
148+
if (s[i]=='n') color(black);
149+
if (s[i]=='+') pausing=true;
150+
else if (s[i]=='-') pausing=false;
151+
if (s[i]=='*') colorful=true;
152+
else colorful=false;
153+
}else if (s[i]=='%'){
154+
i++;
155+
if (s[i]=='1') printf("%lld",num1);
156+
if (s[i]=='2') printf("%lld",num2);
157+
if (s[i]=='3') printf("%lld",num3);
158+
}else if (s[i]=='|'){
159+
i++;
160+
if (s[i]=='n') putchar('\n');
161+
if (s[i]=='t') putchar('\t');
162+
if (s[i]=='|') putchar('|');
163+
if (s[i]=='%') printf("%%");
164+
if (s[i]=='&') putchar('&');
165+
if (s[i]=='p') c=_getch();
166+
if (s[i]=='c') system("cls");
167+
if (s[i]=='k') Sleep((DEBUG)?10:500);
168+
}else{
169+
if (colorful==true) color(rand()%8);
170+
if (pausing==true) Sleep((DEBUG)?2:100);
171+
putchar(s[i]);
172+
}
173+
}
174+
color(white);
175+
}
176+
void tellrawfast(std::string s,LL num1=0,LL num2=0,LL num3=0){
177+
//system("color 1A");
178+
color(white);
179+
bool colorful=false;
180+
bool pausing=false;
181+
int len=s.size();
182+
char c;
183+
for (LL i=0;i<len;++i){
184+
if (s[i]=='&'){
185+
i++;
186+
if (s[i]=='w') color(white);
187+
if (s[i]=='r') color(red);
188+
if (s[i]=='b') color(blue);
189+
if (s[i]=='y') color(yellow);
190+
if (s[i]=='c') color(cyan);
191+
if (s[i]=='p') color(purple);
192+
if (s[i]=='g') color(green);
193+
if (s[i]=='a') color(gray);
194+
if (s[i]=='n') color(black);
195+
if (s[i]=='+') pausing=true;
196+
else if (s[i]=='-') pausing=false;
197+
if (s[i]=='*') colorful=true;
198+
else colorful=false;
199+
}else if (s[i]=='%'){
200+
i++;
201+
if (s[i]=='1') printf("%lld",num1);
202+
if (s[i]=='2') printf("%lld",num2);
203+
if (s[i]=='3') printf("%lld",num3);
204+
}else if (s[i]=='|'){
205+
i++;
206+
if (s[i]=='n') putchar('\n');
207+
if (s[i]=='t') putchar('\t');
208+
if (s[i]=='|') putchar('|');
209+
if (s[i]=='%') printf("%%");
210+
if (s[i]=='&') putchar('&');
211+
if (s[i]=='p') c=_getch();
212+
if (s[i]=='c') system("cls");
213+
if (s[i]=='k') Sleep((DEBUG)?10:500);
214+
}else{
215+
if (colorful==true) color(rand()%8);
216+
if (pausing==true) Sleep((1)?2:100);
217+
putchar(s[i]);
218+
}
219+
}
220+
color(white);
221+
}
222+
char readkey(std::string s,std::string range=""){
223+
tellraw(s);
224+
int len=range.size();
225+
char c=_getch();
226+
while (true){
227+
for (int i=0;i<len;++i){
228+
if (c==range[i]) return c;
229+
}
230+
c=_getch();
231+
}
232+
}
233+
std::string inputbox(std::string s){
234+
tellraw(s+"|n");
235+
cursorshow();
236+
std::string t;
237+
color(white);
238+
std::cin>>t;
239+
cursorhide();
240+
return t;
241+
}
242+
int chosenbox(std::string s){
243+
int len=s.size();
244+
int i=0;
245+
std::string t="";
246+
while (s[i]!='@' && i<len) t=t+s[i],i++;
247+
tellraw(t);
248+
putchar('\n');
249+
std::pair <int,int> tmp=getxy();
250+
int j=i;
251+
t="";
252+
int item=0;
253+
for (int i=j+1;i<len;++i){
254+
if (s[i]=='#'){
255+
j=i;
256+
break;
257+
}else if (s[i]=='@'){
258+
item++;
259+
tellraw(" &c%1. &w"+t+"|n",item);
260+
t="";
261+
}else{
262+
t=t+s[i];
263+
}
264+
}
265+
t="";
266+
for (int i=j+1;i<len;++i){
267+
t=t+s[i];
268+
}
269+
tellraw(t);
270+
std::pair <int,int> tmp2=getxy();
271+
gotoxy(0,tmp.second);
272+
tellraw("&w>>");
273+
char c=_getch();
274+
int chosen=1;
275+
while (c!=13){
276+
if (c==72 || c==72){
277+
if (chosen>1){
278+
gotoxy(0,tmp.second+chosen-1);
279+
chosen--;
280+
printf(" ");
281+
gotoxy(0,tmp.second+chosen-1);
282+
tellraw("&w>>");
283+
}
284+
}else if (c==80 || c==80){
285+
if (chosen<item){
286+
gotoxy(0,tmp.second+chosen-1);
287+
chosen++;
288+
printf(" ");
289+
gotoxy(0,tmp.second+chosen-1);
290+
tellraw("&w>>");
291+
}
292+
}else if (c>='1' && c<='9' && c-48<=item){
293+
gotoxy(0,tmp.second+chosen-1);
294+
chosen=c-48;
295+
printf(" ");
296+
gotoxy(0,tmp.second+chosen-1);
297+
tellraw("&w>>");
298+
}else if (c==75 || c==75){
299+
gotoxy(0,tmp.second+chosen-1);
300+
chosen=1;
301+
printf(" ");
302+
gotoxy(0,tmp.second+chosen-1);
303+
tellraw("&w>>");
304+
}else if (c==77 || c==77){
305+
gotoxy(0,tmp.second+chosen-1);
306+
chosen=item;
307+
printf(" ");
308+
gotoxy(0,tmp.second+chosen-1);
309+
tellraw("&w>>");
310+
}
311+
c=_getch();
312+
}
313+
gotoxy(tmp2.first,tmp2.second);
314+
return chosen;
315+
}
316+
std::string getJavaChosen(std::vector<std::string>& jdks){
317+
std::vector<std::string>::iterator o;
318+
std::string cho;
319+
cho=cho+"@";
320+
//vector<string> choice;
321+
std::string Java8List[100];
322+
std::string Java8ListPath[100];
323+
int ioindex=1;
324+
for(o=jdks.begin();o!=jdks.end();++o){
325+
std::string p_1=(*o);
326+
for(int p_2=0;p_2<p_1.size();p_2++){
327+
if(p_1[p_2]=='\\'){
328+
p_2++;
329+
p_1.insert(p_2,"\\");
330+
}
331+
}
332+
Java8List[ioindex]=p_1;
333+
Java8ListPath[ioindex]=(*o);
334+
ioindex++;
335+
//choice.push_back(p_1);
336+
cho=cho+(*o)+"@";
337+
}
338+
cho=cho+"#";
339+
340+
341+
int index=chosenbox(cho);
342+
343+
std::cout<<std::endl;
344+
std::cout<<std::endl;
345+
std::cout<<std::endl;
346+
347+
return Java8ListPath[index];
348+
}
349+
int main(){
350+
std::vector<std::string> jdks;
351+
readFromConfig(jdks);
352+
readFromProgram(jdks);
353+
std::string cur=getJavaChosen(jdks);
354+
setJava((char*)cur.c_str());
355+
}
356+

JDKSwitcher_forWindows.exe

1.88 MB
Binary file not shown.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Hileb
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# JDKSwitcher
2+
switch "JAVA_HOME"

0 commit comments

Comments
 (0)